#!/bin/sh

# ugly: this variable is set during port install time
ezjail_prefix=EZJAIL_PREFIX
ezjail_jailcfgs=${ezjail_prefix}/etc/ezjail

if [ -f ${ezjail_prefix}/etc/ezjail.conf ]; then
  . ${ezjail_prefix}/etc/ezjail.conf;
fi

# set defaults
ezjail_jaildir=${ezjail_jaildir:-"/usr/jails"}
ezjail_jailtemplate=${ezjail_jailtemplate:-"$ezjail_jaildir/newjail"}
ezjail_jailbase=${ezjail_jailbase:-"$ezjail_jaildir/basejail"}
ezjail_jailfull=${ezjail_jailfull:-"$ezjail_jaildir/fulljail"}
ezjail_sourcetree=${ezjail_sourcetree:-"/usr/src"}

ezjail_mount_enable=${ezjail_mount_enable:-"YES"}
ezjail_devfs_enable=${ezjail_devfs_enable:-"YES"}
ezjail_devfs_ruleset=${ezjail_devfs_ruleset:-"devfsrules_jail"}
ezjail_procfs_enable=${ezjail_procfs_enable:-"YES"}
ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"}

# check for command
if [ -z "$1" ]; then
  echo "Usage: `basename $0` [create|delete|list|update] {params}";
  exit 1;
fi

