#!/bin/sh

LINK1=/usr/lib/X11/700X
LINK2=/usr/bin/X11/700X
LINKS="$LINK1 $LINK2"

# Remove links to Aptrex dirs if any 

APTREX_INSTALLDIR=`grep '^APTREX_INSTALLDIR=' $LINK2/admin/env.vars 2> /dev/null | awk -F= '{ print $2}'`

if [ "X$APTREX_INSTALLDIR" != X ]
then 
    echo "Restoring Aptrex directories."
    ( cd $APTREX_INSTALLDIR ;
    for files in `ls -d *.aptrex`
    do
      o_file=`echo $files | awk -F. '{ print $1 }'`
      if [ -h "$o_file" ]
      then 
         rm -f "$o_file"  > /dev/null 2>&1
         if [ $? -eq 0 ]
         then 
             echo removed symbolic link $o_file 
             mv $files "$o_file"
         else
             echo removal of symbolic link $o_file file failed.
         fi
      else
         if [ -d $o_file -a -h $o_file/lib -a -h $o_file/bin ]
         then
             rm -rf $o_file
            mv $files $o_file
         fi
      fi
    done
    )
fi

FILES="SUBNETMASK XSESSIONCHOICE GATEWAY env.vars"
for FILE in $FILES 
do
  if [ -f $LINK2/admin/$FILE ]
  then
    rm $LINK2/admin/$FILE > /dev/null
  fi
done

rm -rf $LINK1/config
rm -rf $LINK1/bin*
rm -rf $LINK1/ENWARE*

for LINK in $LINKS
do
  rm $LINK > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo removal of symbolic link $LINK failed
  else
    echo "removed symbolic link $LINK"
  fi
done

rm -f /etc/rc3.d/K172xdm.hp
rm -f /etc/rc3.d/S172xdm.hp
rm -f /etc/init.d/xdm.hp


#  If bootp seems to be ours remove it from /etc/inetd.conf 
#  and restart inetd	
#
egrep 'etc/700X/bootptab' /etc/inetd.conf > /dev/null
if [ $? = 0 ]
then
  cp /etc/inetd.conf /etc/inetd.conf.orig.$$
  sed '/etc[/]700X[/]bootptab/d' /etc/inetd.conf > /etc/inetd.conf.$$
  if [ -s /etc/inetd.conf.$$ ]
  then
    echo removed bootp entry from /etc/inetd.conf 
    cp /etc/inetd.conf.$$ /etc/inetd.conf
    rm /etc/inetd.conf.$$

    ps -e | awk '$4 == "inetd" {print $4 }' | grep . > /dev/null 2>&1

    if [ $? -eq 0 ]
    then
      PID=`ps -e | awk '$4 == "inetd" {print $1 }' | head -1`
      if [ $PID ]
      then
        kill -1 $PID
        sleep 2
        kill -1 $PID
        echo "Signalled inetd to re-read /etc/inetd.conf"
      else
        echo "WARNING: Couldn't send SIGHUP to inetd."
      fi
    fi
  fi
fi


exit 0
