#!/bin/sh
# Prints a listing of the installed 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

# Check if we have any jails
if [ ! -d "${JDIR}" ]
then
  echo "Error: No jails found!"
  exit 4
fi


# Prints a listing of the available jails
echo "IP		HOST		AUTOSTART	STATUS
--------------------------------------------------------------"

cd ${JDIR}

for i in `ls -d * 2>/dev/null`
do
  HOST="<unknown>"
  AUTO="Disabled" 
  STATUS="<unknown>"

  # Get the HostName
  if [ -e "${i}/etc/.wardenhost" ]
  then
    HOST="`cat ${i}/etc/.wardenhost`"
  fi

  # Check if we are autostarting this jail
  if [ -e "${i}/etc/.wardenautostart" ]
  then
    AUTO="Enabled"
  fi

  ${PROGDIR}/scripts/backend/checkstatus.sh ${i} 2>/dev/null
  if [ "$?" = "0" ]
  then
    STATUS="Running"
  else
    STATUS="Stopped"
  fi

  # Pad the variables a bit
  AUTO=`echo "${AUTO}          " | cut -c 1-15`
  STATUS=`echo "${STATUS}          " | cut -c 1-10`
  HOST=`echo "${HOST}          " | cut -c 1-15`
  

  echo -e "${i}	${HOST}	${AUTO}	${STATUS}"
done

