#!/bin/sh

#Copyright (c) 2013 NetIQ Corporation and its affiliates. All Rights Reserved. 

# For tarball builds of eDirectory the variable eDirectory_BUILD_LOCATION can be filled in with
# location of eDirectory. Example: eDirectory_BUILD_LOCATION="/home/user1/eDirectory"

path=$1
for i in "$@"
do
j="\"$i\""
command="$command $j"
done

eDirectory_BUILD_LOCATION=""

if [ -d $path ] ; then
	eDirectory_BUILD_LOCATION=$path
fi

version=""
product_name="NetIQ eDirectory"

if [ -n "$eDirectory_BUILD_LOCATION" ] ; then
	if [ ! -d "$eDirectory_BUILD_LOCATION" ] ; then
		echo "The specified eDirectory build location in ndspath is wrong. Enter the correct location."
		return 1
	fi
fi


# Determining OS and setting awk path
ifSun=`uname  -a|grep -c SunOS`
OS=`uname`
                                                                                                                             
if [ $ifSun -ge 1 ] ; then
	AWK=/usr/xpg4/bin/awk
else
	AWK=/bin/awk
fi

script_path=$0
run_command=0
cmd_line="$@"

if [ -n "$DEBUG" ] ; then
	echo "cmd_line=$cmd_line"
	echo "First argument=$0"
	echo "Second argument=$1"
fi

print_usage()
{
	str=`gettext install "Usage:"`
	#echo "Usage:"
	echo $str
	#str=`gettext ndspath "To export paths in the current shell:"`

	if [ $OS = "AIX" -o $OS = "HP-UX" ] ; then
		str=`gettext install "To export paths in the current shell(Use bash --posix):(for custom location please go to a directory under opt)"`
		echo $str
		echo "    . $0"
		#echo "    $str"
	else 
		str=`gettext install "To export paths in the current shell:(for custom location please go to a directory under opt)"`
		echo $str
		echo "    . $0"
		#echo "    $str"
	fi

	echo ""
	str=`gettext install "To execute an eDirectory utility by prefixing ndspath:"`
	#echo "To execute an eDirectory utility by prefixing ndspath:" 
	echo $str
	str=`gettext install "<eDirectory utility with its commandline arguments>"`
	echo "    $0 $str"
	echo ""
	str=`gettext install "To print this help:"`
	echo $str
	#echo "To print this help:"
	echo "    ndspath [-h|--help]"
	exit 1
}

exec_command()
{
	if [[ ! -d $1 ]] ; then
		eval $command
	fi
}

isOESServer()
{
	if [ -f /etc/novell-release ]; then
		isOES=`grep -Fi -m 1 "Open Enterprise Server" /etc/novell-release`
		if [[ $isOES == *"Open Enterprise Server"* ]];
		then
			return 0
		fi
	fi
	return 1
}

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
	print_usage $0
fi

if [ "$1" = "-v" -o "$1" = "--ver" -o "$1" = "--version" ] ; then

        str=`gettext nds "Path setting utility for eDirectory Server "`
        binDir=`dirname $0`
        if [ -x ${binDir}/ndsconfig ]; then
                version=`${binDir}/ndsconfig --version 2> /dev/null | awk '{print substr($0, index($0, "NetIQ"))}'`
        else
                version=${product_name}
        fi
	echo $str $version
	exit 0
fi

# Library paths are different for 32 bit Linux and 64 bit Linux
# Default is lib. This will be modified by Linux.mak, SunOS.mak etc
# to reflect actual library path. Ex: This will be changed by
# Linux.mak to lib64 when NDSbase is built.
libDir=lib64

firstchar=`echo $0 | sed -n "s/^\(.\).*$/\1/p"`
if [ "$firstchar" != "-" ] ; then
	basescript=`basename $0`
fi

if [ -n "$DEBUG" ] ; then
	echo "basescript=$basescript"
fi

if [ "$basescript" = "ndspath" -a "$1" = "" ] ; then
	print_usage $0
else
	run_command=1
fi

path=`echo $0 | $AWK -F"/" '{print $1}'`

if [ -n "$DEBUG" ] ; then
	echo "path before first slash=$path"
