#!/bin/sh

LANG="C"
export LANG

ARCH=`make -V MACHINE_ARCH`
#HOST=`hostname -s`
HOST=`hostname -s`"-${ARCH}"
target="${1}"
shift

setup_jail5() {
	D="${1}"
	if test ! -f "${D}/etc/rc.conf"
	then
		echo "$D"
		make distribution "DESTDIR=$D"
		( cd "${D}"
		ln -sf dev/null kernel )
		touch "$D/etc/fstab"
		cp -pi /etc/localtime "$D/etc/localtime"
		mkdir -p "$D/root/.ssh"
		cp -pi /root/.ssh/authorized_keys "$D/root/.ssh/authorized_keys"
		cp -pi /root/jail.rc.conf "$D/etc/rc.conf"
		touch "$D/etc/rc.conf"
		mkdir "$D/usr/ports"
	fi
}

setup_mail() {
	D="${1}"
	running=`jls -nq | grep "jid=.*${D}"`
	if test "${running}" = ""
	then
		chroot "${D}" /bin/sh -c "cd /etc/mail && make"
		chroot "${D}" /bin/sh -c "cd /etc/mail && make install"
	else
		jid="${running##*jid=}"
		jid="${jid%% *}"
		echo "${jid}"
		echo \
		jexec "${jid}" /bin/sh -c "cd /etc/mail && make"
		jexec "${jid}" /bin/sh -c "cd /etc/mail && make"
		echo \
		jexec "${jid}" /bin/sh -c "cd /etc/mail && make install"
		jexec "${jid}" /bin/sh -c "cd /etc/mail && make install"
	fi
}

install_jail() {
	D="${1}"
	E="${D##*/}"
	if test -L "${D}/usr/sbin/pkg"
	then
		rm -f "${D}/usr/sbin/pkg"
		mv "${D}/usr/sbin/pkg.orig" "${D}/usr/sbin/pkg"
	fi
	make "DESTDIR=${D}" installworld >> "err-${HOST}-installjails-${E}" 2>&1 || exit 1
	setup_jail5 "${D}"
	make "DESTDIR=${D}" delete-old -DBATCH_DELETE_OLD_FILES
	setup_mail "${D}"
	echo "${target} ${D} ok"
}

case "${target}" in
update)
	if test ! -d HISTORY
	then
		mkdir HISTORY
	fi
	mv -vf err* HISTORY/
	if test -e .svn
	then
		if test -e /usr/local/bin/svn
		then
			svn up | tee "err-${HOST}-${target}" 2>&1 || exit 1
		else
			svnlite up | tee "err-${HOST}-${target}" 2>&1 || exit 1
		fi
		echo "${target} ok"
	fi
	if test -e .git
	then
		if test -e /usr/local/bin/git
		then
			git pull | tee "err-${HOST}-${target}" 2>&1 || exit 1
		else
			gitlite up | tee "err-${HOST}-${target}" 2>&1 || exit 1
		fi
		echo "${target} ok"
	fi
	;;
buildworld)
	case "${1}" in
	esac
	make "${target}" "${@}" >> "err-${HOST}-${target}" 2>&1 || exit 1
	echo "${target} ok"
	;;
buildkernel)
	listfile="kernels-${HOST}.txt"
	if test -f "${listfile}"
	then
		for KERNCONF in `grep -v "^#" "${listfile}"`
		do
			echo "${target} ${KERNCONF}"
			make "${target}" "KERNCONF=${KERNCONF}" "${@}" >> "err-${HOST}-${target}-${KERNCONF}" 2>&1 || exit 1
			echo "${target} ok"
		done
		exit
	fi
	KERNCONF=`make -V KERNCONF`
	if test "${KERNCONF}" = ""
	then
		KERNCONF="GENERIC"
	fi
	make "${target}" "KERNCONF=${KERNCONF}" "${@}" >> "err-${HOST}-${target}-${KERNCONF}" 2>&1 || exit 1
	echo "${target} ok"
	;;
build)
	${0} buildworld "${@}" || exit 1
	${0} buildkernel "${@}" || exit 1
	;;
installkernel-pxe)
	target="${target%-pxe}"
	listfile="pxe-${HOST}.txt"
	if test ! -f "${listfile}"
	then
		exit 0
	fi
	KERNCONF="GENERIC"
	for D in `grep -v "^#" "${listfile}"`
	do
		echo "${D}"
		E="${D##*/}"
		make DESTDIR="${D}" KERNCONF="${KERNCONF}" "${target}" "${@}" >> "err-${HOST}-${target}-${E}-${KERNCONF}" 2>&1 || exit 1
	done
	echo "${target} ${D} ok"
	;;
