#!/bin/bash
#
# Authors: 	Jochen Schaefer 	<jschaef@novell.com>,  2016 
#		Dr. Frieder Schmidt 	<fschmidt@novell.com>, 2016
# 
# copyright (c) Novell Deutschland GmbH, 2012. All rights reserved.
# GNU Public License
#
# create-vce-unterricht.sh				27 Apr 2016
# modified: RLa						10 Juni 2016
#
#
# Purpose:
# This scripts processes a LDIF template file replacing place holders with
# actual values and imports the resulting LDIF files into eDirectory creating
# the structure and parts of the ACLs for a new school.
#
# A second LDIF file is used to add ACLs that could not be set during import
# of the first LDIF file do to unresolved references.


ARGV=$@
function debug()
{
	if [ "$1" != "0" ];then
		set -x
	fi
}


function root_req()
{
	function root_reqerror()
	{
		cat <<HERE
	only root can use this script !!!
HERE
	exit 7
	}	
	test $UID -eq $1 || root_reqerror
}


function usage()
{
        cat <<HERE

usage $0
	--admin_dn	<dn of admin user>		# distinguish name of eDirectory administrator
							# in LDAP syntax (comma separated)
							# optional, not required with --create_ldif
	--school	<school>			# name of school, i.e. Schule1
	--area		<world-object>			# organization unit, i.e world-object
	--org		<organization>			# name of organization
	--zcm		<IP address>			# IP address of the ZCM primary server
	--userpass	<vceAdmin Password>		# Passwort for vceAdmin User
	--create_ldif   				# optional: only creates the ldif files without doing corresponding
							# ldap operations (may be helpful troubleshooting syntax errors)


        example: $0 --admin_dn cn=Admin,ou=Adm,o=CL --school Schule<n> --area world-object --org organization --zcm <IP of ZCM Primary> --userpass Passwort [--create_ldif] [--debug]

HERE
        exit 1
}


function getopt()
{
        if [ $# -lt 1 ];then
                echo -ne "\n!!! No parameter specified !!!\n\n"
                usage
	fi

        if [ $# -lt 8 ];then
                echo -ne "\n!!! Sorry, need at least three command line parameters and their values to do my job ;-) !!!\n\n"
                usage
        fi

        while [ $# -gt 0 ];do
                case $1 in --admin_dn) 	  ADMIN_DN=$2;shift;;
                           --school) 	  SCHOOL=$2;shift;;
			   --area) 	  AREA=$2;shift;;
			   --org) 	  ORG=$2;shift;;
                           --zcm) 	  ZCMIP=$2;shift;;
			   --userpass) 	  USERPASS=$2;shift;;
			   --create_ldif) CREATE_LDIF=1;;
			   --create-ldif) CREATE_LDIF=1;;
                           --debug) debug=1;debug;;
                           *) echo "unknown parameter provided"; 
				usage;;
                esac
                shift
        done
        if [ -z "$ADMIN_DN" ] && [ -z "$CREATE_LDIF" ];then
		echo -ne "\n!!! No admin specified; exit here!!!\n"
                usage
	elif [ -z "$SCHOOL" ];then
		echo -ne "\n!!! A school must be provided!!!\n"
                usage
        fi

	# format value of SCHOOL; first letter uppercase, remaining letters lower case
	SCHOOL=$(tr a-z A-Z <<<${SCHOOL:0:1})$(tr A-Z a-z <<<${SCHOOL:1:${#SCHOOL}})
}


function set_vars()
{
	LIB_FILE=ncif-lib.sh
	LIB_PATH=/usr/local/ncif
}


function set_func_vars()
{
	LDAP_OPTS="-x -ZZ -D $ADMIN_DN"

	# ldif files
	INPUT_FILE1=ldapadd_template-unterricht.ldif
	INPUT_FILE2=ldapmodify_template-unterricht.ldif
	OUTPUT_FILE1=ldapadd-${SCHOOL}-unterricht.ldif
	OUTPUT_FILE2=ldapmodify-${SCHOOL}-unterricht.ldif

	for file in 1 2; do
		echo "#" >$(eval echo \$OUTPUT_FILE${file})
	done
}

function source_lib()
{
	if [ -f "$(pwd)/$LIB_FILE" ];then
		source $(pwd)/$LIB_FILE
	elif [ -f "$LIB_PATH/$LIB_FILE" ]; then
		source $LIB_PATH/$LIB_FILE
	else
		echo -ne "\n!!! Sorry $LIB_FILE neither does exist in $(pwd) nor in $LIB_PATH!!!\n" 
		echo -ne "\n\tPlease provide the library $LIB_FILE\n"
		exit 2
	fi
	
}


function check_env()
{
	local file
        for file in $INPUT_FILE1 $INPUT_FILE2; do
                if [ ! -f "$file" -o ! -f "$(pwd)/$file" ]; then
                        echo -ne "\n!!! $file does neither exist in $(pwd) nor in $LIB_PATH!!!\n"
                        exit 3
		fi
	done
}

function apply_ldifs()
{

        # replacing place holders in template file with actual data producing the LDIF for import

	echo -e "#########################################" >>$OUTPUT_FILE1
	echo -e "# Creating structure for school $SCHOOL #" >>$OUTPUT_FILE1
	echo -e "#########################################" >>$OUTPUT_FILE1

	echo -e "##########################################" >>$OUTPUT_FILE2
	echo -e "# Modifying structure for school $SCHOOL #" >>$OUTPUT_FILE2
	echo -e "##########################################" >>$OUTPUT_FILE2


	for file in 1 2; do
		while IFS='' read -r LINE;
		  do
		  sed -e "s/%%SCHOOL%%/$SCHOOL/g" <<<"$LINE" | \
		  sed -e "s/%%AREA%%/$AREA/g" | \
		  sed -e "s/%%ORG%%/$ORG/g" | \
		  sed -e "s/%%SERVER%%/$HOSTNAME/g" | \
		  sed -e "s/%%ZCMIP%%/$ZCMIP/g" | \
		  sed -e "s/%%USERPASS%%/$USERPASS/g" >> $(eval echo \$OUTPUT_FILE${file}) 
		done < $(eval echo \$INPUT_FILE${file})
		echo -ne "\n$(eval echo \$OUTPUT_FILE${file}) created\n"	

	done

	# performing the LDIF operations
	if [ -z "$CREATE_LDIF" ]; then
		echo -ne "\nImporting LDIF files into tree\n"
		ldapadd    $LDAP_OPTS -w $password -c -f $OUTPUT_FILE1
		ldapmodify $LDAP_OPTS -w $password -c -f $OUTPUT_FILE2
	fi

}


#=======================================================================================

function main()
{
	getopt  $ARGV
	set_vars
	source_lib
	set_func_vars
	check_env
	if [ -z "$CREATE_LDIF" ];then
		get_pass $ADMIN_DN
		check_ldap $ADMIN_DN $password
	fi
	apply_ldifs
	exit 0
}

main
