#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bosinst/samples/AE/ae.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2009 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)22        1.1  src/bos/usr/lpp/bosinst/samples/AE/ae.sh, bosinst, bos720 3/29/09 15:14:03

# This is the Activation Engine. It is installed on a virtual machine 
# template to provides a simple interface to run activation scripts
# during the boot up sequence. When the template is cloned, they will
# run at boot, look for a floppy disk containing the options for each 
# activation script, and run just once.

PERL_BIN="/usr/bin/perl"
TEMPCONF="/tmp/tempconf"

ECHOE="echo -e"
UNAME=`uname -s`;
if [ "$UNAME" = "AIX" ]
then
  ECHOE="echo"
fi

#---------------functions-----------------#

usage()
{
	$ECHOE "Usage: sh $0 OPTION [FILE]\n"
	$ECHOE "  -a AL_FILE\t\t\tCreate Multiple Services"
	$ECHOE "  -r AL_FILE\t\t\tRemove Multiple Services"
#	$ECHOE "  -fa AL_FILE\t\t\tCreate Single Service"
#	$ECHOE "  -fr AL_FILE\t\t\tRemove Single Service"
	$ECHOE "  -i [AP_FILE]\t\t\tActivate interactively"
#	$ECHOE "  -f [AP_FILE]\t\t\tActivate forcibly"
	$ECHOE "  -s RESOURCENAME CONFIGNAME\t\t\tActivate by service"
}


# get the full path of script and parameter
getScriptFullPath()
{
	DIR=""
	NAME=""
	CHK=`$ECHOE $0|grep '^/'`
	if [ ! -z "$CHK" ]; then
	 DIR=`dirname $0`
	 NAME=`basename $0`
	else
	 DIR=`pwd`"/"`dirname $0`
	 NAME=`basename $0`
	fi

	echo "$DIR/$NAME"
}

getParameterFullPath()
{
  FPATH=""
  CHK=`$ECHOE "$1"|grep '^/'`
  if [ -z "$CHK" ]; then
    FPATH=`pwd`"/$1"
  else
    FPATH=$1
  fi
  echo "$FPATH"
}

#extract configuration elements
extractConfFromAL()
{
	cp $1 $TEMPCONF
	#cat "$1" |grep "<configuration " > $TEMPCONF
}

extractResrcName(){
	$ECHOE "$1" | sed "s/.*software-resource name=\"//g" | sed "s/\".*//g"
}

extractConfName()
{
	$ECHOE "$1" | sed "s/.*configuration name=\"//g" | sed "s/\".*//g"
}

