#!/bin/bash
#
#  TRIPsystem uninstallation script
#  Copyright (C) DVG Operations GmbH
#
#  This script uninstalls TRIPsystem by removing symbolic links
#  and various system configuration done during install. No files
#  will actually be removed during this process.
#


# save current date and time
INST_DATE=`date '+%Y-%m-%d %H:%M:%S'`

# The current TRIP version
VERS_DIR=v843_1
VERSION=8.4-3
PATCH_NO=1

if [ "$PATCH_NO" = "0" ]; then
   VERSIONP="$VERSION"
else
   VERSIONP="$VERSION:$PATCH_NO"
fi

LINK_DIR=/usr/local/trip/sys
if [ "$OSNAME" = "SunOS" ]; then
   SHARED_STATE_DIR=/var/trip
elif [ "$OSNAME" = "Darwin" ]; then
   SHARED_STATE_DIR=/usr/local/trip/var
else
   SHARED_STATE_DIR=/var/lib/trip
fi

OSNAME=`uname`

if [ "$OSNAME" = "Darwin" ]; then
   SOEXT=dylib
else
   SOEXT=so
fi

WHOM=`(whoami) 2> /dev/null`
if [ $? -ne 0 ]; then
   if [ -f /usr/ucb/whoami ]; then
      WHOM=`/usr/ucb/whoami`
   fi
fi

# This script needs root privileges unless we're on macOS.
if [ "$OSNAME" != "Darwin" ]; then
   if [ "$WHOM" != root ]; then
      echo ""
      echo "You must be root to run this script!"
      echo "Exiting the uninstallation procedure"
      echo ""
      exit 1
   fi
fi

if [ `echo "X\c"` = X ]; then
   C1=
   C2='\c'
elif [ `echo -n X` = X ]; then
   C1='-n'
   C2=
else
   C1=
   C2=
fi

yesno_valid()
{
   inputval=$1
   if [ "$inputval" == "y" ] || [ "$inputval" == "Y" ] || \
      [ "$inputval" == "n" ] || [ "$inputval" == "N" ]
   then
      return 0
   elif [ "$2" != "" ] && [ "$inputval" = "?" ]; then
      return 0
   else
      return 1
   fi
}

yesno()
{
   yesno_result=-1
   yesno_prompt=$1
   yesno_default=$2
   yesno_help=$3
   if [ "$yesno_default" = "Y" ]; then
      yesno_choice="[Y|n"
   elif [ "$yesno_default" = "N" ]; then
      yesno_choice="[y|N"
   else
      yesno_choice="[y|n"
   fi
   if [ "$yesno_help" != "" ]; then
      yesno_choice="$yesno_choice|?"
   fi
   yesno_choice="$yesno_choice]"
   doloop=0
   while [ "$doloop" = "0" ]
   do
      echo $C1 "$yesno_prompt $yesno_choice $C2"
      read answ
      if [ "$answ" != "" ] ; then
         if yesno_valid $answ "$yesno_help"; then
            printf "\n****** Answer: $answ\n"
         else
            continue
         fi
      elif [ "$answ" = "" ] && [ "$yesno_default" != "" ]; then
         printf "\n****** Answer: default [= $yesno_default]\n"
         answ=$yesno_default
      else
         continue
      fi
      case $answ in
         \?)
            if [ "$yesno_help" != "" ]; then
               eval $yesno_help
            fi
            ;;
         Y|y)
            yesno_result=0
            doloop=1
            ;;
         N|n)
            yesno_result=1
            doloop=1
            ;;
         *)
            ;;
      esac
   done
   return $yesno_result
}



##########################################################################
# check to make sure you are starting this from the right directory
#

if [ -d $VERS_DIR ]; then
   cd $VERS_DIR
fi

echo ""
if [ ! -d sbin ]; then
   if [ -f install ]; then
      cd ..
      if [ ! -d demo ]; then
         echo "Please run this script from one of the following directories:"
         echo ""
         echo "  <extract_dir>"
         echo "  <extract_dir>/$VERS_DIR"
         echo "  <extract_dir>/$VERS_DIR/sbin"
         echo ""
         echo "where <extract_dir> is the directory from which you issued the"
         echo "tar command to read the distribution media."
         echo ""
         echo "Exiting the uninstallation procedure"
         echo ""
         exit 1
      fi
   else
      echo "Please run this script from one of the following directories:"
      echo ""
      echo "  <extract_dir>"
      echo "  <extract_dir>/$VERS_DIR"
      echo "  <extract_dir>/$VERS_DIR/sbin"
      echo ""
      echo "where <extract_dir> is the directory from which you issued the"
      echo "tar command to read the distribution media."
      echo ""
      echo "Exiting the uninstallation procedure"
      echo ""
      exit 1
   fi
