#!/bin/sh

_KILL_DIRS="rc0.d rcS.d rc1.d"
_START_DIRS="rc2.d rc3.d rc5.d"
_BASE_DIRS="/etc /etc/init.d /etc/rc.d"


#
# Find server pid from the process tree
#
server_pid()
{
	id=`$id | $awk -F\( '{print $1;}' | $awk -F= '{print $2;}'`
	$ps -u $id -l | $grep $sd_script | $cut -c1-24 | $awk '{print $4 " " $5}' |
  	while read pid ppid; do
    	parent_ps=`$ps -p $ppid | $grep $sd_script`
    	if [ -z "$parent_ps" ]; then
			echo "$pid"
      		break
    	fi
  	done
}

get_server_pid()
{
	if [ -x /usr/bin/pgrep ]
	then
		/usr/bin/pgrep -u 0 -P 1 -x $sd_script
	else
		server_pid
	fi
}

stop_daemon()
{
       	# explicit kill with a signal, does not shutdown the clients
	SIGNAL=-15
	if [ -x /usr/bin/pkill ]; then
		id=`$id | $awk -F\( '{print $1;}' | $awk -F= '{print $2;}'`
		/usr/bin/pkill $SIGNAL -U $id -x uno
	else
		sdaemon_pid=`server_pid`
		if [ "$sdaemon_pid" != "" ] ; then
      			$kill $SIGNAL $sdaemon_pid
       		fi
        fi
	echo $sd_script" stopped."
}

env_daemon()
{
    # set search path for shared libraries
    case $sd_platform in
    SunOS)
	LD_LIBRARY_PATH=$sd_inst/program:$sd_inst/lib:$sd_inst/lib/components:.:$LD_LIBRARY_PATH
#	echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
	export LD_LIBRARY_PATH
	;;
    IRIX*)
	LD_LIBRARYN32_PATH=$sd_inst/program:$sd_inst/lib:$sd_inst/lib/components:.:$LD_LIBRARYN32_PATH
	export LD_LIBRARYN32_PATH
	;;
      *)
	LD_LIBRARY_PATH=$sd_inst/program:$sd_inst/lib:$sd_inst/lib/components:.:$LD_LIBRARY_PATH
	export LD_LIBRARY_PATH
	;;
    esac
}

run_daemon()
{
    env_daemon
    cd $sd_bindir
    echo "starting UNO communication with uno:socket,host=$host,port=$port;urp;SetupServer"
    exec $sd_bin -c com.sun.star.comp.setup.Setup -l $sd_libset -r ${sd_bindir}/applicat.rdb -r ${sd_bindir}/myreg.rdb -u "uno:socket,host=$host,port=$port;urp;SetupServer"
}

start_daemon()
{
    env_daemon
    cd $sd_bindir
    echo "starting UNO communication with uno:socket,host=$host,port=$port;urp;SetupServer"
    exec $sd_bin -c com.sun.star.comp.setup.Setup -l ${sd_libset} -r ${sd_bindir}/applicat.rdb -r ${sd_bindir}/myreg.rdb -u "uno:socket,host=$host,port=$port;urp;SetupServer" 2>&1 > /dev/null &
}



#
# check the root configuration directory for init files
# usually /etc/init.d or /etc/rc.d, see _base_dirs for list
#
get_etcbasedir()
{
	for _DIR in ${_BASE_DIRS} ; do
		if [ -d ${_DIR}/rc0.d ]; then
			_BASE_DIR=${_DIR}
			break
		fi
	done

	_INIT_BASE_DIR=${_BASE_DIR}
	for _DIR in ${_BASE_DIRS} ; do
		if [ -d ${_DIR}/init.d ]; then
			_INIT_BASE_DIR=${_DIR}
			break
		fi
	done
}

install_scripts()
{

	# link the scripts from rc?.d/script to the init.d version
	_CURRENT_WORKING_DIRECTORY=`pwd`
	for _DIR in  ${_INSTALL_DIRS} ; do
		if [ -d ${_BASE_DIR}/${_DIR} ]; then
			cd ${_BASE_DIR}/${_DIR}
			echo "${_BASE_DIR}/${_DIR}" ":" linking ${_LINK_SOURCE} as ${_TARGET_NAME}
			$ln "${_LINK_SOURCE}" "${_TARGET_NAME}"
		fi
	done
	cd "${_CURRENT_WORKING_DIRECTORY}"
}

