#!/bin/sh -e
# Copyright (C) @BABOLO ?? 2003 Jan 15
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: jailup,v 1.6 2003/04/17 03:01:12 babolo Exp $

if [ -z "${JAILUP_CONF}" ]; then
    export JAILUP_CONF=%%CONFDIR%%/jailup.conf
fi
. ${JAILUP_CONF}

case ${1} in
-v)
    export VERBOSE=yes
    shift
    ;;
esac

kind="${1}"

if  [ -z "${kind}" ]; then
    ${cat} << EOF
# jailup @BABOLO V.M %%VERS%%  %%DATE%%
#     Usage:
# jailup <kind> <other parameters>
# jailup <helper> <dir>
#     For help on <kind> use:
# jailup <kind>
#     For help on <helper> use:
# jailup <helper>
#     Some of <kind>s use directory created by:
# jailup devnull /null
#    List of kinds:
EOF
    for i in `${find} "${UPLIBDIR}" -maxdepth 1 -mindepth 1 -type d`; do
        ${find} ${i} -maxdepth 1 -mindepth 1 -type f -perm +0111 -name up\*\
      | ${sed} -e "s|^.*/\(`${basename} ${i}`\)/up\([^/]*\)\$|	\1\2|"\
      | ${sort}
    done
    ${cat} << EOF

#    List of helpers:
EOF
    for i in `${find} "${UPLIBDIR}" -maxdepth 1 -mindepth 1 -type d`; do
        ${find} ${i} -maxdepth 1 -mindepth 1 -type f -perm +0111 -name addon\*\
      | ${sed} -e "s|^.*/\(`${basename} ${i}`\)/addon\([^/]*\)\$|	\1\2|"\
      | ${sort}
    done
    ${cat} << EOF

#       Jailup configuration file: ${JAILUP_CONF}
#            Jail roots placed in: ${JAILUPDIR}/<dir>
# Jail's configurations placed in: ${JAILRODIR}/<dir>
#                  PREFIX used is: ${PREFIX}
#               Email of admin is: ${ADMINEMAIL}
#  Directory with startup scripts: ${STARTUPDIR}
#   Jailed services configuration: ${JAILUPRC}
EOF
    exit
fi
shift
if  [ -z "${1}" ]; then
    echo "# jailup @BABOLO V.M %%VERS%%  %%DATE%%"
fi

# Try a-b-c/up, a-b/up-c, a/up-b-c for kind a-b-c,
# so general kind and more specific kind can coexist

if  [ -x "${UPLIBDIR}/${kind}/up" ]; then
    exec "${UPLIBDIR}/${kind}/up" "$@"
elif [ -x "${UPLIBDIR}/${kind}/addon" ]; then
    exec "${UPLIBDIR}/${kind}/addon" "$@"
fi

count=$((`echo ${kind} | sed -e 's|[^-]*||g' | ${wc} -c` - 1))

case "${count}" in
0)
    ;;
*)
    while [ ${count} -gt 0 ]; do
        count=$((${count} - 1))
        exe=`echo ${kind} | ${sed} -Ee "s|^(([^-]+-){${count}}[^-]+)(-.+)$|\1/up\3|"`
        if  [ -x "${UPLIBDIR}/${exe}" ]; then
            exec "${UPLIBDIR}/${exe}" "$@"
        fi
        exe=`echo ${kind} | ${sed} -Ee "s|^(([^-]+-){${count}}[^-]+)(-.+)$|\1/addon\3|"`
        if  [ -x "${UPLIBDIR}/${exe}" ]; then
            exec "${UPLIBDIR}/${exe}" "$@"
        fi
    done
    ;;
esac

echo 'Illegal parameter' "${kind}"
exit 64