case "$1" in
create)
  shift
  args=`getopt xr: $*`
  if [ $? != 0 ]; then
    echo 'Usage: ezjail create [-r jailroot] [-x] jailname jailip';
    exit 1;
  fi

  newjail_root=
  newjail_softlink=
  newjail_fill="YES"

  set -- $args
  for arg do
    case $arg in
      -x) newjail_fill="NO"; shift;;
      -r) newjail_root="$2"; shift 2;;
      --) shift; break;;
    esac
  done;
  newjail_name=$1;  newjail_ip=$2;

  # we need at least a name and an ip for new jail
  if [ -z "$newjail_name" -o -z "$newjail_ip" -o $# != 2 ]; then
    echo 'Usage: ezjail create [-r jailroot] [-x] jailname jailip'; exit 1;
  fi

  # check, whether IP is configured on a local interface, warn if it isnt
  ping -c 1 -m 1 -t 1 -q $newjail_ip > /dev/null
  if [ $? != 0 ]; then
    echo "Warning: IP $newjail_ip not configured on a local interface"
  fi

  # check, whether ezjail-update has been called. existence of
  # ezjail_jailbase is our indicator
  if [ ! -d $ezjail_jailbase ]; then
    echo "Error: base jail does not exist. Please run 'ezjail-admin update' first"
    exit 1;
  fi

  # relative paths don't make sense in rc.scripts
  if [ ${ezjail_jaildir#/} = ${ezjail_jaildir} ]; then
    echo "Error: Need an absolute path in ezjail_jaildir, it is currently  set to: $ezjail_jaildir"
    exit 1;
  fi

  # jail names must not have names that irritate file systems,
  # excluding dots from this list was done intentionally to
  # allow foo.com style directory names, however, the jail
  # name will be foo_com in most scripts
  newjail_name=`echo $newjail_name | tr /~ __`;
  newjail_root=${newjail_root:-"$ezjail_jaildir/$newjail_name"}
  newjail_nname=`echo $newjail_name | tr . _`;

  # if jail root specified on command line is not absolute,
  # make it absolute inside our jail directory
  if [ ${newjail_root#/} = ${newjail_root} ]; then
    newjail_root=$ezjail_jaildir/$newjail_root
  fi

  # if jail root specified on command line does not lie
  # within our jail directory, we need to create a softlink
  if [ ${newjail_root##${ezjail_jaildir}} = $newjail_root ]; then
    newjail_softlink=$ezjail_jaildir/`basename $newjail_root`
    if [ -e $newjail_softlink -a $newjail_fill = "YES" ]; then
      echo Error: an ezjail already exists at $newjail_softlink
      exit 1;
    fi
  fi

  # now take a copy of our template jail
  if [ $newjail_fill = "YES" ]; then
    mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} \
          && find * | cpio -p -v ${newjail_root}
  fi

  # if a soft link is necessary, create it now
  if [ $newjail_softlink ]; then
    ln -s $newjail_root $newjail_softlink
  fi

  # if the automount feature is not disabled, create an
  # fstab entry for new jail
  echo $ezjail_jailbase	$newjail_root/basejail	nullfs	ro	0	0 > /etc/fstab.$newjail_nname

  # now, where everything seems to have gone right,
  # create control file in ezjails config dir
  mkdir -p $ezjail_jailcfgs
  echo export jail_${newjail_nname}_hostname=\"${newjail_name}\"                 > ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_ip=\"${newjail_ip}\"                        >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\"                 >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_exec=\"/bin/sh /etc/rc\"                    >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_mount_enable=\"${ezjail_mount_enable}\"     >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_devfs_enable=\"${ezjail_devfs_enable}\"     >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_devfs_ruleset=\"devfsrules_jail\"           >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_procfs_enable=\"${ezjail_procfs_enable}\"   >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_fdescfs_enable=\"${ezjail_fdescfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}

  ;;
delete)
  shift
  args=`getopt w $*`
  if [ $? != 0 ]; then
    echo 'Usage: ezjail delete [-w] jailname';
    exit 1;
  fi
  
  oldjail_wipe="NO"
  
  set -- $args
  for arg do
    case $arg in
      -w) oldjail_wipe="YES"; shift;;
      --) shift; break;;
    esac
  done;
  oldjail_name=$1;

  # we only need name of jail to vanish
  if [ -z "$oldjail_name" -o $# != 1 ]; then
    echo 'Usage: ezjail delete [-w] jailname'; exit 1;
  fi

  # tidy up jail name the ezjail way
  oldjail_nname=`echo $oldjail_name | tr /~. ___`;

  # check for existence of jail in our records
  if [ ! -f ${ezjail_jailcfgs}/${oldjail_nname} ]; then
    echo "Error: Nothing known about jail $oldjail_name"; exit 1
  fi

  # fetch information about the jail to be gone
  # by parsing our records
  . ${ezjail_jailcfgs}/${oldjail_nname}
  eval oldjail_rootdir=\"\$jail_${oldjail_nname}_rootdir\"

  # now we know everything we need to let the jail be gone
  # remove entry from ezjail resource structure
  rm -f ${ezjail_jailcfgs}/${oldjail_nname}

  # delete fstab.JAILNAME
  rm -f /etc/fstab.$oldjail_nname

  # if there is a soft link pointing to the jail root, remove it
  oldjail_softlink=$ezjail_jaildir/`basename $oldjail_rootdir`
  if [ -L $oldjail_softlink ]; then
    rm $oldjail_softlink
  fi

  # if wiping the jail was requested, remove it
  if [ $oldjail_wipe = "YES" ]; then
    rm -rf $oldjail_rootdir
  fi

  ;;
list)
  jail_list=`ls $ezjail_jailcfgs`
  for jail in $jail_list; do
    . ${ezjail_jailcfgs}/$jail
    eval jail_ip=\"\$jail_${jail}_ip\"
    eval jail_hostname=\"\$jail_${jail}_hostname\"
    eval jail_rootdir=\"\$jail_${jail}_rootdir\"
    printf "%-15s %-28s %s\\n" $jail_ip $jail_hostname $jail_rootdir
  done
  ;;
update)
  shift
  args=`getopt is: $*`
  if [ $? != 0 ]; then
    echo 'Usage: ezjail update [-s sourcetree] [-i]';
    exit 1;
  fi

  updatejail_installaction="world"

  set -- $args
  for arg do
    case $arg in
      -i) updatejail_installaction="installworld"; shift;;
      -s) ezjail_sourcetree="$2"; shift 2;;
      --) shift; break;;
    esac
  done;

  if [ ! -d ${ezjail_sourcetree} ]; then
    echo "Cannot find your copy of the FreeBSD source tree in $ezjail_sourcetree."; exit 1;
  fi

  cd ${ezjail_sourcetree}
  rm -rf ${ezjail_jailfull}; mkdir -p ${ezjail_jailfull}
  make ${updatejail_installaction} DESTDIR=${ezjail_jailfull}
  make distribution DESTDIR=${ezjail_jailfull}

  cd ${ezjail_jailfull}
  mkdir -p ${ezjail_jailbase}/usr
  for a in bin sbin usr/bin usr/include usr/lib usr/libexec usr/sbin usr/src usr/share; do
    find ${a} | cpio -d -p -v ${ezjail_jailbase};
    chflags -R noschg ${a}; rm -r ${a}; ln -s /basejail/${a} ${a}
  done
  mkdir basejail
  ln -s /basejail/usr/ports usr/ports

  if [ -d ${ezjail_jailtemplate} ]; then
    rm -rf ${ezjail_jailtemplate}_old
    mv ${ezjail_jailtemplate} ${ezjail_jailtemplate}_old
  fi
  mv ${ezjail_jailfull} ${ezjail_jailtemplate}

  ;;
*)
  echo "Usage: `basename $0` [create|delete|list|update] {params}"; exit;
  ;;
esac