#!/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-config.sh				28 Apr 2016
# modified: RLa
#
##
# 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 1
	}	
	test $UID -eq $1 || root_reqerror
}


function usage()
{
        cat <<HERE

usage $0
	--debug			# DEBUG Mode
        example: [--help] [--debug]
HERE
        exit 1
}


function getopt()
{
        while [ $# -gt 0 ];do
                case $1 in --debug) debug=1;debug;;
						   --help) help=2;usage;;
                           *) echo "unknown parameter provided"; 
				usage;;
                esac
                shift
        done
}


function set_vars()
{
	LIB_VALUES=vCE.values
}


function set_func_vars()
{
	# vce.config files
	INPUT_FILE1=webapp_template-vCE.config
	OUTPUT_FILE1=vCE.config
	echo "#" >$(eval echo \$OUTPUT_FILE1)
}


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


function check_env()
{
	if [ ! -f $(pwd)/$INPUT_FILE1 ];then
		echo -ne "\n!!! $INPUT_FILE1 does not exist in $(pwd)!!!\n"
		exit 2	
	fi	
}


function apply_values()
{

        # replacing place holders in template file with actual data producing the vCE.config

	echo -ne "Create vCE.config\n"
	
	while IFS='' read -r LINE;
		do
		sed -e "s/%%vceInstallDate%%/$vceInstallDate/g" <<<"$LINE" | \
		sed -e "s/%%vceInstallVersion%%/$vceInstallVersion/g" | \
		sed -e "s/%%vceBaseContext%%/$vceBaseContext/g" | \
		sed -e "s/%%vceDirectoryHost%%/$vceDirectoryHost/g" | \
		sed -e "s/%%vceDirectoryPort%%/$vceDirectoryPort/g" | \
		sed -e "s/%%vceDirectoryServer%%/$vceDirectoryServer/g" | \
		sed -e "s/%%vceDirectoryTree%%/$vceDirectoryTree/g" | \
		sed -e "s/%%vceServerContextDotted%%/$vceServerContextDotted/g" | \
		sed -e "s/%%vceServerContext%%/$vceServerContext/g" | \
		sed -e "s/%%vceCifsUser%%/$vceCifsUser/g" | \
		sed -e "s/%%vceCifsKey%%/$vceCifsKey/g" | \
		sed -e "s/%%vceClientPassword%%/$vceClientPassword/g" | \
		sed -e "s/%%vceRegUserPassword%%/$vceRegUserPassword/g" | \
		sed -e "s/%%vceLanguage%%/$vceLanguage/g" | \
		sed -e "s/%%vncPassword%%/$vncPassword/g" | \
		sed -e "s/%%vceZENDBHost%%/$vceZENDBHost/g" | \
		sed -e "s/%%vceZENDBPort%%/$vceZENDBPort/g" | \
		sed -e "s/%%vceZENDBName%%/$vceZENDBName/g" | \
		sed -e "s/%%vceZENDBAdmin%%/$vceZENDBAdmin/g" | \
		sed -e "s/%%vceZENDBPassword%%/$vceZENDBPassword/g" | \
		sed -e "s/%%vceZENAppHost%%/$vceZENAppHost/g" | \
		sed -e "s/%%vceZENAppHostUser%%/$vceZENAppHostUser/g" | \
		sed -e "s/%%vceZENAppHostPassword%%/$vceZENAppHostPassword/g" | \
		sed -e "s/%%vceZENSchoolBundles%%/$vceZENSchoolBundles/g" | \
		sed -e "s/%%vceZENSystemBundles%%/$vceZENSystemBundles/g" >> $OUTPUT_FILE1
	done < $INPUT_FILE1
	dos2unix $OUTPUT_FILE1
	echo -ne "Done!\n"

}


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

function main()
{
	getopt  $ARGV
	set_vars
	source_lib
	set_func_vars
	check_env
	apply_values
	exit 0
}

main