fi

if [ "$path" != "" ] ; then
	script_path="`pwd`/$0"
fi

if [ -n "$DEBUG" ] ; then
	echo "script_path=$script_path"
fi

bin_dir=`echo "$script_path" |$AWK -F"/" '{for(i=2; i<NF; i++) printf "/%s", $i;} END{printf "\n"}'`

if [ -n "$DEBUG" ] ; then
	echo "bin_dir=$bin_dir"
fi

base_dir=`echo $bin_dir|$AWK -F"/opt" '{for(i=1;i<NF;i++)if(i>1)printf("/opt%s",$i);else printf("%s", $i);}'`

if [ -n "$eDirectory_BUILD_LOCATION" ] ; then
	base_dir="$eDirectory_BUILD_LOCATION"
fi

if [ -n "$DEBUG" ] ; then
	echo "base_dir=$base_dir"
fi

if [ ! -z `tty | grep dev` ]; then
str=` gettext install "Setting eDirectory binary path to $base_dir/opt/novell/eDirectory/bin ..."`
echo ""
echo "$str"
echo ""
fi

if [ "$base_dir" = "" ] ; then
	PATH=/opt/novell/eDirectory/bin:/opt/novell/eDirectory/sbin:$PATH
	export PATH
	if [ $OS = "AIX" ] ; then
		export LIBPATH=/opt/novell/eDirectory/lib:/opt/novell/eDirectory/lib/nds-modules:/opt/novell/lib:$LIBPATH
	fi
	if [ $OS = "HP-UX" ] ; then
		export SHLIB_PATH=/opt/novell/eDirectory/lib:/opt/novell/eDirectory/lib/nds-modules:/opt/novell/lib:$SHLIB_PATH
	fi
else
	PATH=$base_dir/opt/novell/eDirectory/bin:$base_dir/opt/novell/eDirectory/sbin:/opt/novell/eDirectory/bin:$PATH
	export PATH
	if [ $OS = "AIX" ] ; then
		export LIBPATH=$base_dir/opt/novell/eDirectory/lib:$base_dir/opt/novell/eDirectory/lib/nds-modules:$base_dir/opt/novell/lib:/opt/novell/lib:$LIBPATH
	fi

	if [ $OS = "HP-UX" ] ; then
		export SHLIB_PATH=$base_dir/opt/novell/eDirectory/lib:$base_dir/opt/novell/eDirectory/lib/nds-modules:$base_dir/opt/novell/lib:/opt/novell/lib:/opt/novell/eDirectory/lib:$SHLIB_PATH
	fi
fi 
                                                                                                                             
if [ $OS = "Linux" ] ; then
	if isOESServer; then
                export LD_LIBRARY_PATH=/usr/lib64:$base_dir/opt/novell/eDirectory/lib64:$base_dir/opt/novell/eDirectory/lib64/nds-modules:$base_dir/opt/novell/lib64:$LD_LIBRARY_PATH
	else
		export LD_LIBRARY_PATH=$base_dir/opt/novell/eDirectory/lib64:$base_dir/opt/novell/eDirectory/lib64/nds-modules:$base_dir/opt/novell/lib64:$LD_LIBRARY_PATH
	fi
	export SHLIB_PATH=$base_dir/opt/novell/eDirectory/lib:$base_dir/opt/novell/eDirectory/lib/nds-modules:$base_dir/opt/novell/lib:/opt/novell/lib:/opt/novell/eDirectory/lib:$SHLIB_PATH
	export MANPATH=$base_dir/opt/novell/man:$base_dir/opt/novell/eDirectory/man:$MANPATH:/usr/share/man:/usr/local/man:/usr/X11R6/man:/opt/gnome/share/man:/usr/man
else
	MANPATH=$base_dir/opt/novell/man:$MANPATH:/opt/SUNWspro/man:/usr/man:/usr/share/man:/usr/local/man
	export MANPATH
fi

TEXTDOMAINDIR=$base_dir/opt/novell/eDirectory/share/locale
export TEXTDOMAINDIR

if [ $run_command -eq 1 ] ; then
	exec_command "$cmd_line" 
fi
