#!/bin/sh -e
#
# 2012 Jake Guffey (jake.guffey at eprotex.com)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# The __jail type creates, configures, and deletes FreeBSD jails for use as
#  virtual machines.
#

jaildir="$(cat "$__object/parameter/jaildir")"

jailbase="$(cat "$__object/parameter/jailbase")"

state="$(cat "$__object/parameter/state")"

if [ "$state" = "present" ] && [ -z "$jailbase" ]; then
   exec >&2
   echo "jailbase is a REQUIRED parameter when state=present!"
   exit 1
fi

remotebase="${jaildir}/jailbase.tgz"
basepresent="$(cat "$__object/explorer/basepresent")"

if [ "$state" = "present" ]; then
   if [ "$basepresent" = "NONE" ]; then
     # IPv6 fix
     if echo "${__target_host}" | grep -q -E '^[0-9a-fA-F:]+$'
     then
         my_target_host="[${__target_host}]"
     else
         my_target_host="${__target_host}"
     fi
      echo "$__remote_copy" "${jailbase}" "${my_target_host}:${remotebase}"
   fi   # basepresent=NONE
fi   # state=present

