#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bosinst/samples/AE/AE/RemoveService.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 

# @(#)27        1.1  src/bos/usr/lpp/bosinst/samples/AE/AE/RemoveService.sh, bosinst, bos720 3/29/09 15:20:00

# RemoveService - a utility for removing services added by AE scripts.

AL=`find /opt/IBM/AE/AL/ -name "*.al"`
TEMPCONF="/tmp/tempconf"

PERL_BIN="/usr/bin/perl"
AEPATH="/opt/IBM/AE"

START_SCRIPT_PATH="/etc/rc.d"
ECHOE="echo -e"

UNAME=`uname -s`;
if [ "$UNAME" = "AIX" ]
then
  # /etc/rc.d is a symlink to /etc/init.d on suse
  # /etc/init.d does not exist on aix, but /etc/rc.d/init.d does
  # scripts usually kept in init.d instead of rc.d for aix

  START_SCRIPT_PATH="$START_SCRIPT_PATH/init.d"
  ECHOE="echo"
fi

MANAGEAL_BIN="${AEPATH}/AE/ManageALFile"

#---------------functions-----------------#
# extract configuration element
extractConfFromAL()
{
	cat $AL> $TEMPCONF
}

# extract service names
extractServName()
{
  $ECHOE "$1" | sed "s/.*service name=\"//g" | sed "s/\".*//g"
}
function hasDependency()
{
    servName=$1
    lineNum=`grep -n "${servName}" ${AL}|awk -F : '{print $1}'`
    while :
	do
	    line=`sed -n "${lineNum}p" ${AL}`
	    if [ -z "$line" ]
	    then
		break
	    else
		case ${line} in
		    *dependency*)
			containDeps=1
			break
			;;
		    *configuration*)
			break
			;;
		    *)
		    ;;
		esac
	    fi
	    lineNum=$[ $lineNum + 1 ]
	done
    echo -n "$containDeps"
}

# parse the resource name
function extractRsrcName()
{
    line=$1
    retValue=`echo ${line}|awk -F \" '{print $2}'`
    echo "${retValue}"
}

# parse the configuration name
function extractConfName()
{
    line=$1
    retValue=`echo ${line}|awk -F \" '{print $2}'`
    echo "${retValue}"
}
# delete record in start script file
function delStartRecord()
{
    servName=$1
    rsrcName=$2
    confName=$3
#    if [ "$UNAME" = "AIX" ]
#    then
#	rmitab ${servName}
#    else
	sed -i "s/.*${rsrcName} ${confName}.*//" "${START_SCRIPT_PATH}/${servName}" 
	sed -i "/^$/d" "${START_SCRIPT_PATH}/${servName}"
#    fi
}

# parse temp al file and remove services
parseTempConf()
{
  while read line
  do
      case ${line} in
	  *\<software-resource*)
	  rsrcName=`extractRsrcName "$line"`
	  ;;
	  *\<configuration*)
	  confName=`extractConfName "$line"`
	  ;;
	  *\<service*)
	  servName=`extractServName "$line"`
	  ;;
	  *\<dependency*)
	  hasDeps="yes"
	  ;;
	  *\</configuration*)
	  if [ "${hasDeps}" = "yes" ]
	  then
	    removeService "$servName"
	  else
	    delStartRecord "$servName" "$rscrname" "$confName"
	  fi
	  hasDeps="NO"
      esac
  done<$TEMPCONF
  $ECHOE "$0: Remove Service Done"
}

del_service()
{
  if [ `uname -r` == "AIX" ]
  then
    /usr/bin/perl \
	-e "my @run_levels = split($2);" \
        -e "foreach my \$run_level (@run_levels)" \
	-e "{" \
	-e "`rm /etc/rc.d/rc[0-9].d/[SK][0-9][0-9]${1}`;" \
	-e "}"
  else
    /sbin/chkconfig --del "$1" $2
  fi
}

# remove services
removeService()
{
  servName="$1"
	if [  -f /etc/SuSE-release ]
	then
		del_service "$servName" -f
	else
		del_service --del "$servName"
	fi
  test -f "/etc/init.d/$servName" && rm -f "/etc/init.d/$servName"
	find /etc/rc.d -name "*$servName" -exec rm {} \;
}
#---------------functions-----------------#


#----------begin of main process----------#

# input AL path manually
if [ ! -z "$1" ]; then
  AL="$1"
  $ECHOE "$0: use input AL file:			 \"$AL\""
else
  $ECHOE "$0: use default AL file:	 		\"$AL\""
fi

# input single service name
if [ ! -z "$2" ]; then
  SERVICE_NAME="$2"
fi

test -f $AL || {
	echo "$AL is missing."
	exit 1
}

if [ ! -z "$SERVICE_NAME" ]; then
  removeService "$SERVICE_NAME"
else
  extractConfFromAL
  if [ ! -f $TEMPCONF ]; then
    $ECHOE "$0: $TEMPCONF is not created."
    exit -1
  fi
  
  parseTempConf
  rm -rf $TEMPCONF
fi

$ECHOE "$PERL_BIN $MANAGEAL_BIN -r $AL"
$PERL_BIN $MANAGEAL_BIN -r $AL

#----------end of main process----------#
