#! /bin/bash
#######################################################################
# Copyright 2012 VMware, Inc.  All rights reserved.                   #
#######################################################################

# Get the fully qualified directory name where this script is located
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

#Verify that the setups script has been executes.
if [ ! -f ${DIR}/../conf/sva.jks ]
    then
        echo "You must run \"${DIR}/setup\" script first."
        exit 1
fi

#wrapper-script location
WRAPPER_SCRIPT="$DIR/wrapper-script"

cleanupPSVA() {

        # Check if the service is running. If it does, we need to stop it.
        $WRAPPER_SCRIPT status | grep -i "is running" > /dev/null
        if [ $? == 0 ]
            then
                $WRAPPER_SCRIPT stop
        fi

        # Verify that the service is not running.
        $WRAPPER_SCRIPT status | grep -i "not running" > /dev/null
        if [ $? != 0 ]
            then
                echo "Failed to stop the service. Try to run as sudo."
                return 1
        fi

        pushd `pwd` > /dev/null
        cd ${DIR}

        # Set pseudosva.destroy to 'true'
        ../jre/bin/java -cp ../lib/pseudosvadomain.jar -Dcommand=cleanup -DconfigXml=../conf/vmconfig-sva.xml com.vmware.sva.common.util.PseudoSvaUtility
        cleanupExitCode=$?

        if [ $cleanupExitCode != 0 ]
            then
                echo "The cleanup failed."
                popd > /dev/null
                return 1
        fi

        # Start the service. It will clean itself up.
        echo " "
        $WRAPPER_SCRIPT start

        # Verify cleanup, i.e. verify that pseudosva.destroy is back to 'false'
        ../jre/bin/java -cp ../lib/pseudosvadomain.jar -Dcommand=verify-cleanup -Dtimeout=60 -DconfigXml=../conf/vmconfig-sva.xml com.vmware.sva.common.util.PseudoSvaUtility
        cleanupExitCode=$?

        if [ $cleanupExitCode != 0 ]
            then
                echo "The cleanup verification failed."
                popd > /dev/null
                return 1
        fi

        # Sleep for 10 seconds
        echo "Sleeping for 10 seconds..."
        sleep 10s

        # Stop the service
        $WRAPPER_SCRIPT stop

        if [ $? != 0 ]
            then
                echo "Could not stop the service."
                popd > /dev/null
                return 1
        fi

        popd > /dev/null
        echo " "
        echo "The cleanup completed successfully."
}

errorcode=0

case "$1" in

    'console')
        $WRAPPER_SCRIPT console
        errorcode=$?
        ;;
    'start')
        $WRAPPER_SCRIPT start
        errorcode=$?
        ;;

    'stop')
        $WRAPPER_SCRIPT stop
        errorcode=$?
        ;;

    'restart')
        $WRAPPER_SCRIPT restart
        errorcode=$?
        ;;

    'condrestart')
        $WRAPPER_SCRIPT condrestart
        errorcode=$?
        ;;

    'status')
        $WRAPPER_SCRIPT status
        errorcode=$?
        ;;

    'install')
        $WRAPPER_SCRIPT install
        errorcode=$?
        ;;

    'remove')
        $WRAPPER_SCRIPT remove
        errorcode=$?
        ;;

    'dump')
        $WRAPPER_SCRIPT dump
        errorcode=$?
        ;;

    'cleanup')
        cleanupPSVA
        errorcode=$?
        ;;

    *)
        echo "Usage: $0 { console | start | stop | restart | condrestart | status | install | remove | dump | cleanup }"
        exit 1
        ;;
esac

exit $errorcode