#!/bin/sh -e
genclass=tftpd
suffix=
inclass=tftpd
. ${JAILUP_CONF}

if  [ -z "${1}" ]; then
    ${cat} << EOF
#     Usage:
# jailup tftpd jailname jailaddr dir
#     where dir relative to ${jailupdir} and ${jailrodir}
# tftpd from base system used
EOF
    exit
fi

jailsname="${1}"
jailsaddr="${2}"
jdir="${3}"
main_dir="${JAILUPDIR}/${jdir}"
conf_dir="${JAILRODIR}/${jdir}"
umask 022

${mkdir} "${main_dir}" "${conf_dir}/etc" "${conf_dir}/boot"
${up_mdir} "${main_dir}" ${MTREE}
${up_data} "${conf_dir}" << "EOF"
    /etc/host.conf
    /etc/hosts
    /etc/resolv.conf
    /etc/localtime
    /etc/protocols
EOF

${up_shlib} "${main_dir}/usr/lib" << "EOF"
    c crypt m util wrap
EOF

${up_libexe} "${main_dir}/usr/libexec" << EOF
    ld-elf.so.1
    tftpd
    xinetd
EOF

      #####################################
      ####                             ####
      #### Begin of etc fill           ####
      ####                             ####
if  [ ! -s "${conf_dir}/etc/xinetd" ]; then
    ${cat} << "EOF" > "${conf_dir}/etc/xinetd"
defaults
{
	log_type	= SYSLOG daemon
	log_on_success	= PID HOST DURATION
	log_on_failure	= HOST RECORD
}

service	tftp
{
	type		= UNLISTED
	protocol	= udp
	socket_type	= dgram
	port		= 69
	wait		= yes
	user		= root
	group		= nogroup
	server		= /usr/libexec/tftpd
	server_args	= -lu tftp -s /boot /r /w
	only_from	= 127.0.0.1
}
EOF
fi

if  [ ! -e "${conf_dir}/etc/group" ]; then
    ${cat} << "EOF" > "${conf_dir}/etc/group"
wheel:*:0:
tftp:*:99:
nogroup:*:65533:
EOF
fi

if  [ ! -e "${conf_dir}/etc/master.passwd" ]; then
    ${cat} << "EOF" > "${conf_dir}/etc/master.passwd"
root:*:0:0::0:0:root:/:/nonexistent
tftp:*:99:99::0:0:TFTP:/boot:/nonexistent
EOF
fi
      ####                             ####
      #### End of etc fill             ####
      ####                             ####
      #####################################

${mtree} -Ueqxp "${main_dir}" -f ${MTREE}
${mtree} -Ueqxp "${conf_dir}" << "EOF"
/set type=dir uname=root gname=wheel mode=0755 flags=none
.
    boot
    ..
    etc
/set type=file mode=0644
        host.conf
        hosts
        resolv.conf
        localtime
        protocols
        group
        xinetd
        master.passwd		mode=0600
    ..
..
EOF

${pwd_mkdb} -d "${conf_dir}/etc" -p "${conf_dir}/etc/master.passwd"

${cat} << EOF >> "${genconfig}"
#${genclass}	xinetd		${jdir}	${jailsname}		${jailsaddr}	xinetd
EOF

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