#!/bin/bash
#
# This script lives at 
# <https://mo.joyent.com/mountain-gorilla/blob/master/tools/setup-build-zone>.
#
# This script automates part of the process of setting up a zone for SDC builds
# and getting all the dependent pieces.
#
shopt -s xpg_echo

#
# We are purposefully putting some of the GNU tools first... While this is
# usually not a good thing, it does make our lives a fair bit easier for what we
# have to do.
#
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/usr/sbin:/bin
export "PATH=$PATH"

sbz_arg0=$(basename $0)
sbz_fake_tgz=fake-subset.tbz2
sbz_fake_loc="/root/$sbz_fake_tgz"
sbz_fake_root="/opt"
sbz_fake_target="$sbz_fake_root/$sbz_fake_tgz"
sbz_fake_dir="$sbz_fake_root/fake-subset"
sbz_pkgsrc_dir="/opt/local"

sbz_pkgin_list="scmgit"

sbz_env_flag=


function fail
{
	local msg="$*"
	[[ -z "$msg" ]] && msg="failed"
	echo "$sbz_arg0: $msg" >&2
	exit 1
}

function usage
{
	local msg="$*"
	[[ -z "$msg" ]] && echo "$msg" >&2
	cat >&2 <<EOF
	Usage: $sbz_arg0 [ -e ]

Set up a provisioned zone for building illumos-live, illumos-joyent, and
illumos-extra. This sets up fake files, insures certain pkgsrc prequisites
are met, and handles crle and mounts.

	-e		Only set up mounts and crle. This should be used any
			time a zone has been rebooted after it has been
			prepared.
EOF
	exit 2
}

function fail_if_already_setup
{
	if [[ -n $(mount | grep fake-subset) ]]; then
		fail "Already setup."
	fi
}

#
# XXX We assume that fake-subset.tbz2 is in /root
#
function setup_fakes
{
	local usrdirs
	[[ -f $sbz_fake_loc ]] || fail "missing $sbz_fake_loc"
	cp $sbz_fake_loc $sbz_fake_target
	gtar xjf $sbz_fake_target -C $sbz_fake_root
	[[ -d $sbz_fake_dir/usr ]] || fail "fake files missing /usr"
	[[ $? -eq 0 ]] || fail "failed to extract fake files"
	gcp -a /usr $sbz_fake_dir
	[[ $? -eq 0 ]] || fail "failed to copy /usr"
	gfind /usr -type f \( -perm -04000 \) -exec chmod u+s $sbz_fake_dir/'{}' \;
	[[ $? -eq 0 ]] || fail "failed to fix suid on /usr"
	gfind /usr -type f \( -perm -02000 \) -exec chmod g+s $sbz_fake_dir/'{}' \;
	[[ $? -eq 0 ]] || fail "failed to fix sgid on /usr"
	return 0
}

#
# Take care of all the crle and mounting necessary. This is done as a separate
# function to facilitate itself as an eventual separate target
#
function setup_environment
{
	mount -O -F lofs $sbz_fake_dir/usr /usr
	[[ $? -eq 0 ]] || fail "failed to mount /usr"
	crle -u -l /usr/sfw/lib
	[[ $? -eq 0 ]] || fail "failed to add /usr/sfw/lib to crle"
	crle -64 -u -l /usr/sfw/lib/64
	[[ $? -eq 0 ]] || fail "failed to add /usr/sfw/lib/64 to crle"
	return 0
}

function install_packages
{
	pkgin -f update > /dev/null
	[[ $? -eq 0 ]] || fail "failed to update packages"
	pkgin -y in $sbz_pkgin_list > /dev/null
	[[ $? -eq 0 ]]  || fail "failed to install packages"
	return 0
}

while getopts "e" c $@; do
	case "$c" in
	e)
		sbz_env_flag=1
		;;
	*)
		usage "invalid option: $OPTARG"
		;;
	esac
done

fail_if_already_setup

if [[ $sbz_env_flag -ne 1 ]]; then
	echo "Setting up fake files ... \c "
	setup_fakes || fail "failed"
	echo "done."

	echo "Installing necessary packages ... \c "
	install_packages || fail "failed"
	echo "done."
fi

echo "Setting up environment ... \c "
setup_environment || fail "failed"
echo "done."
