#!/bin/sh
#
# Copyright(C) 1993-2007 Adobe Systems Incorporated. All rights reserved.
#
# Adobe Flash Media Server Uninstaller
#

# installation specific
SERVICE_NAME=fms
INSTALLDIR=/opt/adobe/fms
WEBPUB=
APPDEST=/opt/adobe/fms

SERVICES_DIR=/etc/adobe/fms/services

PRODUCT="Adobe Flash Media Server"

# Environment variables
PATH=.:/bin:/usr/bin:/usr/local/bin:/sbin:$PATH
export PATH

# Get the path of this script
cwd=`dirname $0`


###############
# main section
###############

# check user
USERID=`id | sed -e 's/).*//; s/^.*(//;'`
if [ "X$USERID" != "Xroot" ]; then
  echo ""
  echo "ERROR: You must be logged in as the root user to uninstall"
  echo "       the $PRODUCT."
  echo ""
  exit 1
fi

echo ""
echo "Uninstalling $PRODUCT..."

echo ""
echo "Installation directory to remove:   $INSTALLDIR"
echo "Server-side source files to remove: $APPDEST"
#echo "Service to remove:   $SERVICE_NAME"
ask_to_uninstall () {
echo ""
printf "Proceed with the uninstallation? (y/n): "
read yn
  case $yn in
    y | Y) okToProceed=1
           ;;
    n | N) echo ""
           echo "Uninstallation aborted." 
           exit 1
           ;;
        *) echo ""
           echo "Please enter 'y' or 'n'."
           ask_to_uninstall
           ;;
  esac
}
ask_to_uninstall

if [ $okToProceed -eq 1 ]; then
  # remove server service
  if [ -f "$SERVICES_DIR/$SERVICE_NAME" ]; then
    $INSTALLDIR/fmsmgr server $SERVICE_NAME stop
    $INSTALLDIR/fmsmgr adminserver stop
    $INSTALLDIR/fmsmgr remove $SERVICE_NAME
  fi

  # remove FMS start/stop script
  if [ -d "$SERVICES_DIR" ]; then
    NUMSERVICES=`ls "$SERVICES_DIR" | sed -e "/fmsadmin/d" | wc -l`
    if [ $NUMSERVICES -eq 0 ]; then
      /sbin/chkconfig --del fms
      rm -f /etc/init.d/fms > /dev/null 2>&1
    fi
  fi

  # remove /etc/adobe/fms/services directory
  rm -rf /etc/adobe/fms > /dev/null 2>&1

  # remove adobe directory if empty
  rmdir /etc/adobe > /dev/null 2>&1

  # remove server-side source
  rm -rf "$APPDEST"
  
  # remove FMS files
  rm -rf $INSTALLDIR > /dev/null 2>&1

  # remove adobe dir if standard location
  rmdir `dirname $INSTALLDIR` > /dev/null 2>&1


  echo ""
  echo "The $PRODUCT uninstallation is complete."
  echo ""
fi