fi


if [ "`uname`" = "Darwin" ]; then
  LIBTDBS_PATH=/usr/local/lib/libtdbs.$SOEXT
else
  LIBTDBS_PATH=/usr/lib/libtdbs.$SOEXT
fi
if [ ! -f $LIBTDBS_PATH ]; then
  LIBTDBS_PATH=/usr/local/trip/sys/lib/libtdbs.$SOEXT
fi

echo "This script will uninstall TRIPsystem from your machine. Your data will be"
echo "left intact as will the TRIPsystem files themselves."
echo ""
echo "IMPORTANT: When upgrading TRIPsystem, you must NOT uninstall the old version."
echo "If you uninstall and you later wish to install a new TRIPsystem version and"
echo "continue to use your current data, you must perform a manual migration of the"
echo "data since the TRIPsystem installer then will not be able to detect your "
echo "previous installation if it has been uninstalled."
echo ""
if ! yesno "Do you wish to continue?" Y ; then
   echo ""
   echo "Exiting the uninstallation procedure."
   echo ""
   exit 1
fi
echo ""

LIBTDBS_ACTUAL=""
if [ ! -f $LIBTDBS_PATH ]; then
   echo "WARNING: The libtdbs.$SOEXT library is not installed. TRIPsystem"
   echo "         is either not installed or its installation is broken."
   echo ""
else
   LIBTDBS_ACTUAL=`readlink -f $LIBTDBS_PATH`

   VER_INSTALLED=`strings -a $LIBTDBS_ACTUAL | grep ^[0-9]\.[0-9]\-[0-9] | sort -u | head -1 2>/dev/null`
   VER_CURRENT=`strings -a lib/libtdbs.$SOEXT | grep "^$VERSIONP" | sort -u 2>/dev/null`

   if [ "$VER_INSTALLED" != "$VER_CURRENT" ]; then
      echo "The installed TRIPsystem version is $VER_INSTALLED, which doesn't"
      echo "match the version associated with this script ($VER_CURRENT)."
      echo "Please run the uninstallation script for the matching version of TRIPsystem."
      echo ""
      echo "Exiting the uninstallation procedure."
      echo ""
      exit 1
   fi
fi

qrcs()
{
   RCSNAME=$1
   grep "^$RCSNAME" $LINK_DIR/conf/tdbs.conf | ( read r; echo $r | cut -d= -f2 | tr -d '\r')
}

TDBS_CTL=""
TDBS_LOG=""
TDBS_BASES=""

if [ -f $LINK_DIR/conf/tdbs.conf ]; then
   TDBS_CTL=`qrcs TDBS_CTL`
   TDBS_LOG=`qrcs TDBS_LOG`
   TDBS_BASES=`qrcs TDBS_BASES`
fi

if [ "$TDBS_CTL" != "" ] && [ -d "$TDBS_CTL" ]; then
   if [ -f $TDBS_CTL/CONTROL.BAF ]; then
      echo "Your control database files are located in $TDBS_CTL"
      echo "You may wish to keep them if you wish to continue to use TRIP"
      echo "later. If not, they can be deleted."
      echo ""
   fi
fi

if [ "$TDBS_BASES" != "" ] && [ -d "$TDBS_BASES" ]; then
   DBCNT=`ls $TDBS_BASES/*.BAF 2> /dev/null | wc -l`
   echo "The database directory TDBS_BASES is defined as $TDBS_BASES"
   echo ""
   FOUNDMARK=""
   if [ $DBCNT -gt 0 ]; then
      echo "   You have files for $DBCNT databases in the TDBS_BASES directory."
      echo "   You may wish to keep them if you wish to continue to use TRIP"
      echo "   later. If not, they can be deleted."
      echo ""
      FOUNDMARK=" too"
   else
      echo "   The TDBS_BASES directory does not contain any database files."
   fi
   echo "   You may have database files in other directories$FOUNDMARK."
   echo "   Such database files are not reported by this script."
   echo ""
fi

if [ -e $LINK_DIR/var ]; then
   ACTUAL_VAR=`readlink $LINK_DIR/var 2> /dev/null`
   if [ "$ACTUAL_VAR" != "" ] && [ -d "$ACTUAL_VAR" ]; then
      echo "The shared state directory for TRIP is $ACTUAL_VAR"
      echo "This directory will be kept. If you do not wish to resume using"
      echo "TRIP later, you can remove this directory at your convenience."
      echo ""
   fi
fi

