#!/bin/sh
# Install a package set into 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

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

# Prompt for the file we want to import
dialog --title "The Warden" --inputbox "Please enter the path to a .wdn file for importing." 14 45 2>${ATMP}
IFILE=`cat ${ATMP}`
rm ${ATMP}

if [ ! -e "${IFILE}" ]
then
   echo "ERROR: The file: ${IFILE} does not exist!"
   rtn
   exit 2
fi


# Assign a new IP to this jail?
IP="OFF"
dialog --title "The Warden" --yesno "Assign a new IP to this jail?" 7 40
if [ "$?" = "0" ]
then
  # Prompt for an IP for this jail
  dialog --title "The Warden" --inputbox "Please enter a IP address for this jail:" 15 35 2>${ATMP}
  IP=`cat ${ATMP}`
  rm ${ATMP}

  if [ -e "${JDIR}/${IP}" ]
  then
    echo "ERROR: A jail with this IP already exists!"
    rtn
    exit 2
  fi

  # Do a sanity check on the IP address
  check_ip $IP
  if [ "$?" != "0" ]
  then
    echo "ERROR: IP address is invalid!"
    rtn
    exit 2
  fi

fi


# Assign a new host to this jail?
HOST="OFF"
dialog --title "The Warden" --yesno "Assign a new hostname to this jail?" 7 40
if [ "$?" = "0" ]
then
  # Prompt for a hostname for this jail
  dialog --title "The Warden" --inputbox "Please enter a Hostname for this jail:" 15 35 2>${ATMP}
  HOST=`cat ${ATMP}`
  rm ${ATMP}
fi

# Import the jail now
${PROGDIR}/scripts/backend/importjail.sh "${IFILE}" "${IP}" "${HOST}"
rtn

exit 0
