#!/bin/sh
#
# @package      hubzero-mw-service
# @file         mkvztemplate
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @author       John Rosheck <jrosheck@purdue.edu>
# @copyright    Copyright (c) 2008-2011 Purdue University. All rights reserved.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2008-2011 Purdue University
# All rights reserved.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of Purdue University.
#

ARCH=$1
DEBRELEASE=$2
HUBRELEASE=$3

if [ `/usr/bin/id -u` != 0 ]; then echo "Must be root to make and install the maxwell openvz templates"; exit 100; fi

if [[ "${DEBRELEASE}" != "etch" && "${DEBRELEASE}" != "lenny" && "${DEBRELEASE}" != "squeeze" ]]; then
	echo "Unrecognized or no debian release [${DEBRELEASE}] requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze] [sid|lenny|buck]"
	echo "  where options are architecture, debian release, and HUBzero release"
	exit 1
fi

if [[ "${ARCH}" != "i386" && "${ARCH}" != "amd64" ]]; then
	echo "Unrecognized or no architecture [${ARCH}] requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze] [sid|lenny|buck]"
	echo "  where options are architecture, debian release, and HUBzero release"
	exit 1
fi

if [[ "${HUBRELEASE}" != "lenny" && "${HUBRELEASE}" != "sid" && "${HUBRELEASE}" != "buck" ]]; then
	echo "Unrecognized or no HUBzero release [${HUBRELEASE}] requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze] [sid|lenny|buck]"
	echo "  where options are architecture, debian release, and HUBzero release"
	exit 1
fi

if [[ "${DEBRELEASE}" == "etch" ]]; then
	DEBVERSION=4.0
fi

if [[ "${DEBRELEASE}" == "lenny" ]]; then
	DEBVERSION=5.0
fi

if [[ "${DEBRELEASE}" == "squeeze" ]]; then
	DEBVERSION=6.0
fi

VZROOT=/var/lib/vz
VZTEMPLATEDIR=${VZROOT}/template
MYTEMPLATE=debian-${DEBVERSION}-${ARCH}-maxwell
MYTEMPLATEDIR=${VZTEMPLATEDIR}/${MYTEMPLATE}

if [ -f ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz ]; then
	echo "Template cache ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz already exists."
	exit 0;
fi

if [ ! -d ${VZTEMPLATEDIR} ]; then
	echo "Template directory does not exist: ${VZTEMPLATEDIR}"
	exit 2;
fi

if [ ! -d ${MYTEMPLATEDIR} ]; then

	mkdir -p ${MYTEMPLATEDIR}

	if [ ! -d ${MYTEMPLATEDIR} ]; then
        	echo "Unable to create template root directory: ${MYTEMPLATEDIR}"
        	exit 4;
	fi
fi

if [ ${ARCH} == "amd64" ]; then
	ARCHPKGS="ia32-libs"
else
	ARCHPKGS="amd64-libs,lib64gcc1,lib64ncurses5,lib64z1,libc6-amd64"
fi

/usr/sbin/debootstrap --arch ${ARCH} --exclude=laptop-detect,tasksel,tasksel-data,dhcp3-client,dhcp3-common --include=${ARCHPKGS},gawk,locales,openssh-client,rdist ${DEBRELEASE} ${MYTEMPLATEDIR}
/bin/cat <<EOF > ${MYTEMPLATEDIR}/etc/apt/sources.list
deb http://http.us.debian.org/debian/ ${DEBRELEASE} main contrib non-free
deb-src http://http.us.debian.org/debian/ ${DEBRELEASE} main contrib non-free
deb http://security.debian.org/ ${DEBRELEASE}/updates main contrib non-free
deb-src http://security.debian.org/ ${DEBRELEASE}/updates main contrib non-free
deb http://packages.hubzero.org/deb ${HUBRELEASE} main contrib non-free
EOF
wget http://packages.hubzero.org/deb/hubzero-signing-key.asc -O ${MYTEMPLATEDIR}/root/hubzero-signing-key.asc
echo "/usr/bin/apt-key add /root/hubzero-signing-key.asc" | chroot ${MYTEMPLATEDIR}
/bin/rm ${MYTEMPLATEDIR}/root/hubzero-signing-key.asc
/bin/sed -i -e '/getty/d' ${MYTEMPLATEDIR}/etc/inittab
/bin/sed -i -e 's/^root::/root:!:'/g ${MYTEMPLATEDIR}/etc/shadow
/bin/chmod 700 ${MYTEMPLATEDIR}/root
/bin/rm -f ${MYTEMPLATEDIR}/etc/mtab
/bin/ln -s /proc/mounts ${MYTEMPLATEDIR}/etc/mtab
/bin/cat << EOF > ${MYTEMPLATEDIR}/etc/locale.gen
en_US.UTF-8 UTF-8
en_US ISO-8859-1
EOF
echo "/usr/sbin/locale-gen" | chroot ${MYTEMPLATEDIR}
echo "apt-get update -y" | chroot ${MYTEMPLATEDIR}
echo "apt-get install -y xbase-clients xterm xfonts-base xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-scalable x-ttcidfont-conf icewm hubzero-mw-session hubzero-icewm hubzero-icewm-themes hubzero-use hubzero-chuse hubzero-icewm-captive hubzero-ratpoison-captive hubzero-twm-captive hubzero-submit-client" | chroot ${MYTEMPLATEDIR}
/bin/rm ${MYTEMPLATEDIR}/root/.bash_history
echo "localhost" > ${MYTEMPLATEDIR}/etc/hostname
echo "127.0.0.1       localhost" > ${MYTEMPLATEDIR}/etc/hosts
/bin/grep nameserver /etc/resolv.conf > ${MYTEMPLATEDIR}/etc/resolv.conf
find ${MYTEMPLATEDIR}/var/cache/apt/archives -maxdepth 1 -type f -name "*.deb" -exec /bin/rm {} \;
find ${MYTEMPLATEDIR}/var/cache/apt/archives/partial -maxdepth 1 -type f -name "*.deb" -exec /bin/rm {} \;
/bin/mkdir -p ${MYTEMPLATEDIR}/root/.ssh
/bin/chmod 700 ${MYTEMPLATEDIR}/root/.ssh
/bin/cat << EOF > ${MYTEMPLATEDIR}/etc/rc2.d/S15ssh_gen_host_keys
#!/bin/bash
/usr/bin/ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
/usr/bin/ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''
rm -f \$0
EOF
/bin/chmod a+x ${MYTEMPLATEDIR}/etc/rc2.d/S15ssh_gen_host_keys
/bin/cp /etc/localtime ${MYTEMPLATEDIR}/etc/localtime
/bin/cp /etc/timezone ${MYTEMPLATEDIR}/etc/timezone
/bin/tar -zcf ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz -C ${MYTEMPLATEDIR} .
#if [[ -d ${MYTEMPLATEDIR} && "${MYTEMPLATEDIR}" != "" && "${MYTEMPLATEDIR}" != "/" ]]; then
#/bin/rm -r ${MYTEMPLATEDIR}
#fi
echo "Done."
