#!/bin/sh
set -e

# Automatically added by dh_installdocs
if [ \( "$1" = "upgrade" -o "$1" = "remove" \) -a -L /usr/doc/pwrtest ]; then
        rm -f /usr/doc/pwrtest
fi
# End automatically added section

proot="/pwrp"
aroot="/usr/pwrp/adm"

echo ""
echo -n "Do you wan't to remove project and users (y/n) [n] "
read remove_all

if [ "$remove_all" = "y" ]; then
  
  # Check that any user that is to be remoted isn't logged in
  set +e
  user_found=0
  currentusers=`eval users`
  userstr=""
  checkuser=`eval echo $currentusers | grep "\bb55\b"`
  if [ "$checkuser" != "" ]; then
    user_found=1
    userstr=$userstr" b55"
  fi
  checkuser=`eval echo $currentusers | grep "\bb66\b"`
  if [ "$checkuser" != "" ]; then
    user_found=1
    userstr=$userstr" b66"
  fi
  checkuser=`eval echo $currentusers | grep "\bpwrp\b"`
  if [ "$checkuser" != "" ]; then
    user_found=1
    userstr=$userstr" pwrp"
  fi
  checkuser=`eval echo $currentusers | grep "\bskiftel\b"`
  if [ "$checkuser" != "" ]; then
    user_found=1
    userstr=$userstr" skiftel"
  fi

  if [ $user_found -eq 1 ]; then
    echo "-- Current users: $currentusers"
    echo "** Remove user will fail. Logout user $userstr first."
    exit -1;
  fi
  set -e

  echo "-- Remove user b55"
  if grep -q "\bb55:" /etc/passwd; then
    userdel b55
  fi
  if [ -e /home/b55 ]; then
    rm -r /home/b55
  fi

  echo "-- Remove user b66"
  if grep -q "\bb66:" /etc/passwd; then
    userdel b66
  fi
  if [ -e /home/b66 ]; then
    rm -r /home/b66
  fi

  echo "-- Remove user pwrp"
  if grep -q "\bpwrp:" /etc/passwd; then
    userdel pwrp
  fi
  if [ -e /home/pwrp ]; then
    rm -r /home/pwrp
  fi

  echo "-- Remove user skiftel"
  if grep -q "\bskiftel:" /etc/passwd; then
    userdel skiftel
  fi
  if [ -e /home/skiftel ]; then
    rm -r /home/skiftel
  fi

  if grep -q "\bb55:" /etc/group; then
    groupdel b55
  fi

  if grep -q "\bb66:" /etc/group; then
    groupdel b66
  fi

  if grep -q "\bpwrp:" /etc/group; then
    groupdel pwrp
  fi

  if grep -q "\bskiftel:" /etc/group; then
    groupdel skiftel
  fi

  echo "-- Remove project $proot"
  if [ -e $proot ]; then
    rm -r $proot
  fi

  if [ -e $aroot ]; then
    rm -r $aroot
  fi
fi

# Remove startup
checklink=`eval ls /etc/rc2.d/S90pwr 2>/dev/null`
if [ "$checklink" != "" ]; then
  rm /etc/rc2.d/S90pwr
fi

# Remove jar-files on web directory
if [ -e /etc/proview.cnf ]; then
  web_dir=`eval cat /etc/proview.cnf | grep "\\bwebDirectory\\b" | awk '{print $2}'`

  if [ -e $web_dir/pwr_rt_client.jar ]; then
    rm $web_dir/pwr_rt_client.jar
  fi
  if [ -e $web_dir/pwr_jop.jar ]; then
    rm $web_dir/pwr_jop.jar
  fi
fi