#!/bin/sh
#
# Functions
#
ECHO="/bin/echo"
ECHONL=""
ECHOPARM="-n"

# Ask for confirmation
#
yesno () {
  ask=$2
  while [ -z "$ask" ]
  do
    $ECHO $ECHOPARM "$1 [yes|no] ? $ECHONL"
    read ask
    ask=`echo $ask | tr "[A-Z]" "[a-z]"`
    if [ "$ask" != "yes" -a "$ask" != "no" ]; then ask=""; fi
  done
}

# Unlink files in /etc
#
etcunlink() {
  up="`echo $1 | sed 's~/[^/]*$~~'`"
  rm -f $1/FrontBase.sh
  rm -f $1/FrontBase
}
user=`whoami`
if test "$user" != "root"
then
   echo "You must be root to remove FrontBase package"
   $ECHO $ECHOPARM "Please enter root $ECHONL" 
   su root -c "$0" && exit $?
   exit 1
fi
cd=`echo $0 | sed "s~/[^/]*$~~"`
test "$cd" != "deinstall.sh" && cd $cd

if [ ! -f /etc/FrontBase.conf ]
then
  echo "FrontBase is not installed"
  exit 1
fi

. /etc/FrontBase.conf

yesno "\n
	All FrontBase services and servers will be stopped.\n\
	All FrontBase files, except LicenseString and files containing user data, will be erased.\n\n\
	Continue " ""
if [ $ask = "no" ]; then exit 1; fi


test -x $ETCDIR/FBWeb && $ETCDIR/FBWeb stop
echo "Stopping FrontBase servers"
test -x $ETCDIR/FrontBase && $ETCDIR/FrontBase stop

echo "Removing FrontBase files"
for i in `cat $FBDIR/filelist | sort -r`
do
  test -d $i && rmdir $i >/dev/null 2>&1
  rm -f $i >/dev/null 2>&1
done

etcunlink $ETCDIR

echo
echo "Any remaining files in $FBDIR must be removed manually."
echo

exit 0
