#!/bin/sh
# Script to stop a jail
# Args $1 = jail-dir
#######################################################################

# 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"

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

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

# Check if the jail is running first
${PROGDIR}/scripts/backend/checkstatus.sh "${IP}"
if [ "$?" = "0" ]
then
  # Make sure the jail is stopped
  ${PROGDIR}/scripts/backend/stopjail.sh "${IP}"
fi

echo -e "Deleting Jail...\c"
chflags -R noschg "${JDIR}/${IP}"
rm -rf "${JDIR}/${IP}"
echo "Done"

