#!/bin/sh
# Script for user navigation of jails
#######################################################################

# 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



ls -d ${JDIR}/* >/dev/null 2>/dev/null
if [ "$?" != "0" ]
then
   echo "No jails installed! Please create a jail, or import one!"
   rtn
   exit 0
fi

AVAILJAILS=""

# Build our list of jails
cd ${JDIR}
for i in `ls -d *`
do
  if [ -e "${i}/etc/.wardenip" ]
  then
    AVAILJAILS="${AVAILJAILS} ${i} '`cat ${i}/etc/.wardenhost`' off"
  fi
done

# Ask the user which jail to work with
echo $AVAILJAILS > /tmp/.jaillist
echo "dialog --title \"The Warden\" --radiolist \"View which jail?\" 20 50 8 ${AVAILJAILS} back \"Main Menu\" off 2>/tmp/.jaillistout" > /tmp/.jaillist
sh /tmp/.jaillist
rm /tmp/.jaillist
ANS="`cat /tmp/.jaillistout`"

if [ "${ANS}" = "back" -o -z "${ANS}" ]
then
  exit 0
fi

# Get the jail IP / Host
IP="${ANS}"
HOST="`cat ${JDIR}/${IP}/etc/.wardenhost`"

# We have a jail to work with, lets bring up some details
while
 # Get Status of Jail here
 ${PROGDIR}/scripts/backend/checkstatus.sh "${IP}" 2>/dev/null
 if [ "$?" = "0" ]
 then
   JTOG="Stop Jail"
   JTOG2="0"
 else
   JTOG="Start Jail"
   JTOG2="1"
 fi

 # Check if this jail autostarts
 if [ -e "${JDIR}/${IP}/etc/.wardenautostart" ]
 then
   AUTOS="Disable Autostart" 
 else
   AUTOS="Enable Autostart" 
 fi

 # Display dialog of jail details
  dialog --title "The Warden" --menu "Working on $HOST (${IP})" 20 50 10 toggle "${JTOG}" shell "Open Shell Session" pkgs "View packages" inload "Install Inmate Package" export "Export the jail" auto "$AUTOS" delete "Delete Jail" back "Main Menu" 2>${ATMP}
  ANS="`cat ${ATMP}`"
  rm ${ATMP}
do

  case $ANS in
      back) exit 0 ;;

    # The user wants to log into this jail
     shell) ${PROGDIR}/scripts/backend/chrootjail.sh "${IP}"
            rtn ;;

     # The user wants to see what packages are installed in this jail
     pkgs) ${PROGDIR}/scripts/backend/listpkgs.sh "${IP}"
         rtn
         ;;
     
   # The user wants to install an inmate into this jail
   inload) # Prompt for the file we want to install
           dialog --title "The Warden" --inputbox "Please enter the path to a .wit file for installation." 14 45 2>${ATMP}
           IFILE=`cat ${ATMP}`
           rm ${ATMP}

           if [ ! -e "${IFILE}" ]
           then
              echo "ERROR: The file: ${IFILE} does not exist!"
              rtn
           else
             ${PROGDIR}/scripts/backend/installinmate.sh "${IP}" "${IFILE}"
           fi
         rtn
         ;;
    
    # The user wants to disable / enable autostart
    auto) if [ -e "${JDIR}/${IP}/etc/.wardenautostart" ]
          then
             rm "${JDIR}/${IP}/etc/.wardenautostart" 
          else
             touch "${JDIR}/${IP}/etc/.wardenautostart" 
          fi ;;
    # The user wants to start / stop this jail
    toggle) if [ "$JTOG2" = "0" ]
            then
              ${PROGDIR}/scripts/backend/stopjail.sh "${IP}" 
            else
              ${PROGDIR}/scripts/backend/startjail.sh "${IP}" 
            fi ;;


      export) ${PROGDIR}/scripts/backend/exportjail.sh "${IP}" 
              rtn
              ;;

      # The use asked to delete this jail
    delete) dialog --title "The Warden" --yesno "Are you sure you want to delete this jail?" 10 30
              if [ "$?" = "0" ]
              then 
                ${PROGDIR}/scripts/backend/deletejail.sh "${IP}" 
                rtn
                exit 0
              fi ;;
      *) ;;
  esac

done




