#!/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_filesystem.sh				30 Mai 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 1
	}	
	test $UID -eq $1 || root_reqerror
}


function usage()
{
        cat <<HERE

usage $0
	--school	 <Schulname>		# Organisations Einheit | Name der Schule
	--area		 <world-object>		# organization unit, i.e world-object
	--org		 <organization>		# name of organization
	--tree		 <eDir Tree>		# eDirectory Tree
	--debug


        example: $0 --school Schule<n> --area world-object --org organization --tree VCETREE [--debug]
HERE
        exit 1
}


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

        if [ $# -lt 5 ];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 --school) 	  SCHOOL=$2;shift;;
                           --area) 	  AREA=$2;shift;;
			   --org) 	  ORG=$2;shift;;
                           --tree) 	  TREE=$2;shift;;
                           --debug) debug=1;debug;;
                           *) echo "unknown parameter provided"; 
				usage;;
                esac
                shift
        done        
}


function set_vars()
{
	LIB_VCE=vCE.values
}


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


function apply_values()
{
    # replacing place holders in template file with actual data producing the vCE.config

	echo -ne "creating directory structure and assigning rights\n"
	

	mkdir -p -v /media/nss/DAT/$AREA
	/sbin/rights -f /media/nss/DAT/$AREA -r all trustee  .ou=Administratoren.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE

	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL -r all trustee  .ou=Administratoren.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE

	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Administratoren
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Administratoren/vCEAdmin
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Administratoren/vCEAdmin/Daten
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Administratoren/vCEAdmin/Vorlagen
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Administratoren/vCEAdmin -r all trustee  .cn=vceAdmin.ou=Administratoren.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE

	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Dateipakete
	
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/DeletedHomes
	
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Klassen
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Klassen -r rwcemf trustee  .ou=Lehrer.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE
	
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Kurse
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Kurse -r rwcemf trustee  .ou=Lehrer.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE
		
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Lehrer
	
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Projekte
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Projekte -r rwcemf trustee  .ou=Lehrer.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE
	
	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Pruefungen

	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Schueler
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Schueler -r rwcemf trustee  .ou=Lehrer.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE

	mkdir -p -v /media/nss/DAT/$AREA/$SCHOOL/Vorlesungen
	/sbin/rights -f /media/nss/DAT/$AREA/$SCHOOL/Vorlesungen -r rwcemf trustee  .ou=Lehrer.ou=$SCHOOL.ou=$AREA.o=$ORG.t=$TREE

	
	echo -ne "Done!\n"
}


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

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

main