uninstall_scripts()
{
	if [ -d ${_INIT_BASE_DIR}/init.d ]; then
		_SOURCE=${_INIT_BASE_DIR}/init.d/${SCRIPT}
	else
		_SOURCE=${_INIT_BASE_DIR}/${SCRIPT}
	fi

	if [ -f ${_SOURCE} -o -h ${_SOURCE} ]; then
		echo removing ${_SOURCE}
		$rm ${_SOURCE}
	fi

	for _DIR in  ${_INSTALL_DIRS} ; do
		_TARGET=${_BASE_DIR}/${_DIR}/${_TARGET_NAME}
		if [ -f ${_TARGET} -o -h ${_TARGET} ]; then
			echo removing ${_TARGET}
			$rm ${_TARGET}
			if [ -f ${_TARGET} ]; then
				echo "    " FAILED
			fi
		fi
	done
}

install_daemon()
{
	# check the root configuration directory for init files
	get_etcbasedir

	if [ -d ${_INIT_BASE_DIR}/init.d ]; then
		_INIT_SOURCE="${_INIT_BASE_DIR}/init.d/${sd_script}"
		_LINK_SOURCE="../init.d/${sd_script}"
	else
		_INIT_SOURCE="${_INIT_BASE_DIR}/${sd_script}"
		_LINK_SOURCE="../${sd_script}"
	fi

	if [ ${_INIT_BASE_DIR} != ${_BASE_DIR} ]; then
		_LINK_SOURCE="${_COPY_SOURCE}"
	fi
	_SOURCE=${sd_bindir}/${sd_script}

	# link the script to xxx/init.d
	echo installing ${_SOURCE} as ${_INIT_SOURCE}
	$ln -sf "${_SOURCE}" "${_INIT_SOURCE}"

	# install Kill scripts
	_INSTALL_DIRS=${_KILL_DIRS}
	_TARGET_NAME=${KILLS}
	install_scripts

	# install Start scripts
	_INSTALL_DIRS=${_START_DIRS}
	_TARGET_NAME=${STARTS}
	install_scripts

}

uninstall_daemon()
{
	# check the root configuration directory for init files
	get_etcbasedir

	if [ -d ${_INIT_BASE_DIR}/init.d ]; then
		_INIT_SOURCE="${_INIT_BASE_DIR}/init.d/${sd_script}"
		_LINK_SOURCE="../init.d/${sd_script}"
	else
		_INIT_SOURCE="${_INIT_BASE_DIR}/${sd_script}"
		_LINK_SOURCE="../${sd_script}"
	fi

	if [ ${_INIT_BASE_DIR} != ${_BASE_DIR} ]; then
		_LINK_SOURCE="${_COPY_SOURCE}"
	fi

	# link the script to xxx/init.d/daemon.sh
	echo uninstalling ${_INIT_SOURCE}
	$rm "${_INIT_SOURCE}"

	# uninstall Kill scripts
	_INSTALL_DIRS=${_KILL_DIRS}
	_TARGET_NAME=${KILLS}
	uninstall_scripts

	# uninstall Start scripts
	_INSTALL_DIRS=${_START_DIRS}
	_TARGET_NAME=${STARTS}
	uninstall_scripts

}

find_prog()
{
	if [ -x /usr/bin/${PROG} ]; then
		eval ${PROG}=/usr/bin/${PROG}
	else
		eval ${PROG}=/bin/${PROG}
	fi
}

# try to find some of the programs needed to perform properly
 _PROGRAMS="cut awk grep cp ln basename dirname rm ps kill id"
for PROG in ${_PROGRAMS} ;  do
    find_prog
done


# resolve installation directory
sd_platform=`uname -s`
case $sd_platform in
	SCO_SV) test=/bin/test     ;;
	FreeBSD) test=/bin/test    ;;
  	*)      test=/usr/bin/test ;;
esac
sd_cwd="`pwd`"

if $test -L "$0" ; then
	sd_basename=`$basename $0`
 	sd_script=`ls -l $0 | sed "s/.*${sd_basename} -> //g"`
else
	sd_script="$0"
fi

cd "`$dirname "$sd_script"`"
sd_bindir=`pwd`
cd ..
sd_script=`$basename $sd_script`
sd_bin=$sd_bindir"/uno"
sd_inst=`pwd`
sd_libset=`ls $sd_bindir/libset?????.so`
sd_rc=$sd_inst/etc/$sd_script"rc"

if [ !  -f $sd_rc ]; then
    echo "$sd_rc"": not found"
    sd_rc="/etc/$sd_script""rc"
fi

port=`grep port= $sd_rc | awk -F= '{print $2;}'`
host=0

STARTS=S99${sd_script}
KILLS=K10${sd_script}

mode="$1"

# main dispatch routine
case "$mode" in
'start')
	shift;
	start_daemon $*
	;;
'run')
	shift;
	run_daemon $*
	;;
'stop')
	stop_daemon
	;;
'install')
	install_daemon
	;;
'uninstall')
	uninstall_daemon
	;;
*)
	run_daemon $*
	exit 1
	;;
esac

# well done
exit 0
