#!/bin/sh
# Deltes an inmate from a jail
######################################################################

# Source our functions
PROGDIR="`pbreg get /PC-BSD/TheWarden/ProgDir`"

if [ -z "${PROGDIR}" ]
then
   echo "PROGDIR unset! Is The Warden installed properly?"
   exit 155
fi

# Source our variables
. ${PROGDIR}/conf/warden.conf

IP="$1"
IPKG="$2"

if [ -z "${IP}" ]
then
  echo "ERROR: No jail specified!"
  exit 5
fi

if [ -z "${IPKG}" ]
then
  echo "ERROR: No inmate specified!"
  exit 5
fi

if [ -z "${JDIR}" ]
then
  echo "ERROR: JDIR is unset!!!!"
  exit 5
fi

if [ ! -d "${JDIR}/${IP}" ]
then
  echo "ERROR: No jail located at $JDIR/$IP"
  exit 5
fi


if [ ! -e "${JDIR}/${IP}/etc/inmates/${IPKG}/pkgs" ]
then
  echo "ERROR: Inmate does not exist: ${IPKG}!"
  exit 5
fi


# Delete the inmate now
#####################################################################

echo "Deleting Inmate: ${IPKG}..."

# Run the uninstall script if we need it
if [ ! -e "${JDIR}/${IP}/etc/inmates/${IPKG}/uninstall.sh" ]
then
  export IP
  chroot "${JDIR}/${IP}" "/etc/inmates/${INAMEVER}/uninstall.sh"
fi


echo "#!/bin/sh

while read line
do
  pkg_delete -f \$line
done < \"/etc/inmates/${IPKG}/pkgs\"
 rm -rf /etc/inmates/${IPKG}
" > "${JDIR}/${IP}/.indel.sh"

chmod 755 ${JDIR}/${IP}/.indel.sh
chroot "${JDIR}/${IP}" /.indel.sh >/dev/null 2>/dev/null
rm ${JDIR}/${IP}/.indel.sh

echo "Done"



