#!/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

IFILE="$1"
IP="${2}"
HOST="${3}"


if [ -z "${IFILE}" -o ! -e "${IFILE}" ]
then
  echo "ERROR: No jail specified or invalid file!"
  exit 5
fi

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

if [ "${IP}" != "OFF" ]
then
  # Make sure we don't have a jail with this new IP already
  if [ -e "${JDIR}/${IP}" ]
  then
    echo "ERROR: A Jail exists with IP: $IP"
    exit 5
  fi

fi

# Lets start importing the jail now
######################################################################


chk=`echo "${IFILE}" | cut -c 1-1`
if [ "$chk" != "/" ]
then
  IFILE="`pwd`/${IFILE}"
fi


# Extract the header info of the file
cd ${WTMP}
rm -rf tmp.$$ >/dev/null
mkdir tmp.$$
cd tmp.$$

tar xvzf ${IFILE} >/dev/null 2>/dev/null
if [ "${?}" != "0" ]
then
    echo "ERROR: Extracting header info failed! "
    cd ..
    rm -rf tmp.$$
    exit 5
fi

# Blank our variables and read in the header information
VER=""
OS=""
FILES=""
FIP=""
FHOST=""

HEADER=`ls *.header`

while read line
do
  #Check for the file version
  echo "$line" | grep "Ver:" >/dev/null 2>/dev/null
  if [ "$?" = "0" ]
  then
    VER="`echo $line | cut -d ' ' -f 2-10`"
  fi

  # Check for the OS Platform
  echo "$line" | grep "OS:" >/dev/null 2>/dev/null
  if [ "$?" = "0" ]
  then
    OS="`echo $line | cut -d ' ' -f 2-10`"
  fi

  # Check for the File Number 
  echo "$line" | grep "Files:" >/dev/null 2>/dev/null
  if [ "$?" = "0" ]
  then
    FILES="`echo $line | cut -d ' ' -f 2-10`"
  fi
  
  # Check for the built in IP
  echo "$line" | grep "IP:" >/dev/null 2>/dev/null
  if [ "$?" = "0" ]
  then
    FIP="`echo $line | cut -d ' ' -f 2-10`"
  fi
  
  # Check for the built in HOST
  echo "$line" | grep "HOST:" >/dev/null 2>/dev/null
  if [ "$?" = "0" ]
  then
    FHOST="`echo $line | cut -d ' ' -f 2-10`"
  fi
  
done < $HEADER 

cd ..
rm -rf tmp.$$ 2>/dev/null

# Make sure this is a file version we understand
if [ "${VER}" != "1.0" ]
then
    echo "ERROR: Specified file is a incompatiable .wdn file!"
    exit 7
fi

# Check that we are on the same OS platform
if [ "${OS}" != "`uname -r | cut -d '-' -f 1`" ]
then
    echo "WARNING: This .wdn file was created on $OS, while this host is `uname -r | cut -d '-' -f 1`"
    echo "This jail may not work...Importing anyway..."
fi


if [ "${IP}" = "OFF" ]
then
  # Make sure we don't have a jail with this new IP already
  if [ -e "${JDIR}/${FIP}" ]
  then
    echo "ERROR: A Jail already exists with IP: $FIP"
    exit 7
  fi
 
  # The user didn't specify a new IP, so use the built in one
  IP="${FIP}"
fi

SKIP="`awk '/^___WARDEN_START___/ { print NR + 1; exit 0; }' ${IFILE}`"
echo "Importing ${IFILE} with IP: ${IP}..."

# Make the new directory
mkdir -p "${JDIR}/${IP}"

# Extract the jail contents
tail +${SKIP} ${IFILE} | lzma -d -c 2>/dev/null | tar xpf - -C "${JDIR}/${IP}" 2>/dev/null

# Make sure we have an IP address saved
echo "${IP}" >"${JDIR}/${IP}/etc/.wardenip"

# Create the host for this device
if [ "${HOST}" != "OFF" -a ! -z "${HOST}" ]
then
  # Save the details to the .wardenhost file
  echo "${HOST}" >"${JDIR}/${IP}/etc/.wardenhost"

  # Change the hostname in rc.conf
  cat "${JDIR}/${IP}/etc/rc.conf" | grep -v "hostname=" >${JDIR}/${IP}/.rc.conf
  echo "hostname=\"${HOST}\"" >>"${JDIR}/${IP}/.rc.conf"
  mv "${JDIR}/${IP}/.rc.conf" "${JDIR}/${IP}/etc/rc.conf"

# Setup /etc/hosts now
echo "# : src/etc/hosts,v 1.16 2003/01/28 21:29:23 dbaker Exp $
#
# Host Database
#
# This file should contain the addresses and aliases for local hosts that
# share this file.  Replace 'my.domain' below with the domainname of your
# machine.
#
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
#
#
::1                     localhost localhost.localdomain
127.0.0.1               localhost localhost.localdomain ${HOST}
${IP}			${HOST}" > "${JDIR}/${IP}/etc/hosts"

# End Hostname setup
fi

echo "Done"
