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

IP="$1"
IFILE="$2"
PWD="`pwd`"

if [ -z "${IP}" ]
then
  echo "ERROR: No jail specified!"
  exit 5
fi

if [ -z "${IFILE}" ]
then
  echo "ERROR: No inmate file specified!"
  exit 5
fi

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

chk=`echo "${IFILE}" | cut -c 1-1`
if [ "$chk" != "/" ]
then
  IFILE="${PWD}/${IFILE}"
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

# Install the inmate now
#####################################################################

OS="`uname -r | cut -d '-' -f 1`"
ARCH="`uname -p`"


# 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

# Get the inmate variables
INAME="`cat inmate-name`"
export INAME
IVER="`cat inmate-version`"
export IVER

# Set the program variable with no spaces
INAMEVER="`cat inmate-name | tr -d ' '``cat inmate-version | tr -d ' '`"
export INAMEVER

# First check that we have the right architecture / OS
if [ "$OS" != "`cat inmate-OS`" ]
then
  echo "Warning: Inmate was created on FreeBSD `cat inmate-OS` currently running $OS"
fi

if [ "$ARCH" != "`cat inmate-platform`" ]
then
  echo "Warning: Inmate was created on `cat inmate-platform` currently running $ARCH"
fi

# Check if the jail is running first
${PROGDIR}/scripts/backend/checkstatus.sh "${IP}"
if [ "$?" != "0" ]
then
  # Make sure the jail is started
  ${PROGDIR}/scripts/backend/startjail.sh "${IP}" >/dev/null 2>/dev/null
fi


echo "Installing $INAME-$IVER into ${IP}..."

SKIP="`awk '/^___INMATE_START___/ { print NR + 1; exit 0; }' ${IFILE}`"

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

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

# Export our varibles for the scripts  
IDIR="/.tmp.$$" ; export IDIR
JLOC="${JDIR}/${IP}"
export IP JLOC

# Check if we have a preinstall-ext.sh script, and run it externally
if [ -e "${JDIR}/${IP}/.tmp.$$/preinstall-ext.sh" ]
then
  ${JDIR}/${IP}/.tmp.$$/preinstall-ext.sh
  if [ "$?" = "2" ]
  then

    # Remove the created dir in the jail
    rm -rf ${JDIR}/${IP}/.tmp.$$

    # Remove the tmp directory
    cd ${WTMP}
    rm -rf tmp.$$

    exit 2
  fi
fi

# Check if we have a preinstall.sh script, and run it in the jail
if [ -e "${JDIR}/${IP}/.tmp.$$/preinstall.sh" ]
then
  chroot ${JDIR}/${IP} /.tmp.$$/preinstall.sh 
  if [ "$?" = "2" ]
  then

    # Remove the created dir in the jail
    rm -rf ${JDIR}/${IP}/.tmp.$$

    # Remove the tmp directory
    cd ${WTMP}
    rm -rf tmp.$$

    exit 2
  fi
fi

# Now add any packages to the system
if [ -d "${JDIR}/${IP}/.tmp.$$/pkgs" ]
then

  echo '#!/bin/sh
cd /${IDIR}/pkgs
pkg_add -f *
mkdir -p /etc/inmates/${INAMEVER}
echo "${INAME} ${IVER}" > /etc/inmates/${INAMEVER}/inmate-name
ls *.tbz | sed "s/.tbz//g" > /etc/inmates/${INAMEVER}/pkgs
touch /${IDIR}/uninstall.sh
cp /${IDIR}/uninstall.sh /etc/inmates/${INAMEVER}/uninstall.sh
chmod 755 /etc/inmages/${INAMEVER}/uninstall.sh
' >${JDIR}/${IP}/.tmp.$$/inspkgs.sh
  chmod 755 ${JDIR}/${IP}/.tmp.$$/inspkgs.sh
  chroot ${JDIR}/${IP} /.tmp.$$/inspkgs.sh >/dev/null 2>/dev/null


fi

echo "Done"

# Check if we have a postinstall-ext.sh script, and run it externally
if [ -e "${JDIR}/${IP}/.tmp.$$/postinstall-ext.sh" ]
then
  ${JDIR}/${IP}/.tmp.$$/postinstall-ext.sh
fi

# Check if we have a postinstall.sh script, and run it in jail
if [ -e "${JDIR}/${IP}/.tmp.$$/postinstall.sh" ]
then
  chroot ${JDIR}/${IP} /.tmp.$$/postinstall.sh
fi


# Remove the created dir in the jail
rm -rf ${JDIR}/${IP}/.tmp.$$

# Remove the tmp directory
cd ${WTMP}
rm -rf tmp.$$