installkernel)
	df -k /boot/ | tail -1 |
	while read mdef mblock mused mfree mdir
	do
		if test "${mfree}" -lt "380000"
		then
			rm -f /boot/kernel/*.symbols
		fi
	done
	KERNCONF=`make -V KERNCONF`
	if test "${KERNCONF}" = ""
	then
		KERNCONF="GENERIC"
	fi
	make "${target}" "${@}" >> "err-${HOST}-${target}-${KERNCONF}" 2>&1 || exit 1
	echo "${target} ok"
	${0} installkernel-pxe "${@}" || exit 1
	;;
installworld)
	make "${target}" "${@}" >> "err-${HOST}-${target}" 2>&1 || exit 1
	echo "${target} ok"
	make delete-old -DBATCH_DELETE_OLD_FILES "${@}"
	( cd /etc/mail && make )
	( cd /etc/mail && make install )
	;;
install)
	${0} installkernel "${@}" || exit 1
	${0} installworld "${@}" || exit 1
	;;
installjails)
	case "${1}" in
	*/*)
		install_jail "${@}"
		exit 0
		;;
	esac
	listfile="jails-${HOST}.txt"
	if test ! -f "${listfile}"
	then
		echo "${target}: File ${listfile} not found."
		exit 0
	fi
	for D in `grep -v "^#" "${listfile}"`
	do
		echo "${D}"
		install_jail "${D}" "${@}"
	done
	echo "${target} ok"
	;;
installworlds)
	${0} installworld "${@}" || exit 1
	${0} installjails "${@}" || exit 1
	;;
installall)
	${0} installkernel "${@}" || exit 1
	${0} installworld "${@}" || exit 1
	${0} installjails "${@}" || exit 1
	;;
mergemaster)
	# etcupdate
	mergemaster -U -F -i "${@}"
	while test $# -gt 0
	do
		case "${1}" in
		-D)
			;;
		*/*)
			setup_mail "${@}"
			;;
		esac
		shift
	done
	;;
mergemasterjails)
	case "${1}" in
	*/*)
		${0} mergemaster -D "${@}" || exit 1
		exit 0
		;;
	esac
	listfile="jails-${HOST}.txt"
	if test ! -f "${listfile}"
	then
		echo "${target}: File ${listfile} not found."
		exit 0
	fi
	for D in `grep -v "^#" "${listfile}"`
	do
		echo "${D}"
		${0} mergemaster -D "${D}" "${@}" || exit 1
	done
	echo "${target} ok"
	;;
mergemasterall)
	${0} mergemaster "${@}" || exit 1
	${0} mergemasterjails "${@}" || exit 1
	;;
delete-old-libs)
	make delete-old -DBATCH_DELETE_OLD_FILES "${@}"
	make delete-old-libs -DBATCH_DELETE_OLD_FILES "${@}"
	if test -e "jails-${HOST}.libs"
	then
		listfile="jails-${HOST}.libs"
	else
		listfile="jails-${HOST}.txt"
	fi
	if test ! -f "${listfile}"
	then
		exit 0
	fi
	for D in `grep -v "^#" "${listfile}"`
	do
		echo "${D}"
		make "DESTDIR=${D}" delete-old -DBATCH_DELETE_OLD_FILES "${@}"
		make "DESTDIR=${D}" delete-old-libs -DBATCH_DELETE_OLD_FILES "${@}"
	done
	;;
diff)
	for i in HISTORY/err-*
	do
		if ! test -f "${i}"
		then
			continue
		fi
		j="${i##*/}"
		if test -f "${j}"
		then
			continue
		fi
		echo "Only in HISTORY: ${j}"
	done
	;;
ver*)
	egrep '^(REVISION|BRANCH)' sys/conf/newvers.sh
	;;
*)
	echo "${0##*/} [ update | buildworld | buildkernel | build ]" >&2
	echo "${0##*/} [ installkernel | installworld | install ]" >&2
	echo "${0##*/} [ installjails | installworlds | installall ]" >&2
	echo "${0##*/} [ mergemaster | mergemasterjails | mergemasterall ]" >&2
	echo "${0##*/} [ delete-old-libs | version ]" >&2
	exit 64
	;;
esac
exit 0
# eof