unset LDSOINC
LIBTDBS_LINKED=0
if [ -f /etc/ld.so.conf ]; then
   LDSOINC=`grep ^include /etc/ld.so.conf 2>/dev/null|cut -d" " -f2- |rev|cut -d/ -f2-|rev`
   if [ "$LDSOINC" != "" ]; then
      pushd /etc > /dev/null 2>&1
      cd $LDSOINC 2> /dev/null
      if [ $? -eq 0 ]; then
         LDSOINC=`pwd`
      else
         unset LDSOINC
      fi
      popd > /dev/null 2>&1
   fi
fi


get_pidof()
{
   PIDOF_CMD=`which pidof 2> /dev/null`
   if [ "$PIDOF_CMD" = "" ]; then
      ps -ef | grep $1 | grep -v ttymon | grep -v grep | awk '{print $2}'
   else
      pidof $1 | awk '{print $1}'
   fi
}

##
# Check if a process is running based on info from /proc
#
is_running()
{
   if [ -d /proc/$1 ]; then
      return 0
   else
      return 1
   fi
}


##
# Wait for up to ten seconds for a process to exit.
#
waitpid()
{
   WPID=$1
   TIMELEFT=10
   while is_running $WPID
   do
      if [ $TIMELEFT -eq 0 ]; then
         return 1
      fi
      TIMELEFT=`expr $TIMELEFT - 1`
      sleep 1s
   done
   return 0
}


echo "Uninstalling..."
echo ""

WARNCNT=0

PID_TRIPD=`get_pidof tripd`
if [ "x$PID_TRIPD" != "x" ]; then
   echo "Stopping tripd (pid $PID_TRIPD) ... "
   kill -TERM $PID_TRIPD
   if ! waitpid $PID_TRIPD ; then
      kill -KILL $PID_TRIPD
   fi
   if [ $? -ne 0 ]; then
      WARNCNT=`expr $WARNCNT + 1`
      echo "Unable to stop tripd"
   fi
fi

PID_TRIPNETD=`get_pidof tripnetd`
if [ "x$PID_TRIPNETD" != "x" ]; then
   echo "Stopping tripnetd (pid $PID_TRIPNETD) ... "
   kill -TERM $PID_TRIPNETD
   if ! waitpid $PID_TRIPNETD ; then
      kill -KILL $PID_TRIPNETD
   fi
   if [ $? -ne 0 ]; then
      WARNCNT=`expr $WARNCNT + 1`
      echo "Unable to stop tripnetd"
   fi
fi

killall -TERM tbserver

if [ ! -e /.dockerenv ] && [ "$OSNAME" = "Linux" ]; then
   sbin/install_services --remove -y
   if [ $? -ne 0 ]; then
      WARNCNT=`expr $WARNCNT + 1`
      echo ""
   fi
fi

if [ "$TDBS_LOG" != "" ] && [ -d "$TDBS_LOG" ]; then
   pushd $TDBS_LOG > /dev/null 2>&1
   LOGCNT=`ls *.log *.BUT *.SIF *.STO* *.RIF* ERRLOG*.TFO 2>/dev/null | wc -l`
   if [ $LOGCNT -gt 0 ]; then
      echo "There are $LOGCNT files remaining in the log directory $TDBS_LOG"
      if ! yesno "Do you wish to keep them?" N ; then
         rm -f *.log *.BUT *.SIF *.STO* *.RIF* ERRLOG*.TFO
         if [ $? -eq 0 ]; then
            echo ""
            echo "Log files removed."
         fi
      fi
      echo ""
   fi
   popd > /dev/null 2>&1
fi

if [ "$OSNAME" = "Linux" ]; then
   sbin/chfapolicy_sys -r -p `pwd`
fi

rm -vf $LINK_DIR/lib $LINK_DIR/conf $LINK_DIR/var
if [ $? -ne 0 ]; then
   WARNCNT=`expr $WARNCNT + 1`
   echo ""
fi

if [ "$LDSOINC" != "" ] && [ -f $LDSOINC/tripsystem-libpath.conf ]; then
   rm -fv $LDSOINC/tripsystem-libpath.conf
   LDCONFIG=`which ldconfig 2>/dev/null`
   if [ "$LDCONFIG" = "" ]; then
      LDCONFIG=/sbin/ldconfig
   fi
   $LDCONFIG
   if [ $? -ne 0 ]; then
      WARNCNT=`expr $WARNCNT + 1`
      echo ""
   fi
fi

echo ""
if [ $WARNCNT -eq 0 ]; then
   echo "Uninstallation completed successfully"
else
   echo "Errors and/or warnings detected."
   echo "Uninstallation not completed successfully."
fi
echo ""

exit $WARNCNT

