#!/bin/sh
#
# Copyright (c) 1996, by Sun Microsystems, Inc.
#
# "@(#)ab2mgr	1.34 99/12/09 SMI"

#
# Startup/Stop for AnswerBook2 server
#

AB2UsrDir=/usr


# Check if AnswerBook2 has this feature disable
config_file=$AB2UsrDir/lib/ab2/dweb/data/config/ab2_init.template
if [ -f $config_file ]; 
then
	status=`/usr/bin/grep  AdminAutoRestart $config_file | /usr/bin/awk '{print $3}'`
	if [ ! -z "$status" ];
	then
	    if (test $status = "0")
	    then
		exit 0
	    fi 
	fi
fi


# temporary workaround for dynaweb lib's dependency on LD_LIBRARY_PATH
if [ ! "$LD_LIBRARY_PATH" ]; then
    LD_LIBP=$AB2UsrDir/lib/ab2/lib:$AB2UsrDir/lib/ab2/dweb/sunos5/lib:/usr/dt/lib:/usr/openwin/lib:/usr/lib
else
    LD_LIBP=$AB2UsrDir/lib/ab2/lib:$AB2UsrDir/lib/ab2/dweb/sunos5/lib:$LD_LIBRARY_PATH
fi
export LD_LIBP

# DWeb wants a magic env var, EBT_REGISTRY
ER=$AB2UsrDir/lib/ab2/dweb/data/ebtcom.txt
export ER

#if [ ! "$LANG" ]; then
#    LNG=C
#else
#    LNG=$LANG
#fi
LNG=C
export LNG

if [ ! "$LANG" ]; then
    ORGLNG=C
else
    ORGLNG=$LANG
fi
export ORGLNG

#this dir will contain binary
AB2BIN=$AB2UsrDir/lib/ab2/dweb/sunos5/bin
# this dir will contain ./config
AB2CFG=$AB2UsrDir/lib/ab2/dweb/data

pre_load=$AB2UsrDir/lib/ab2/lib/libfmanage.so

case "$1" in
'start')
	if [ -d $AB2CFG ] ; then
		# DynaWeb requires pwd == bin dir, save & restore pwd
		PWD=`pwd`
		cd $AB2BIN
		/usr/bin/su daemon -c "LD_PRELOAD=$pre_load;LANG=$LNG;AB2_ORIG_LANG=$ORGLNG;LD_LIBRARY_PATH=$LD_LIBP;EBT_REGISTRY=$ER;export LD_PRELOAD LD_LIBRARY_PATH LANG AB2_ORIG_LANG LC_ALL EBT_REGISTRY; $AB2BIN/dwhttpd $AB2CFG > /dev/null"
		cd $PWD

		# detect if server started OK
		PID=`/usr/bin/ps -e | /usr/bin/fgrep dwhttpd | /usr/bin/awk '{print $1}'`
		if [ -z "$PID" ] ;
		then
		    # server not running
		    echo "AnswerBook2 server startup failed."	
		    
		    # check to see if user daemon is defined in the system
		    USER=`/usr/bin/grep daemon /etc/passwd`
		    if [ -z "$USER" ] ;
		    then
			echo "User daemon is not defined on this system."
		    fi  
		fi
	fi
	;;
'stop')
	PID=`/usr/bin/ps -e|/usr/bin/fgrep dwhttpd|/usr/bin/awk '{print $1}'`
	if [ ! -z "$PID" ] ;  then
		/usr/bin/kill ${PID} 1>/dev/null 2>&1
	fi
	;;
*)
	echo "Usage: /etc/init.d/ab2mgr { start | stop }"
	;;
esac
exit 0
