#!/bin/sh
#
# Startup script for the Lasso Service
#
# Author: Adam Randall adam.randalla@omnipilot.com
# Date: 3/15/2004
#
# chkconfig: 2345 60 50
#
# description: Lasso Professional Server is a web application server.  it is used \
#              to process Lasso format files to bridge the gap between html \
#              and dynamic database content. Now includes an embedded version \
#              of SQLite 3.
# processname: Lasso8Service
# pidfile: /tmp/Lasso8Service.pid
#

lassodir="/usr/local/Lasso Professional 8"

lpid=`ps axc | awk "{ if ( \\$5 == \\"Lasso8Service\\" ) print \\$1 }" | tr '\n' '|' | sed -e 's/^\([0-9]*\).*/\1/'`

if [ `id -u` != 0 ]; then
	echo "This script must be run as the system administrator."
	exit 1
fi

case "$1" in
	start)

		echo -n "Starting Lasso Service: "

		if [ -f /tmp/LassoService8.pid ] || [ "$lpid" ]; then
			echo "Lasso Service is running"
		else

			find . \
				-maxdepth 3 \
				\( -iname "*.so" -or -iname "*.bundle" \) \
				-exec chmod 775 "{}" \; > /dev/null 2>&1

			chmod -R 660 /usr/local/Lasso\ Professional\ 8/SQLiteDBs/* > /dev/null 2>&1
			chmod 770 /usr/local/Lasso\ Professional\ 8/SQLiteDBs > /dev/null 2>&1
			chown -R lasso:admin /usr/local/Lasso\ Professional\ 8/SQLiteDBs > /dev/null 2>&1

			/usr/local/Lasso\ Professional\ 8/LassoAdmin/Lasso8Service.sh 2> /dev/null >> /dev/null &

			ctime=120
			counter=0
			lassoup=0

			while [ $counter -lt $ctime ] && [ ! -f /tmp/LassoService8.pid ];
			do
				sleep 1
				counter=$((counter+1))
				if [ -f /tmp/LassoService8.pid ]; then
					lassoup=1
				fi
			done

			echo "Done"
		fi

		;;

	stop)
		echo -n "Stopping Lasso Service: "

		if [ -f /tmp/LassoService8.pid ] || [ "$lpid" ]; then
			killall Lasso8Service

			dead=0

			while [ $dead = 0 ];
			do
				if [ ! `ps axc | awk "{ if ( \\$5 == \\"Lasso8Service\\" ) print \\$1 }" | tr '\n' '|' | sed -e 's/^\([0-9]*\).*/\1/'` ]; then
					dead=1
				fi
				sleep 1
			done

			rm -f /tmp/LassoService8.out

			echo "Done"
		else
			echo "Lasso Service not running"
		fi

		;;

	status)

		if [ -f /tmp/LassoService8.pid ]; then
			echo "Lasso Service is running"
		elif [ `ps axc | awk "{ if ( \\$5 == \\"Lasso8Service\\" ) print \\$1 }" | tr '\n' '|' | sed -e 's/^\([0-9]*\).*/\1/'` ]; then
			echo "Lasso Service is in the process of starting or stopping"
		else
			echo "Lasso Service is not running"
		fi

		;;

	restart|reload)

		echo -n "Restarting Lasso Service: "
		
		if [ -f /tmp/LassoService8.pid ] || [ "$lpid" ]; then
			killall Lasso8Service
		fi

		dead=0

		while [ $dead = 0 ];
		do
			if [ ! `ps axc | awk "{ if ( \\$5 == \\"Lasso8Service\\" ) print \\$1 }" | tr '\n' '|' | sed -e 's/^\([0-9]*\).*/\1/'` ]; then
				dead=1
			fi
			sleep 1
		done

		find . \
			-maxdepth 3 \
			\( -iname "*.so" -or -iname "*.bundle" \) \
			-exec chmod 775 "{}" \; > /dev/null 2>&1

		chmod -R 660 /usr/local/Lasso\ Professional\ 8/SQLiteDBs/* > /dev/null 2>&1
		chmod 770 /usr/local/Lasso\ Professional\ 8/SQLiteDBs > /dev/null 2>&1
		chown -R lasso:admin /usr/local/Lasso\ Professional\ 8/SQLiteDBs > /dev/null 2>&1

		/usr/local/Lasso\ Professional\ 8/LassoAdmin/Lasso8Service.sh 2> /dev/null >> /dev/null &

		ctime=120
		counter=0
		lassoup=0

		while [ $counter -lt $ctime ] && [ ! -f /tmp/LassoService8.pid ];
		do
			sleep 1
			counter=$((counter+1))
			if [ -f /tmp/LassoService8.pid ]; then
				lassoup=1
			fi
		done

		echo "Done"

		;;
	
	*)
		echo "Usage: {start|stop|restart|status}"
		exit 1
		;;

esac