#!/bin/sh
# Script to startup 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 start!"
  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

# Make sure the jail is NOT already running
jls | grep ${IP} >/dev/null 2>/dev/null
if [ "$?" = "0" ]
then
  echo "ERROR: Jail appears to be running already!"
  exit 6
fi


HOST="`cat ${JDIR}/${IP}/etc/.wardenhost`"

ifconfig $NIC inet alias ${IP}/32
mount -t devfs devfs "${JDIR}/${IP}/dev"
mount -t procfs proc "${JDIR}/${IP}/proc"
jail ${JDIR}/${IP} ${HOST} ${IP} /bin/sh /etc/rc 2>&1

# Get the JailID for this new jail
JID="`jls | grep ${IP} | tr -s " " ":" | cut -d ":" -f 2`"
echo "$JID" >"${JDIR}/${IP}/var/run/warden.jid"




