#!/bin/sh

#
# esdx is used by set_path as a param
#

SOFTNAME=esd
SSL_LIB=libcrypto.so.0.9.6

DEST_DIR=

echo_nonl()
{
	echo "$1" 2>&1 | awk '{printf("%s", $0)}'
}

# $1 msg
# $ default path
ask_input()
{
fin=0
while [ $fin = "0" ]
do
echo_nonl ">> $1 [$2]: "

read ans
case "$ans" in
	/*)
		ans=$ans
		fin=1
		;;
	"")
		ans=$2
		fin=1
		;;
	*)
		echo "   Invalid choice $ans"
		fin=0
		;;
esac
done
}
# $1 question
# $2 default answer
prompt()
{
fin=0
while [ $fin = "0" ]
do
echo_nonl ">> $1 (y/n)? [$2]: "

read ans
case "$ans" in
	[yY]|[yY][eE][sS])
		ans=y
		fin=1
		;;
	[nN]|[nN][oO])
		ans=n
		fin=1
		;;
	"")
		ans=$2
		fin=1
		;;
	*)
		echo "Invalid choice $ans"
		fin=0
		;;
esac
done
}

BYE()
{
	echo "Bye!"
	exit 0
}

setup_es()
{
dir_done=0
while [ $dir_done = "0" ]
do
if [ "$ESDIR" != "" ]; then
	if [ -d $ESDIR ]; then
	echo "   $SOFTNAME has already been installed at $ESDIR"
	prompt "Would you like to uninstall it and reinstall again" "y"
		if [ "$ans" = "y" ]; then
			extract $ESDIR "es" true
			dir_done=1
		else
			BYE
		fi
	fi
else
	ask_input "Please specify the destination directory" "/usr/local/esd"
	DEST_DIR=$ans
	extract $DEST_DIR "es" true
	dir_done=1
fi
done
}

setup_SSL()
{
	prompt "Would you like to install and use SSL" "y"
	if [ "$ans" = "y" ]; then
		show_txt SSL.TXT T
		extract $DEST_DIR/lib "SSL" false
		if [ ! -h $DEST_DIR/lib/libcrypto.so ]; then
			(cd $DEST_DIR/lib && ln -s $SSL_LIB libcrypto.so)
		fi
	fi
}

# $1 target dir
# $2 software package
# $3 prompt or not - true/false
extract()
{
	if [ ! -d $1 ]; then
		if [ $3 = true ]; then
			prompt "Setup will now create directory $1" "y"
			if [ "$ans" = "y" ]; then
				mkdir -p $1
				copy $1 $2
			else
				BYE
			fi
		else
			mkdir -p $1
			copy $1 $2
		fi
	else
		if [ $3 = true ]; then
			echo "   The directory $1 exists."
			prompt "Would you like to delete this directory tree and install there" "n"
			if [ "$ans" = "y" ]; then
				rm -rf $1
				mkdir -p $1
				copy $1 $2
			else
				BYE
			fi
		else
			copy $1 $2
		fi
	fi
}

complete()
{
	echo "Complete!"
}

# $1 start file
# $2 var
# $3 path
set_path()
{
	# set startup file path according to user specified during install
	echo "#!/bin/sh" >/tmp/path.$$
	echo "" >> /tmp/path.$$
	echo "$2=$3" >> /tmp/path.$$
	echo "export $2" >> /tmp/path.$$

	cp $3/bin/$1 /tmp/$1
	
	cat /tmp/path.$$ /tmp/$1 > $3/bin/$1
	
	
	# set other config files path
	echo "options { " 								>/tmp/ns.$$
	echo "directory	\"$3/etc/nsdb\";" 		>>/tmp/ns.$$
	echo "pid-file 	\"$3/run/ns.pid\";"		>>/tmp/ns.$$
	echo "dump-file \"$3/run/named_dump.db\";"  	>>/tmp/ns.$$
	echo "statistics-file \"$3/run/named.stats\";"	>>/tmp/ns.$$
	echo "memstatistics-file \"$3/run/named.memstats\";"  >>/tmp/ns.$$  	
	echo "};"					>>/tmp/ns.$$  
	
	cp $3/etc/ns.conf /tmp/ns.conf								
	cat /tmp/ns.$$ /tmp/ns.conf > $3/etc/ns.conf

	# clean up all tmp files
	rm /tmp/path.$$ /tmp/$1 /tmp/ns.$$ /tmp/ns.conf
}

# copy install_path piece_name
copy()
{
	prompt "Setup will now copy program files into $1" "y"
	if [ "$ans" = "y" ]; then
		echo_nonl "Copying ... "
		untar "$2" "$1"
	else
		BYE
	fi
}

# $1 basename and the dir name, which contain splitted tar files
# $2 dest dir
untar()
{
	if [ -d $1 ]; then
		cat $1/$1.?? | tar -xvzf - -C $2  >>$DEST_DIR/setup.log 2>&1
	else
		echo "error finding the program files"
		BYE
	fi
}

# Check if file $1 and $2 exists.
copy_done()
{
	if [ -f $1 -o -f $2 ]; then
		echo "done"
		complete
	else
		echo "installation has errors?"
	fi
}

# $1 name of file that contains text
# $2 "T" or "F"
show_txt()
{
	if [ -f $1 ]; then
		clear
		cat $1 | more

		if [ "$2" = "T" ]; then
			prompt "Proceed" "y"
			[ "$ans" = "n" ] && BYE
		fi
	fi
}
# $1 dir contains the distribution tar files
check_dist()
{
if [ ! -d $1 ]; then 
	echo "Please change to the directory where \"setup\" is located."
	echo "Then type: \"./setup\"." 
	BYE 		# will exit setup here
fi
}

main()
{
PATH=/usr/ucb:$PATH
export PATH

check_dist es 

show_txt README.TXT T
show_txt LICENSE.TXT T

user=`whoami`
if [ "$user" != "root" ]; then
	echo "   Your USER name ($user) is not root!"
	echo "   You may not have enough permissions to install this software!"

	prompt "Do you wish to continue" "n"
	if [ "$ans" = "n" ]; then
		BYE
	fi
fi

	setup_es
	copy_done $DEST_DIR/sbin/clmd $DEST_DIR/sbin/csd
	set_path "esdx" "ESDIR" $DEST_DIR

	setup_SSL
	copy_done $DEST_DIR/lib/$SSL_LIB $DEST_DIR/lib/$SSL_LIB 

	prompt "Do you wish to view the quick start file" "n"
	if [ "$ans" = "n" ]; then
		CURDIR=`pwd`
		echo "You may view it later in $CURDIR."
		echo "The file name is \"qstart.txt\"".
		BYE
	fi
	show_txt qstart.txt F
}

main
