#!/sbin/sh
# @(#) unconfigure.sd - SQL/R A.04.00
# SD unconfiguration script, HP-UX 11.x
# (C) Copyright 2010-2022 Marxmeier Software AG. All rights reserved.
#
# Unconfigure scripts are executed during the un-configuration phase
# of a swremove process. They can also be executed by the swconfig
# command.
#
# Changes:
# 19.07.2010 - link to libcrypto.so
# 27.05.2020 - /var/opt/sqlr2/run directory
# 05.07.2022 - /opt/sqlr/4.0 base directory

#################################################
#             Startup                           #
#################################################

# source standard SD control script utilities

UTILS=/usr/lbin/sw/control_utils
if [ ! -f $UTILS ]
then
   echo "ERROR:    Cannot find $UTILS"
   exit 1
fi
. $UTILS

# set retval and exitval

exitval=$SUCCESS
set_status()
{
   retval=${1:-$?}
   [[ $retval -eq  $FAILURE ]] && exitval=$retval
}


#################################################
#             unconfigure product               #
#################################################

SQLR_base=/opt/sqlr
SQLR=$SQLR_base/4.0

# kill sqlrodbcd process if active

if [ -f ${SQLR}/bin/sqlrodbcd ]
then
   pid=$(/usr/sbin/fuser ${SQLR}/bin/sqlrodbcd 2>/dev/null)
   if [ -n "$pid"  ]
   then
      kill $pid
      retval=$?
      if [ $retval -ne $SUCCESS ]
      then
         echo "WARNING: could not kill the \"sqlrodbcd\" process"
         exitval=$WARNING
      fi
   fi
fi
rm -f /var${SQLR}/run/sqlrodbcd_*.pid


# remove symlink ${SQLR}/lib/hpux../lib...sl

rm -f ${SQLR}/lib/hpux32/libsqlrodbc.sl
rm -f ${SQLR}/lib/hpux64/libsqlrodbc.sl
rm -f ${SQLR}/lib/hpux64/libsqlrodbc64.sl

rm -f ${SQLR}/lib/hpux32/libsqlr3odbc.sl
rm -f ${SQLR}/lib/hpux64/libsqlr3odbc.sl
rm -f ${SQLR}/lib/hpux64/libsqlr3odbc64.sl

# remove symlink ${SQLR}/lib/*/libcrypto.*

rm -f ${SQLR}/lib/hpux32/libcrypto.so
rm -f ${SQLR}/lib/hpux64/libcrypto.so
rm -f ${SQLR}/lib/pa11_32/libcrypto.sl


# remove startup scripts

if [ -f /sbin/rc2.d/S900sqlr40 ]
then
   rm /sbin/rc2.d/S900sqlr40
   set_status
fi

if [ -f /sbin/rc1.d/K100sqlr40 ]
then
   rm /sbin/rc1.d/K100sqlr40
   set_status
fi

if [ -f /sbin/init.d/sqlr40 ]
then
   rm /sbin/init.d/sqlr40
   set_status
fi

# delete /opt/sqlr/4.0 from /etc/PATH

mod_pathfile -d P ${SQLR}/bin

# delete unchanged /etc/rc.config.d/sqlr40 config file

if [ -f /etc/rc.config.d/sqlr40 ]; then
   cmp -s ${SQLR}/newconfig/startup/rc.sqlr40 /etc/rc.config.d/sqlr40
   if [ $? -eq 0 ]; then
      rm -f /etc/rc.config.d/sqlr40
   else
echo "NOTE:    File /etc/rc.config.d/sqlr40 has been customized and is not removed."
   fi
fi


# Delete unchanged config files

for f in /opt/sqlr/4.0/newconfig/config/*
do
  cf=`basename $f`
  if [ -f /etc/opt/sqlr/4.0/$cf ]; then
     cmp -s $f /etc/opt/sqlr/4.0/$cf
     if [ $? -eq 0 ]; then
        rm -f /etc/opt/sqlr/4.0/$cf
     else
echo "NOTE:    File /etc/opt/sqlr/4.0/$cf has been changed and is not removed."
     fi
  fi
done


# Delete dirs: /var/opt/sqlr/4.0, /etc/opt/sqlr/4.0

if [ -d /var/${SQLR} ]; then
   find /var/${SQLR} -type d -depth|xargs rmdir -f 2>/dev/null
fi

if [ -d /etc/${SQLR} ]; then
   find /etc/${SQLR} -type d -depth|xargs rmdir -f 2>/dev/null
fi

exit $exitval
