#! /bin/bash
# start/stop script for Mac OS X VMware Fusion VM
# $Id: mk $
#
# ==> Usage()
#
# date         who  vers   text
# 2011-Mar-18  pmo  1.1	   added: check ssh connection to database
# 2011-Jan-17  pmo  1.0    $prog_cmd introduced

  version="1.0"
copyright="(c..2009..2011, censhare AG, Munich"
 suppmail="technik@censhare.de"
 progfile="$0"
 prog_cmd="${0##*/}"; 
 progname="${prog_cmd%%.*}"
  progdir="${0%/*}"

Error ()   { echo -e >&2 "$*"; exit 1; }
Warning () { echo -e >&2 "$*"; }
Hint ()    { echo -e     "$*"; }

#################### Environment #################

# check for root user
test "$UID" -ne 0 && Error "$progname: You must be root.";

ec=0                                                    # default: success
CONFDIRS="/etc/s2s /etc" 								# where to search for sysconfig directories
cfglist=""
CONFIGS=""

# read configuration file
ConfErr () { echo >&2 "$progname: $*"; exit 6; }
ChkCfgfile () {						# try /etc/sysconfig, /etc/s2s/sysconfig, in reverse order 
    local cf="$1"; test -z "$cf" && ConfErr "$FUNCNAME(): Filename missing."
    for confdir in $CONFDIRS
    do
        CONFIG_FILE=$confdir/sysconfig/$cf
        cfglist="$cfglist $CONFIG_FILE"
        test -r "$CONFIG_FILE" && return 0
    done
    return 1						# failed
}

# test configuration file
test -z "$CONFIG_FILE" && {				# if no config file defined by environment, look for
    ChkCfgfile $progname.$HOSTNAME || {			#     host specific one or
	ChkCfgfile $progname || {			#     general one or error
	    ConfErr "Configuration missing, searched: '${cfglist# }'."; }; }
}							# endif
test -s "$CONFIG_FILE" && . $CONFIG_FILE		# read config

#################### Script parameter #################

PID_FILE="/var/run/vmfusion/${VMFUSION_FILE%%.vmx}.pid"
VMFUSION_HOST="database"
STOP_TIMEOUT="60"

#################### And Action ;-) #################

check_running()
{
    "$VMFUSION_VMRUN" list | grep "$VMFUSION_PATH"/"$VMFUSION_FILE" > /dev/null || return 1
	return 0
}

case "$1" in
    start)
   		echo -n "    Starting $progname: "
		check_running && {
			Hint "running"; exit 0
		}
 		su - $VMFUSION_USER -c "\"$VMFUSION_VMRUN\" start \"$VMFUSION_PATH/$VMFUSION_FILE\" nogui" &>/dev/null && {
			check_running && Hint "started"; exit 0
		} || {
			Error "error"
		}
		;;
		
   stop)
    	echo -n "    Shutting down $progname: "
		# check ssh connection to database
		ssh -o StrictHostKeyChecking=no -o BatchMode=yes root@"$VMFUSION_HOST" pwd >&/dev/null || {
	    	Error "cannot connect to host '$VMFUSION_HOST' via ssh without user interaction. Check manually 'ssh root@$VMFUSION_HOST pwd'."
		}
    	ssh root@"$VMFUSION_HOST" -o StrictHostKeyChecking=no -o BatchMode=yes "init 0" && { 
        	while check_running && test $STOP_TIMEOUT -gt 0; do
            	echo -n "."
            	sleep 1
            	let TIMEOUT=$TIMEOUT-1
        	done && { 
        	    Hint "stopped"
        	} || Error "error"
        }
		;;
		
   restart)
		# Stop the service and regardless of whether it was
		# running or not, start it again.
		$0 stop; $0 start;
		;;
		
   status)
		echo -n "    Checking for $progname: "
		check_running && {
			Hint "running"
		} || Hint "unused"
		;;
		
   *)
		echo "Usage: $0 {start|stop|restart|status}"
		ec=1
		;;
esac
exit $ec