#clean AR
cleanAR()
{
  rm -rf ${AE_DIR}/AR/*
}


# excute every service forcibly.
executeForcely()
{
  counter=1
	rsrcName=""
	while :
	do
		loopstr=`sed -n "${counter}p" $TEMPCONF`
		if [ -z "$loopstr" ]; then
			$ECHOE "$0: Activation Done"
			break
		else
			case $loopstr in
				*\<software-resource*)
				rsrcName=`extractResrcName "$loopstr"`
				;;
				*\<configuration*)
				confName=`extractConfName "$loopstr"`
				;;
				*\</configuration*) # finish one configration
				if [ -z "$confName" -o -z "$rsrcName" ]; then
					$ECHOE "$0: parameter CONFIGNAME or RESOURCENAME is not found."
					exit -1
				fi
				echo -n "activate $confName:\n"
				$PERL_BIN $ACTVM_BIN $AE_DIR $rsrcName $confName
				$confName=""
				;;
				*)
				;;
			esac
			counter=`expr $counter + 1`
		fi
	done
}

# Interactively execute services
executeInteractively()
{
  counter=1
	rsrcName=""
	while :
	do
		loopstr=`sed -n "${counter}p" $TEMPCONF`
		if [ -z "$loopstr" ]; then
			$ECHOE "$0: Activation Done"
			break
		else
			case $loopstr in
				*\<software-resource*)
				rsrcName=`extractResrcName "$loopstr"`
				;;
				*\<configuration*)
				confName=`extractConfName "$loopstr"`
				;;
				*\</configuration*)
				if [ -z "$confName" -o -z "$rsrcName" ]; then
					$ECHOE "$0: parameter CONFIGNAME or RESOURCENAME is not found."
					exit -1
				fi
				
				echo -n "activate $rsrcName $confName ? (Y/N): "
				read CHOICE
				CHOICE=`$ECHOE $CHOICE | grep -i "^y"`
				if [ ! -z "$CHOICE" ]; then
					$PERL_BIN $ACTVM_BIN $AE_DIR $rsrcName $confName
				else
					$ECHOE "$rsrcName $confName: Skip Activation"
				fi
				confName=""
				;;
				*)
				;;
			esac
			counter=`expr $counter + 1`
		fi
	done
}

#---------------functions-----------------#



#--------This is begin of script--------#
ScriptPath=`getScriptFullPath`
ScriptPath=`$ECHOE "$ScriptPath" | sed "s|/\./|/|"`
AE_DIR=`dirname $ScriptPath`
export LIBPATH=${AE_DIR}:$LIBPATH

ACTVM_BIN="${AE_DIR}/AE/ActivateVM"


if [ ! -f "$ACTVM_BIN" ]; then
  $ECHOE "$0: \"$ACTVM_BIN\" is not found."
  exit -1
fi

if [ ! -f "$PERL_BIN" ]; then
  $ECHOE "$0: \"$PERL_BIN\" is not found."
  exit -1
fi

# there may be seven different options to execute the services and remove services
case $1 in
  -a) # Create Multiple Services
  if [ $# -eq 2 ]; then
	  AL_PATH=`getParameterFullPath $2`
	else
	  usage
	  exit -1
  fi

	savedPath=`pwd` #save the current directory path
  cd "$AE_DIR"
  
	sh AE/CreateService "$AL_PATH" AE/ServiceTemplate "$ScriptPath"

  cd "$savedPath" # go back to original directory
  ;;
  -r) # Remove Multiple Services
  if [ $# -eq 2 ]; then
	  AL_PATH=`getParameterFullPath $2`
	else
	  usage
	  exit -1
  fi

	savedPath=`pwd`
  cd "$AE_DIR"
  
	sh AE/RemoveService "$AL_PATH"

  cd "$savedPath"
  ;;
  -fa) # Create Single Service
  if [ $# -eq 3 ]; then # get needed parameters
	  AL_PATH=`getParameterFullPath $2`
	  SERVICE_NAME="$3"
	else
	  usage
	  exit -1
  fi

	savedPath=`pwd`
  cd "$AE_DIR"
  
  
	sh AE/CreateService "$AL_PATH" AE/ServiceTemplate "$ScriptPath" "$SERVICE_NAME"

  cd "$savedPath"
  ;;
  -fr) # Remove Single Service
  if [ $# -eq 3 ]; then
	  AL_PATH=`getParameterFullPath $2`
	  SERVICE_NAME="$3"
	else
	  usage
	  exit -1
  fi

	savedPath=`pwd`
  cd "$AE_DIR"
  
	sh AE/RemoveService "$AL_PATH" "$SERVICE_NAME"

  cd "$savedPath"
  ;;
	-s) # Activate by service
	if [ $# -eq 3 ]; then
		RESOURCENAME="$2"
	  CONFIGNAME="$3"
	else
	  $ECHOE "$0: Parameter Error!\n"
	  exit -1
	fi
	
	if [ -z "$CONFIGNAME" -o -z "$RESOURCENAME" ]; then
	  $ECHOE "$0: parameter CONFIGNAME or RESOURCENAME is not found."
	  exit -1
	fi

	savedPath=`pwd`
  cd "$AE_DIR"
	
	$PERL_BIN $ACTVM_BIN $AE_DIR $RESOURCENAME $CONFIGNAME

  cd "$savedPath"
	;;
	-f) # Activate forcibly
	# find AL and AP first
	if [ $# -eq 1 ]; then
		AL=`find ${AE_DIR}/AL -name "master.al" -print`;
		test -f "$AL" || {
			$ECHOE "AL file is not found."
			exit -1 
		}
	elif [ $# -eq 2 ]; then
		AL=`find ${AE_DIR}/AL -name "master.al" -print`;
		test -f "$AL" || {
			$ECHOE "AL file is not found."
			exit -1 
		}
		apSrc="$2"
		if [ -f "$apSrc" ]; then #local path, copy
		  cp "$apSrc" ${AE_DIR}/AP
		else #remote path, download
		  rm -rf ${AE_DIR}/AP/*
		  savedPath=`pwd`
		  cd ${AE_DIR}/AP
		  wget "$apSrc" 1>/dev/null 2>/dev/null
		  cd "savedPath"
		fi
	else
	  usage
	  exit -1
	fi
	
  cleanAR
	
        AP=`find ${AE_DIR}/AP -name "ovf-env.xml" -print`
        if [ ! -e "$AP" ]
        then
         	AP=`find ${AE_DIR}/AP -name "*.ap" -print`;
        fi
	test -f "$AP" || {
		$ECHOE "AP file is not found."
		exit -1 
	}
	
	extractConfFromAL "$AL"
	if [ ! -f "$TEMPCONF" ]; then
  	$ECHOE "$0: $TEMPCONF is not created."
  	exit -1
	fi
  executeForcely
  rm -rf "$TEMPCONF"
	;;
	-i) # Activate interactively
	if [ $# -eq 1 ]; then
		AL=`find ${AE_DIR}/AL -name "master.al" -print`;
		test -f "$AL" || {
			$ECHOE "AL file is not found."
			exit -1 
		}
	elif [ $# -eq 2 ]; then
		AL=`find ${AE_DIR}/AL -name "master.al" -print`;
		test -f "$AL" || {
			$ECHOE "AL file is not found."
			exit -1 
		}
		apSrc="$2"
		if [ -f "$apSrc" ]; then #local path, copy
		  cp "$apSrc" ${AE_DIR}/AP
		else #remote path, download
		  rm -rf ${AE_DIR}/AP/*
		  savedPath=`pwd`
		  cd ${AE_DIR}/AP
		  wget "$apSrc" 1>/dev/null 2>/dev/null
		  cd "savedPath"
		fi
	else
	  usage
	  exit -1
	fi
  
  cleanAR
  
        AP=`find ${AE_DIR}/AP -name "ovf-env.xml" -print`
        if [ ! -e "$AP" ]
        then
		AP=`find ${AE_DIR}/AP -name "*.ap" -print`
        fi
	test -f "$AP" || {
		$ECHOE "AP file is not found."
		exit -1 
	}
	
	extractConfFromAL "$AL"
	if [ ! -f "$TEMPCONF" ]; then
  	$ECHOE "$0: $TEMPCONF is not created."
  	exit -1
	fi
  executeInteractively
  rm -rf "$TEMPCONF"
	;;
	*)
	usage
	;;
esac

#--------This is end of script--------# 
