#! /bin/sh
set -e

rm -rf config

SEEDMIRROR=http://people.canonical.com/~ubuntu-archive/seeds/
if [ -z "$MIRROR" ]; then
	case $(hostname --fqdn) in
		bld-*.mmjgroup.com)	;;
		*.mmjgroup.com)
			case $ARCH in
				i386|amd64)	MIRROR=http://archive.mmjgroup.com/ubuntu/ ;;
				*)		MIRROR=http://archive.mmjgroup.com/ubuntu-ports/ ;;
			esac
			;;
		*.0c3.net)
			case $ARCH in
				i386|amd64)	MIRROR=http://mirrors.0c3.net/ubuntu/ ;;
				*)		MIRROR=http://mirrors.0c3.net/ubuntu-ports/ ;;
			esac
			;;
		*.ubuntu.com)		MIRROR=http://ftpmaster.internal/ubuntu/
					SEEDMIRROR=http://archive-team.internal/seeds/
					;;
		*.warthogs.hbd.com)	MIRROR=http://ftpmaster.internal/ubuntu/
					SEEDMIRROR=http://archive-team.internal/seeds/
					;;
		*.buildd)		MIRROR=http://ftpmaster.internal/ubuntu/
					SEEDMIRROR=http://archive-team.internal/seeds/
					;;
	esac
fi

mkdir -p config/task-lists config/package-lists

add_task ()
{
	local pass="$1"
	shift
	local task

	for task; do
		echo "$task" >> "config/task-lists/livecd-rootfs.chroot_$pass.list"
	done
}

add_package ()
{
	local pass="$1"
	shift
	local pkg

	for pkg; do
		echo "$pkg" >> "config/package-lists/livecd-rootfs.chroot_$pass.list"
	done
}

OPTS=
COMPONENTS=
BINARY_REMOVE_LINUX=:
BINARY_IMAGES=none
MEMTEST=none
SOURCE='--source false'
BOOTLOADER=none
LIVE_TASK=
PREINSTALLED=false
PREINSTALL_POOL=
PREINSTALL_POOL_SEEDS=

case $IMAGEFORMAT in
	ext2|ext3|ext4)
		OPTS="${OPTS:+$OPTS }--initramfs none --chroot-filesystem $IMAGEFORMAT"
		PREINSTALLED=true
		case $SUBPROJECT in
			wubi)
				add_package install lupin-support
				COMPONENTS='main restricted universe multiverse'
				;;
			*)
				add_package live jasper
				OPTS="${OPTS:+$OPTS }--swap-file-path SWAP.swap"
				OPTS="${OPTS:+$OPTS }--swap-file-size 512"
				;;
		esac
		;;

	plain)
		OPTS="${OPTS:+$OPTS }--initramfs none --chroot-filesystem $IMAGEFORMAT"
		PREINSTALLED=true
		;;

	*)
		add_package live lupin-casper
		;;
esac

if [ "$PREINSTALLED" = "true" ] && [ "$SUBPROJECT" != "wubi" ]; then
	case $PROJECT in
		kubuntu*)
			add_package live oem-config-kde ubiquity-frontend-kde
			;;
		ubuntu-server)
			add_package live oem-config-debconf ubiquity-frontend-debconf
			;;
		ubuntu-core|base)
			;;
		*)
			add_package live oem-config-gtk ubiquity-frontend-gtk
			;;
	esac
fi

case $BINARYFORMAT in
	iso*|usb*)
		BINARY_IMAGES="$BINARYFORMAT"
		MEMTEST=memtest86+
		BOOTLOADER=syslinux
		;;
esac

