#!/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: up,v 1.10 2003/04/30 00:35:26 babolo Exp $

genclass=apache
suffix=
inclass=apache-ssl
. ${JAILUP_CONF}
kind=`${dirname} "${0}"`
kind=`${basename} "${kind}"`
t=`${basename} "${0}"`
if  [ "${t}" != "${t#*-}" ]; then
    kind="${kind}/-${t#*-}"
fi

if  [ -z "${1}" ]; then
    ${cat} << EOF
# jailup ${kind} @BABOLO V.M %%VERS%%  %%DATE%%
#     Usage:
# jailup apache-ssl dir jailname jailaddr [user [group [port]]]
#     where dir relative to ${JAILUPDIR} and ${JAILRODIR}
#     default user=0, default group=0
# installed ports/www/apache13-modssl used
EOF
    if  [ "${ADMINEMAIL}" = "some@host" ]; then
        ${cat} << EOF
# Please tune ADMINEMAIL in ${JAILUP_CONF}
EOF
    fi
    exit
fi

jdir="${1}"
jailsname="${2}"
jailsaddr="${3}"
user="${4:-0}"
group="${5:-0}"
port="${6:-80}"
main_dir="${JAILUPDIR}/${jdir}"
conf_dir="${JAILRODIR}/${jdir}"
umask 022

${mkdir} "${main_dir}" "${conf_dir}/apache/ssl.crt" "${conf_dir}/apache/ssl.key"
${up_mdir} "${main_dir}" ${MTREE}
${up_genlist} www/icons/ ${MTREE} | ${up_data} "${main_dir}/www/icons/" ${PREFIX}/www/icons/
echo "localtime" | ${up_data} "${conf_dir}/" /etc/

${up_conf} "${conf_dir}/" /etc/ << "EOF"
    host.conf
    hosts
    protocols
    resolv.conf
EOF

${up_conf} "${conf_dir}/apache/" ${PREFIX}/etc/apache/ << "EOF"
    magic
    mime.types
    ssl.crt/server.crt
    ssl.key/server.key
EOF
#   access.conf
#   srm.conf

${up_shlib} "${main_dir}/usr/lib" << "EOF"
    c crypt mm ssl crypto
EOF

${up_libexe} "${main_dir}/usr/libexec" "${PREFIX}/libexec/apache" << EOF
    ld-elf.so.1
    httpd
    `${up_genlist} usr/libexec/ ${MTREE}`
EOF

      #####################################
      ####                             ####
      #### Begin of etc fill           ####
      ####                             ####
if  [ ! -e "${conf_dir}/group" ]; then
    ${cat} << "EOF" > "${conf_dir}/group"
wheel:*:0:root
nogroup:*:65533:
nobody:*:65534:
www:*:80:
EOF
fi

if  [ ! -e "${conf_dir}/master.template" ]; then
    ${cat} << "EOF" > "${conf_dir}/master.template"
root:*:0:0::0:0:Charlie &:/:/nonexistent
nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/nonexistent
www:*:80:80::0:0:WWW:/www:/nonexistent
EOF
fi

if  [ ! -e "${conf_dir}/master.passwd" ]; then
    ${cp} -p "${conf_dir}/master.template" "${conf_dir}/master.passwd"
fi
      ####                             ####
      #### End of etc fill             ####
      ####                             ####
      #####################################

cf="apache/httpd.conf"; if  [ -e "${conf_dir}/${cf}" ]; then cf="${cf}.sample"; fi
${cp} -f "${TEMPLATE}" "${conf_dir}/${cf}"
cf="apache/httpsd.conf"; if  [ -e "${conf_dir}/${cf}" ]; then cf="${cf}.sample"; fi
${cp} -f "${TEMPLATE}-ssl" "${conf_dir}/${cf}"

${mtree} -Ueqxp "${main_dir}" -f ${MTREE}
${mtree} -Ueqxp "${conf_dir}" << "EOF"
/set type=file uname=root gname=wheel mode=0644 flags=none
.			type=dir mode=0755
    group
    host.conf
    hosts
    localtime
    master.passwd	mode=0600
    master.template	mode=0600
    protocols
    resolv.conf
    apache		type=dir mode=0755
#       access.conf
        httpd.conf
        httpsd.conf
        magic
        mime.types
#       srm.conf
        ssl.crt		type=dir mode=0755
            server.crt	mode=0400
        ..
        ssl.key		type=dir mode=0700
            server.key	mode=0400
        ..
    ..
..
EOF
${pwd_mkdb} -d "${conf_dir}" -p "${conf_dir}/master.passwd"

${cat} << EOF >> "${genconfig}"
#${genclass}	${inclass}	${jdir}	${jailsname}	${jailsaddr}	0	${user}	${group}	${port}	${ADMINEMAIL}
#${genclass}	${genclass}	${jdir}	${jailsname}	${jailsaddr}	0	${user}	${group}	${port}	${ADMINEMAIL}
EOF

      #####################################
      ####                             ####
      #### Begin of fstab mod          ####
      ####                             ####
${cat} << EOF >> "${etcdir}/fstab"
#/null			${main_dir}/dev	null	ro,noexec	0 0
#${conf_dir}		${main_dir}/etc	null	ro,nodev,noexec	0 0
EOF
      ####                             ####
      #### End of fstab mod            ####
      ####                             ####
      #####################################
