#! /bin/sh

KERNELPATH=/usr/src/linux

echo
echo This script partially removes the DIPC V1.1 software.
echo
if test `whoami` != root; then
 echo You should be the root to run this script.
 exit 1
else
 echo Press RETURN to continue, or Control-C to break...
 read
fi


#getting removal options
echo
echo -n Remove DIPC kernel patch \(Y/n\)?
read dokernel
if [ -z $dokernel ]; then dokernel='y'; fi
echo
echo -n Remove dipcd and its config files in the /etc directory \(Y/n\)?
read dodipcd
if [ -z $dodipcd ]; then dodipcd='y'; fi
echo
echo -n Remove DIPC tools in /usr/bin and /usr/X11/bin \(Y/n\)? 
read dotools
if [ -z $dotools ]; then dotools='y'; fi
echo
echo -n Delete DIPC man pages \(Y/n\)?
read doman
if [ -z $doman ]; then doman='y'; fi
echo
echo
echo -n Delete DIPC executables and object files \(Y/n\)?
read doobjs
if [ -z $doobjs ]; then doobjs='y'; fi
echo

if test $dokernel = 'y' -o $dokernel = 'Y'; then
 echo restoring old kernel files
 find /usr/src -name \*.pre_dipc | awk -F".pre_dipc" '{print "mv " $0 " " $1}' | /bin/sh
 echo
fi

if test $dodipcd = 'y' -o $dodipcd = 'Y'; then
 #checking to see if dipcd processes are running
 ps -ax >/tmp/dipc_check_tasks
 grep dipcd /tmp/dipc_check_tasks >/dev/null
 if test $? -eq 0; then
  echo 
  echo dipcd processes seem to be running. You could use the \'ps -ax\' command 
  echo to find them \(they are named \'/usr/sbin/dipcd\'\), and then use  
  echo \'kill \<pid\>\' to kill them one by one. If your system has the \'killall\' 
  echo command, you may run \'killall dipcd\'. Then run this script again.
  echo
  rm /tmp/dipc_check_tasks
  exit 1
 fi
 rm /tmp/dipc_check_tasks
 
 echo deleting config files in /etc directory...
 if test -s /etc/dipc.conf; then
  rm /etc/dipc.conf
 fi
 
 if test -s /etc/dipc.allow; then
  rm /etc/dipc.allow
 fi

 echo deleting /usr/sbin/dipcd
 if test -s /usr/sbin/dipcd; then
  rm /usr/sbin/dipcd
 fi 
fi


if test $dotools = 'y' -o $dotools = 'Y'; then
 echo deleting DIPC tools...

 cd tools
 for file in dipcker dipcref dipcshm 
 do
  if test -s /usr/bin/$file; then
   rm /usr/bin/$file
  fi
 done

 for file in dipcx 
 do
  if test -s /usr/X11/bin/$file; then
   rm /usr/X11/bin/$file
  fi
 done
 cd ..
fi

if test $doobjs = 'y' -o $doobjs = 'Y'; then
 for file in dipc.1 dipcker.1 dipcref.1 dipcshm.1 dipcx.1
 do
  if test -s /usr/man/man1/$file; then
   rm /usr/man/man1/$file
  fi
  if test -s /usr/man/man1/$file.gz; then
   rm /usr/man/man1/$file.gz
  fi
 done
 for file in dipc.conf.5 dipc.allow.5
 do
  if test -s /usr/man/man5/$file; then
   rm /usr/man/man5/$file
  fi
  if test -s /usr/man/man5/$file.gz; then
   rm /usr/man/man5/$file.gz
  fi
 done
 if test -s /usr/man/man8/dipcd.8; then
  rm /usr/man/man8/dipcd.8
 fi
 if test -s /usr/man/man8/dipcd.8.gz; then
  rm /usr/man/man8/dipcd.8.gz
 fi
fi

if test $doobjs = 'y' -o $doobjs = 'Y'; then
 #deleting dipcd
 cd dipcd
 if test -s dipcd; then
  echo cleaning dipcd/dipcd...   
  make clean
 fi
 cd ..
 echo

 #deleting the examples
 cd examples
 for file in hello image pi admin game
 do
  cd $file
  if test -s $file -o -s hello1; then
   echo cleaning examples/$file...
   make clean
  fi
  cd ..
 done
 cd ..
 echo

 #deleting the benchmarks
 cd benchmarks
 for prg in syscalls shmem
 do
  cd $prg
  if test -s sysbench -o -s shmbench; then
   echo cleaning benchmarks/$prg...
   make clean
  fi
  cd ..
 done
 cd ..
 echo


 #deleting the tools
 cd tools
 for file in dipcker dipcshm dipcref dipcx
 do  
  cd $file
  if test -s $file.o; then
   echo cleaning tools/$file...
   make clean
  fi
  cd ..
 done
 cd ..
fi
echo


if test $dodipcd = 'y' -o $dodipcd = 'Y'; then
 echo
 echo PLEASE MANUALLY DO THE FOLLOWING:
 echo *\) Remove the user with the name \'dipcd\'.
 echo *\) Edit file /etc/rc.d/rc.local to remove the dipcd invocation.
fi
if test $dokernel = 'y' -o $dokernel = 'Y'; then
 echo *\) Make and install the new kernel and reboot.
fi

echo
