#!/bin/bash
#. /etc/rc.status
. /home/mix/rc.status.anon
rc_reset


case "$1" in
  start)
#  $0 status
#  rc_status
#	if [ "$?" -eq 0 ]
#		then
		echo "Starting $PROGRAM_NAME..."
		cd $INSTALL_PATH
		ulimit -HSn 4096
		$EXECUTE_COMMAND $INSTALL_PATH/$PROGRAM $PROGRAM_OPTIONS
		if [ ! "`$0 process`" ]
		then
			rc_failed 1
		fi
#	else
#		echo "$PROGRAM_NAME is already running."
#		rc_failed 5
#	fi
	rc_status -v
	;;
  stop)
	printf "Shutting down $PROGRAM_NAME..."
	ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -TERM 2>/dev/null >/dev/null
	i=1
	while [ $i -le 90 ] && [ "`ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep`" ]; do
		printf "."
		i=$[$i+1]
		if [ $i -eq 45 ]; then
			echo ""
			printf "Shutting down $PROGRAM_NAME, second try..."
			ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -TERM 2>/dev/null >/dev/null
		fi
		sleep 1 
	done
	if [ "`ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep`" ]; then
		ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -9 2>/dev/null >/dev/null
		rc_failed 6
	fi
	true
	rc_status -v
	;;
	reload)
	printf "Reloading $PROGRAM_NAME..."
	ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -HUP 2>/dev/null >/dev/null
	rc_status -v
	;;
  restart)
	$0 stop
	$0 start
	;;
  command)
  	# executes the program with command line arguments
	
	if [ $# -lt 2 ]
	then
		$0
	else
		shift 1
  		$EXECUTE_COMMAND $INSTALL_PATH/$PROGRAM $*
		while [ $# -gt 1 ]
		do
			shift 1
		done
	fi
  	;;
  status)
	if [ ! "`$0 process`" ]
	then
		rc_failed 3
	fi
	rc_status -v
	;;
  process)
	ps auxww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep
	;;
  log)
	cat $LOGFILE
	;;
  version)
	$0 command $VERSION_OPTION | grep -i version
	;;
  cvs)
	DO_UPDATE=0

	if [ $# -gt 1 ]
	then
		if [ "$2" = "-update" ]
		then
			DO_UPDATE=1
			shift 1
		fi
	fi
	cd $INSTALL_PATH
	if [ ! -d cvs ]
	then
		DO_UPDATE=0
		mkdir cvs
	fi
	cd cvs
	if [ -d $CVS_MODULE ]
	then
		if [ $DO_UPDATE -eq 0 ]
		then
			rm -R $CVS_MODULE
		fi
	else
		DO_UPDATE=0
	fi

	if [ $DO_UPDATE -eq 0 ]
	then
		mkdir $CVS_MODULE
		cvs checkout $CVS_DATE  $CVS_MODULE
	else
		cvs update $CVS_MODULE
	fi

	# delete the compiled program if it exists
	cd $CVS_MODULE
	if [ -e $PROGRAM ]
	then
		rm $PROGRAM
	fi
	rc_status -v
	;;
  svn)
	DO_UPDATE=0

	if [ $# -gt 1 ]
	then
		if [ "$2" = "-update" ]
		then
			DO_UPDATE=1
			shift 1
		fi
	fi
	cd $INSTALL_PATH
	if [ ! -d svn ]
	then
		DO_UPDATE=0
		mkdir svn
	fi
	cd svn
	if [ -d $SVN_MODULE ]
	then
		if [ $DO_UPDATE -eq 0 ]
		then
			rm -R $SVN_MODULE
		fi
	else
		DO_UPDATE=0
	fi

	if [ $DO_UPDATE -eq 0 ]
	then
		mkdir $SVN_MODULE
		svn checkout $SVN_MODULE_URL $SVN_MODULE
	else
		svn update $SVN_MODULE
	fi

	# delete the compiled program if it exists
	cd $SVN_MODULE
	if [ -e $PROGRAM ]
	then
		rm $PROGRAM
	fi
	rc_status -v
	;;	
  update)
	OLD_VERSION_OPTION=`$0 version`
  	rc_reset	
	NEW_VERSION_OPTION=`$EXECUTE_COMMAND $INSTALL_PATH/svn/$SVN_MODULE/$PROGRAM $VERSION_OPTION | grep -i version`
	echo "OLD_VERSION_OPTION: $OLD_VERSION_OPTION"
	echo "NEW_VERSION_OPTION: $NEW_VERSION_OPTION"
	rc_status
	if [ "$?" -eq 0 ]
	then
		if [ "$NEW_VERSION_OPTION" \>  "$OLD_VERSION_OPTION" ]
		then
			if [ ! -d $INSTALL_PATH/backup ]
			then
				mkdir $INSTALL_PATH/backup
			fi

			if [ -e $INSTALL_PATH/$PROGRAM ]
			then
				VERSION_COUNT=1
				while [ -e "$INSTALL_PATH/backup/$PROGRAM~$VERSION_COUNT" ]
				do
					VERSION_COUNT=`expr $VERSION_COUNT + 1`
				done
				
				$0 stop
				rc_failed $?
				mv "$INSTALL_PATH/$PROGRAM" "$INSTALL_PATH/backup/$PROGRAM~$VERSION_COUNT"
				if [ -e $LOGFILE ]
				then
					mv "$LOGFILE" "$INSTALL_PATH/backup/$PROGRAM.log~$VERSION_COUNT"
				fi
			fi
			cp $INSTALL_PATH/svn/$SVN_MODULE/$PROGRAM $INSTALL_PATH
		else 
			rc_failed 5
		fi
	fi
	rc_status
	case "$?" in
	0)
		echo "Update complete! Please restart $PROGRAM_NAME ."
	;;
	5)
		echo "The current $PROGRAM_NAME is up-to-date. No update needed."
	;;
	*)
		echo "Update of $PROGRAM_NAME failed!"
	;;
	esac

	rc_status -v
	;;

  compile)
	COMPILE_DIR=""
	if [ $# -gt 2 ]
	then 
		if [ "$2" = "-d" ]
		then
			if [ "$3" = "cvs" ]
			then
				COMPILE_DIR="$INSTALL_PATH/$3/$CVS_MODULE"
			elif [ "$3" = "svn" ]
			then
				COMPILE_DIR="$INSTALL_PATH/$3/$SVN_MODULE"
			else
				COMPILE_DIR="$INSTALL_PATH/$3"
			fi
			
			shift 2
		fi
	fi
	if [ ! "$COMPILE_DIR" ]
	then
		COMPILE_DIR="$INSTALL_PATH/svn/$SVN_MODULE"
	fi
	echo "Compiling in directory '$COMPILE_DIR'!"

	if [ ! -d $COMPILE_DIR ]
	then
		rc_status 1
		echo "Cannot compile - please specify a valid directory!"
	else
		echo "..compiling"	
		cd $COMPILE_DIR
	        if [ "$JAR_CLASSES" ] 
	        then
			# JAVA
                	if [ -d classes ]
	                then
        	                rm -R classes
                	fi
	                mkdir classes
			# go into the source directory
        	        cd src
                	javac -d ../classes $COMPILE_OPTIONS $PROGRAM_NAME.java
	                rc_failed $?
			if [ $? -ne 0 ]
			then
				rc_status -v
				rc_exit
			fi
        	        cd ../classes
                        #rm -r META-INF/	
                	JAR_EXEC=""
	                for JAR in $JAR_CLASSES; do
        	                JAR_EXEC="$JAR_EXEC `jar -xf $JAR`"
                	done
                        rm -r META-INF/	
			# create the manifest file
			echo "Manifest-Version: 1.0" > ../MANIFEST.MF
			echo "Main-Class: $PROGRAM_NAME" | sed s/[/]/./g >> ../MANIFEST.MF
	                jar cvfm $PROGRAM ../MANIFEST.MF * $JAR_EXEC
        	        rc_failed $?
                	mv $PROGRAM ..
			cd ..
        	else
			#C++
	                ./configure $COMPILE_OPTIONS
        	        rc_failed $?
			if [ $? -ne 0 ]
			then
				rc_status -v
				rc_exit
			fi
                	make
	                rc_failed $?
        	fi
	fi
	rc_status -v
	;;
  *)
  	echo ""
  	echo "This script controls an AN.ON service."
	echo ""
	echo "Usage: SERVICENAME {start|stop|restart|cvs|svn|compile [-d {directory}]|update|status|process|log|version|command {commands}}"
	echo ""
	echo "start|stop|restart:	basic controls of the $PROGRAM_NAME"
	echo "cvs [-update]:		fetches a new version from CVS; if -update is specified,"
	echo "			the code is only updated, not fully replaced"
  echo "svn [-update]:		fetches a new version from SVN; if -update is specified,"
	echo "			the code is only updated, not fully replaced"	
	echo "update:			if the program version in the CVS directory is newer than the current version,"
	echo "			this makes a backup of the old version and replaces it; the service s stopped if update was successful;"
	echo "compile [-d {dir}]:	compiles the program in the specified directory;"
	echo "			if no directory is given, the program will be compiled in the default CVS directory"
	echo "status:			checks if the $PROGRAM_NAME is running"
	echo "process:		lists all $PROGRAM_NAME processes"
	echo "log:			returns the $PROGRAM_NAME log"
	echo "version:		returns the $PROGRAM_NAME version"
	echo "command {commands}:	runs the program with the specified command line options"
	echo ""
	echo "Several arguments can be combined, for example if you want to update the service from CVS, try:"
	echo "		SERVICENAME cvs compile update"
	echo ""
	exit 1
	;;
esac

# apply further commands if given
if [ $# -gt 1 ]
then
	shift 1
	# the commands are only applied if everything was ok so far
	rc_status
	if [ $? -eq 0 ]
	then
		$0 $*
	fi
fi

rc_exit