case $PROJECT in
	ubuntu|ubuntu-dvd)
		add_task install minimal standard ubuntu-desktop
		LIVE_TASK='ubuntu-live'
		;;

	kubuntu|kubuntu-dvd)
		add_task install minimal standard kubuntu-desktop
		LIVE_TASK='kubuntu-live'
		;;

	kubuntu-mobile)
		add_task install minimal standard kubuntu-mobile
		LIVE_TASK='kubuntu-mobile-live'
		COMPONENTS='main restricted universe'
		;;

	edubuntu|edubuntu-dvd)
		add_task install minimal standard edubuntu-desktop-gnome
		LIVE_TASK='edubuntu-live'
		COMPONENTS='main restricted universe'
		;;

	xubuntu)
		add_task install minimal standard xubuntu-desktop
		add_package install xterm
		LIVE_TASK='xubuntu-live'
		COMPONENTS='main restricted universe multiverse'
		;;

	ubuntu-netbook)
		add_task install minimal standard ubuntu-netbook
		LIVE_TASK='netbook-live'
		;;

	mythbuntu)
		add_task install minimal standard mythbuntu-desktop
		LIVE_TASK='mythbuntu-live'
		COMPONENTS='main restricted universe multiverse'
		;;

	lubuntu)
		add_task install minimal standard lubuntu-desktop
		LIVE_TASK='lubuntu-live'
		COMPONENTS='main restricted universe multiverse'
		;;

	ubuntustudio-dvd)
		add_task install ubuntustudio-desktop ubuntustudio-generation ubuntustudio-recording ubuntustudio-font-meta ubuntustudio-graphics ubuntustudio-audio-plugins ubuntustudio-video
		COMPONENTS='main restricted universe multiverse'
		;;

	base)
		add_task install minimal standard
		;;

	ubuntu-server)
		add_task install minimal standard
		PREINSTALL_POOL_SEEDS='server-ship'
		;;

	ubuntu-core)
		OPTS="${OPTS:+$OPTS }--bootstrap-flavour=minimal"
		OPTS="${OPTS:+$OPTS }--linux-packages=none"
		KERNEL_FLAVOURS=none
		BINARY_REMOVE_LINUX=false
		;;

	*)
		echo "unknown project $PROJECT" >&2
		exit 2
		;;
esac

if [ "$PREINSTALLED" != "true" ]; then
	add_task live "$LIVE_TASK"
fi

case $PROJECT in
	ubuntu-dvd)
		add_task install ubuntu-usb
		add_task live ubuntu-usb-live
		;;

	*-dvd)
		add_task live "$PROJECT-live"
		;;
esac

case $ARCH in
	armel|armhf)
		KERNEL_FLAVOURS="${SUBARCH:-$KERNEL_FLAVOURS}"
		case $SUBARCH in
			dove)
				BINARY_REMOVE_LINUX=false
				;;
			omap)
				add_package install u-boot-linaro-omap3-beagle
				BINARY_REMOVE_LINUX=false
				;;
			omap4)
				add_package install u-boot-linaro-omap4-panda ti-omap4-ppa
				BINARY_REMOVE_LINUX=false
				;;
			mx5)
				COMPONENTS='main restricted universe'
				KERNEL_FLAVOURS=linaro-lt-mx5
				add_package install flash-kernel u-boot-linaro-mx53loco
				BINARY_REMOVE_LINUX=false
				;;
			ac100)
				COMPONENTS='main restricted universe'
				add_package install ac100-tarball-installer zram-config
				BINARY_REMOVE_LINUX=false
				PREINSTALL_POOL_SEEDS=""
				;;
		esac
		;;
esac

lb config noauto \
	--mode ubuntu \
	--distribution "$SUITE" \
	--bootstrap-keyring ubuntu-keyring \
	--binary-images "$BINARY_IMAGES" \
	--memtest "$MEMTEST" \
	$SOURCE \
	--build-with-chroot false \
	${MIRROR:+--parent-mirror-bootstrap $MIRROR} \
	${COMPONENTS:+--parent-archive-areas "$COMPONENTS"} \
	--package-lists none \
	${KERNEL_FLAVOURS:+--linux-flavours "$KERNEL_FLAVOURS"} \
	--initsystem none \
	--bootloader "$BOOTLOADER" \
	--initramfs-compression lzma \
	$OPTS

if $BINARY_REMOVE_LINUX; then
	cat > config/binary_rootfs/excludes << EOF
