#!/bin/bash
# services	This is the init script to setup services
# chkconfig: - 85 15
# description: Starts and stops the services.

LANG=en_US
LC_TIME=en_US
LC_ALL=en_US
LC_MESSAGES=en_US
LC_NUMERIC=en_US
LC_MONETARY=en_US
LC_COLLATE=en_US
export LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE

random_seed=/var/lib/random-seed

killproc() {
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		killlevel="-TERM"
	fi

	# Find pid.
	pid=`pidof "$1"`

	# Kill it.
	if [ -n "${pid:-}" ] ; then
		kill $killlevel $pid
	fi
}

httpd_moduleargs() {
	moduledir=/usr/lib/apache
	moduleargs=`
	/usr/bin/find ${moduledir} -type f -perm -0100 -name "*.so" | env -i tr '[:lower:]' '[:upper:]' | awk '{\
	gsub(/.*\//,"");\
	gsub(/^MOD_/,"");\
	gsub(/^LIB/,"");\
	gsub(/\.SO$/,"");\
	print "-DHAVE_" $0}'`
	echo ${moduleargs}
}


start() {
	echo -n $"Starting services:"
	console=`/usr/bin/tty`

	##
	## random
	##
	echo -n " random" > $console
	if [ -f $random_seed ]; then
		cat $random_seed >/dev/urandom
	else
		touch $random_seed
	fi
	chmod 600 $random_seed
	dd if=/dev/urandom of=$random_seed count=1 bs=512 2>/dev/null

	##
	## ntpd
	##
	echo -n " ntpd" > $console
	[ -f /etc/sysconfig/ntpd ] && . /etc/sysconfig/ntpd
	OPTIONS="$OPTIONS -g"
	/usr/sbin/ntpd $OPTIONS &

	##
	## sshd
	##
	echo -n " sshd" > $console
	/usr/sbin/sshd &

	##
	## xinetd
	##
	echo -n " xinetd" > $console
	/usr/sbin/xinetd -stayalive -reuse -pidfile /var/run/xinetd.pid &

	##
	## httpd
	##
	echo -n " http" > $console
	/usr/sbin/httpd `httpd_moduleargs` &

	##
	## postgresql
	##
	echo -n " postgresql" > $console
	PGDATA=/var/lib/pgsql/data
	su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl -D $PGDATA -p /usr/bin/postmaster start > /dev/null 2>&1" < /dev/null &

	##
	## FreeWnn
	##
	echo -n " FreeWnn" > $console
	rm -f /tmp/jd_sockV4
	/usr/bin/jserver > /dev/null &

	##
	## canna
	##
	echo -n " canna" > $console
        rm -f /tmp/.iroha_unix/IROHA
	/usr/sbin/cannaserver -syslog -u bin -inet &

	##
	## crond
	##
	echo -n " crond" > $console
	/usr/sbin/crond &

	##
	## xfs
	##
	echo -n " xfs" > $console
	rm -fr /tmp/.font-unix
	/usr/X11R6/bin/xfs -droppriv &

	##
	## anacron
	##
	echo -n " anacron" > $console
	/usr/sbin/anacron &

	##
	## atd
	##
	echo -n " atd" > $console
	/usr/sbin/atd &

	touch /var/lock/subsys/services
	echo " Done" > $console
}

stop() {
	echo -n $"Stopping"

	echo -n " atd"
	killproc /usr/sbin/atd

	echo -n " anacron"
	killproc /usr/sbin/anacron

	echo -n " xfs"
	killproc /usr/X11R6/bin/xfs

	echo -n " crond"
	killproc /usr/sbin/crond

	echo -n " canna"
	/usr/sbin/cannakill
	rm -f /tmp/.iroha_unix/IROHA

	echo -n " FreeWnn"
	killproc /usr/bin/jserver
	rm -f /tmp/jd_sockV4

	echo -n " postgresql"
	PGDATA=/var/lib/pgsql/data
	su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl stop -D $PGDATA -s -m fast" > /dev/null 2>&1 &

	echo -n " http"
	test -f /var/run/httpd.pid && kill `cat /var/run/httpd.pid`
	rm -f /var/run/httpd.pid

	echo -n " xinetd"
	test -f /var/run/xinetd.pid && kill `cat /var/run/xinetd.pid`
	rm -f /var/run/xinetd.pid

	echo -n " sshd"
	killproc /usr/sbin/sshd

	echo -n " ntpd"
	killproc /usr/sbin/ntpd

	echo -n " random"
	touch $random_seed
	chmod 600 $random_seed
	dd if=/dev/urandom of=$random_seed count=1 bs=512 2>/dev/null

	echo

	echo -n $"Waiting"
	loop=30
	while test "$loop" -gt 0; do
		run=`pidof /usr/bin/postmaster`
		if [ -z "$run" ]; then
			break
		fi
		echo -n "."
		usleep 200000
		loop=`expr $loop - 1`
	done
	if [ "$loop" -eq 0 ]; then
	    echo "Some processes could not stop."
	else
	    echo ".Done"
	fi
	rm -f /var/lock/subsys/services
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  *)
	echo $"Usage: $prog {start|stop|restart}"
	exit 1
esac
exit 0
