#!/bin/bash
#
# Author: Jochen Schäfer <jschaef@novell.com>, 2001-2014
#         Robert Landenhamer <rla@campuslan.de>, 2016
# 
# copyright (c) Novell Deutschland GmbH, 2014. All rights reserved.
# GNU Public License
#
# cl-lib.sh	28 Apr 2016

###################################################################
# get_school: Gib die Schule aus
# get_area: Gibt den Bereich aus
# get_tree: Gibt den Tree aus
# get_org: Gibt die Organisation aus
###################################################################

VER=1.1
LOGDIR=/var/adm/autoinstall/logs
mkdir -p $LOGDIR
PROD=create-nss
LOGFILE=$PROD.$(date +%Y.%m.%d).log
LOG=$LOGDIR/$LOGFILE
EDIR_CONF=/etc/opt/novell/eDirectory/conf/nds.conf
test -f $EDIR_CONF || (echo -ne "\nThis is not an eDirectory server: better to exit here\n"; exit 1) || \
		(echo -ne "\n$0 not able to run on non eDirectory Server; better to exit now !!!\n" >>$LOG; exit 1) || exit 1
TREE=$(ldapsearch -x -ZZ -LLL -s base -b "" directoryTreeName |awk -F":" 'gsub(" ",""){print $2}')
NCIF_VAR_FILE=/root/install/variables.txt
NLVM_BIN=/sbin/nlvm
FAILED_TRUSTEES=""


function get_school()
{
	#Server context, e.g. OU=server.OU=<my_schule>.OU=<my_bereich.O=<my_company>
	EDIR_CTX=$(awk -F'=' '$1=="n4u.nds.server-context" {print substr($0, index($0,$2))}' $EDIR_CONF)
	#Derive school identifier out of server context
	#operation before '|' returns OU=<SCHULE>.OU=<:company,OU=companies.O=<my_company>
	#operation after '|'  returns <SCHULE>
	local SCHULE=$(awk -F"." '{print substr($0,index($0,$2))}' <<<$EDIR_CTX |awk -F"." '{print substr($1,index($1,"=")+1)}')
	echo "$SCHULE"
}


function get_area()
{
	#Server context, e.g. OU=server.OU=<my_schule>.OU=<company>.OU=companies.O=<my_company>
	EDIR_CTX=$(awk -F'=' '$1=="n4u.nds.server-context" {print substr($0, index($0,$2))}' $EDIR_CONF)
	#Derive area identifier out of server context
	#operation before '|' returns OU=<location>.OU=<company,OU=companies.O=<my_company>
	#operation after '|'  returns <location>
	local BEREICH=$(awk -F"." '{print substr($0,index($0,$3))}' <<<$EDIR_CTX |awk -F"." '{print substr($1,index($1,"=")+1)}')
	echo "$BEREICH"
}


function get_tree()
{
	local dn=$1
	local passwd=$2
	local LDAP_OPTS="-x -ZZ -D $dn -w $passwd -LLL" 
	local search_attr=directoryTreeName
	local tree=$(ldapsearch $LDAP_OPTS -s base -b "" $search_attr |awk  -F': ' '$1~"TreeName" {print $2} ')
	
	if [ $? -ne 0 ];then
		echo -ne "\n!!! LDAP Connect with user $dn failed, exiting here!!!\n"
		exit 9
	else
		echo $tree
	fi	
}	


function get_org()
{
	 grep n4u.nds.server-context  /etc/opt/novell/eDirectory/conf/nds.conf | awk -F"=" '{print $NF}'	
}



