#!/bin/bash

#
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2011, Joyent, Inc.
#

#
# This script automates setting up a zone 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=/usr/bin:/usr/sbin:/bin:/opt/local/bin:/opt/local/sbin
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_pkgin_q3root="http://pkgsrc.joyent.com/2010Q3/All/"
sbz_pkgin_q3list="glib2-2.24.2.tgz dbus-1.2.4.6.tgz dbus-glib-0.88.tgz"

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
}

#
# 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"
	[[ -d $sbz_fake_dir/lib ]] || fail "fake files missing /lib"
	[[ $? -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"
	gcp -a /lib $sbz_fake_dir
	gfind /lib -type f \( -perm -04000 \) -exec chmod u+s $sbz_fake_dir/'{}' \;
	[[ $? -eq 0 ]] || fail "failed to fix suid on /lib"
	gfind /lib -type f \( -perm -02000 \) -exec chmod g+s $sbz_fake_dir/'{}' \;
	[[ $? -eq 0 ]] || fail "failed to fix sgid on /lib"
	[[ $? -eq 0 ]] || fail "failed to copy /lib"
	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"
	mount -O -F lofs $sbz_fake_dir/lib /lib
	[[ $? -eq 0 ]] || fail "failed to mount /lib"
	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"

	#
	# Unfortuantely the current version of glib2 is pathologically busted in
	# future versions. As a temporary workaround we pull it from pkgsrc.
	# Note that eventually we should be able to build this from source
	# automatically as a part of a small set of necessary packages.
	#
	for pkg in $sbz_pkgin_q3list; do
		curl -f -s "$sbz_pkgin_q3root/$pkg" > $pkg
		[[ $? -eq 0 ]] || fail "failed to download $pkg"
		gtar xzf $pkg -C $sbz_pkgsrc_dir
		[[ $? -eq 0 ]] || fail "failed to extract $pkg"
		rm -f $pkg
	done
	#
	# Clean up extras
	#
	rm -f $sbz_pkgsrc_dir/+*
	return 0
}

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


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."
