#!/bin/sh
# $Id: uninstall,v 1.1.2.2 2005/04/28 18:58:29 ms152511 Exp $
# uninstall - uninstall script for scs

BASEDIR=`dirname $0`/..
UNINSTDIR=$BASEDIR/uninstall
PRERM=$UNINSTDIR/preremove
POSTRM=$UNINSTDIR/postremove
PKGLIST=$UNINSTDIR/uninstall_list
SCSBASE=/scs
SCSLOGDIR=$SCSBASE/logs
SCSBACKUPDIR=$SCSBASE/backups

CUT=/usr/bin/cut
PKGRM="/bin/rpm -e --nodeps"
PKGCHK="/bin/rpm -q"

JDK_RPM="j2sdk-1.4.2_03-fcs"
TOMCAT_RPM="tomcat4-4.1.24-full.2jpp"

TRUE="true"
FALSE="false"

export TRUE
export FALSE

#
# functions
#

printError() {
    echo "ERROR: $@" 1>&2
}

printWarn() {
    echo "Warn: $@" 
}

printInfo() {
    echo "Info: $@" 
}

errorExit() {
    printError "$@"
    exit 1;
}

removePkg() {
  PKGNAME=$@
  if [ "X$PKGNAME" != "X" ] ; then
    if $PKGCHK $PKGNAME > /dev/null 2>&1 ; then
      OPTS="`echo $LINE | awk -F'|' '{ print $2; }' | sed -e 's/ //g'`"   
      if [ "X$OPTS" != "X" ] ; then  
        printInfo "Removing $PKGNAME with '$OPTS'"
      else
        printInfo "Removing $PKGNAME"
      fi

      if $PKGRM $OPTS "$PKGNAME" > /dev/null 2>&1 ; then
	    printInfo "Removed $PKGNAME" 
	  else
	    printWarn "Could not remove $PKGNAME"
	  fi
    else
      printInfo "Package $PKGNAME is not installed"
    fi
  fi
}

#
# Main
#

#
# Make sure the customer really wants to remove the software
#
if [ -f /etc/build ] ; then
  BUILD="`cat /etc/build | $CUT -d' ' -f1-2`"
else
  BUILD=""
fi
printInfo "Uninstalling Sun Control Station ($BUILD)"
printf "Proceed [N]? "
read RESPONSE
case "$RESPONSE" in
    [yY]|[Yy]es|YES) : ;;
    *) echo "Uninstall aborted." ; exit 0 ;;
esac

#
# Prompt for removal of jdk and tomcat
#
printf "Do you want to remove tomcat ($TOMCAT_RPM)? [Y] "
read RESPONSE
case "$RESPONSE" in
    [nN]|[Nn]o|NO) REMOVE_TOMCAT=$FALSE ;;
    *) REMOVE_TOMCAT=$TRUE ;;
esac
export REMOVE_TOMCAT

printf "Do you want to remove jdk ($JDK_RPM)? [Y] "
read RESPONSE
case "$RESPONSE" in
    [nN]|[Nn]o|NO) REMOVE_JDK=$FALSE ;;
    *) REMOVE_JDK=$TRUE ;;
esac
export REMOVE_JDK

#
# ask whether to clean up filesystem
#
printf "Do you want to remove $SCSBASE? [Y] "
read RESPONSE
case "$RESPONSE" in
    [nN]|[Nn]o|NO) REMOVE_SCSBASE=$FALSE ;;
    *) REMOVE_SCSBASE=$TRUE ;;
esac
export REMOVE_SCSBASE

if [ $REMOVE_SCSBASE = $FALSE ] ; then
    # ask whether to remove specific subdirectories
    printf "Do you want to remove $SCSLOGDIR? [Y] "
    read RESPONSE
    case "$RESPONSE" in
        [nN]|[Nn]o|NO) REMOVE_SCSLOGS=$FALSE ;;
        *) REMOVE_SCSLOGS=$TRUE ;;
    esac
    export REMOVE_SCSLOGS
    
    printf "Do you want to remove $SCSBACKUPDIR? [Y] "
    read RESPONSE
    case "$RESPONSE" in
        [nN]|[Nn]o|NO) REMOVE_SCSBACKUPS=$FALSE ;;
        *) REMOVE_SCSBACKUPS=$TRUE ;;
    esac
    export REMOVE_SCSBACKUPS
    
    printf "Do you want to remove the SCS SSH keypair? [Y] "
    read RESPONSE
    case "$RESPONSE" in
        [nN]|[Nn]o|NO) REMOVE_SCSKEYPAIR=$FALSE ;;
        *) REMOVE_SCSKEYPAIR=$TRUE ;;
    esac
    export REMOVE_SCSKEYPAIR
fi

#
# Run the preremove script
#
echo
printInfo "Executing preremove"
/bin/sh "$PRERM" || errorExit "Preremove failed"
printInfo "Preremove execution complete"

#
# Remove all the packages listed in the PKGLIST file
#
printInfo "Removing Packages"
while read LINE
do
   case "$LINE" in 
       [A-Za-z]*) : ;;
       *) continue ;;
   esac

   PKGNAME="`echo $LINE | awk -F'|' '{ print $1; }' | sed -e 's/ //g'`"
   removePkg $PKGNAME
done < "$PKGLIST"
printInfo "Package removal complete"

# TODO: NOTE: if we change the versions we are installing in the
# install_list, we must manually change these values here.
#
# Now remove tomcat and jdk if they wanted.
#

if [ "$REMOVE_TOMCAT" = "$TRUE" ] ; then
  removePkg $TOMCAT_RPM
fi

if [ "$REMOVE_JDK" = "$TRUE" ] ; then
  removePkg $JDK_RPM
fi

#
# Run the postremove script
#
printInfo "Executing postremove"
/bin/sh "$POSTRM" || printError "Postremove failed"
printInfo "Postremove complete"

#
# notify and exist
#
printInfo "Uninstall complete"

exit 0