boot/vmlinu?-*
boot/initrd.img-*
EOF
fi

ln -s /usr/share/live/build/examples/hooks/all_chroot_pyc-purge.sh config/chroot_local-hooks/

case $PROJECT in
	kubuntu|kubuntu-mobile)
		ln -s /usr/share/live/build/examples/hooks/kubuntu_chroot_icon-theme.sh config/chroot_local-hooks/
		;;
esac

case $SUBPROJECT in
	wubi)
		ln -s /usr/share/live/build/examples/hooks/ubuntu_binary_build-wubildr.sh config/binary_local-hooks/
		;;
esac

case $PROJECT in
	*-dvd)
		. config/bootstrap

		cat > config/archives/dvd.binary.list << EOF
deb $LB_PARENT_MIRROR_BINARY $SUITE universe multiverse
deb $LB_PARENT_MIRROR_BINARY_SECURITY $SUITE-security universe multiverse
deb $LB_PARENT_MIRROR_BINARY_VOLATILE $SUITE-updates universe multiverse
EOF
		;;
esac

# cribbed from cdimage, perhaps this should be a small helper script in germinate?
add_inheritance () {
	case " $inherit " in
		*" $1 "*)
			;;
		*)
			inherit="${inherit:+$inherit }$1"
			;;
	esac
}

expand_inheritance () {
	for seed in $(grep "^$1:" germinate-output/structure | cut -d: -f2); do
		expand_inheritance "$seed"
	done
	add_inheritance "$1"
}

inheritance () {
	inherit=
	expand_inheritance "$1"
	echo "$inherit"
}

if [ "$PREINSTALLED" = "true" ]; then
	if [ -n "$PREINSTALL_POOL_SEEDS" ]; then
		rm -rf germinate-output && mkdir germinate-output
		case $PROJECT in
			kubuntu-mobile*)
				SEED=kubuntu-mobile.$SUITE
				;;
			kubuntu*)
				SEED=kubuntu.$SUITE
				;;
			xubuntu*)
				SEED=xubuntu.$SUITE
				;;
			*)
				SEED=ubuntu.$SUITE
				;;
		esac
		(cd germinate-output && germinate --no-rdepends --no-installer \
			-S $SEEDMIRROR -m $MIRROR -d $SUITE -s $SEED \
			${COMPONENTS:+-c "$COMPONENTS"} -a $ARCH)

		UNWANTED_SEEDS="${LIVE_TASK:+$LIVE_TASK }boot installer required"
		for i in $UNWANTED_SEEDS; do
			UNWANTED_SEEDS="${UNWANTED_SEEDS:+$UNWANTED_SEEDS }$(inheritance $i)"
		done

		for i in $PREINSTALL_POOL_SEEDS; do
			PREINSTALL_POOL_SEEDS="${PREINSTALL_POOL_SEEDS:+$PREINSTALL_POOL_SEEDS }$(inheritance $i)"
		done

		for i in $PREINSTALL_POOL_SEEDS; do
			case " $UNWANTED_SEEDS " in
				*" $i "*)
					;;
				*)
					PPS_EXP="${PPS_EXP:+$PPS_EXP }$i"
					;;
			esac
		done

		for i in $PPS_EXP; do
			PREINSTALL_POOL="$PREINSTALL_POOL $(awk '{print $1}' \
			germinate-output/$i | egrep -v '^-|^Package|^\|' | tr '\n' ' ')"
		done
	fi
	if [ -n "$PREINSTALL_POOL" ]; then
		rm -rf gnupg && mkdir gnupg
		PREINSTALL_POOL_HOOK=config/chroot_local-hooks/preinstall-pool.sh
		echo "#! /bin/sh" > $PREINSTALL_POOL_HOOK
		echo "mkdir -p /var/lib/preinstalled-pool/" >> $PREINSTALL_POOL_HOOK
		echo "cd /var/lib/preinstalled-pool/" >> $PREINSTALL_POOL_HOOK
		echo "apt-get -y download $PREINSTALL_POOL" >> $PREINSTALL_POOL_HOOK
	fi
fi