#! /bin/sh

set -eu

TMPDIR=$(mktemp -d /tmp/$(basename $0).XXXXX)
SUITE="trusty"
PREFIX="ubuntu-phablet"
CHARTOPTS="${CHARTOPTS---crop-after=unity8 --annotate=unity8}"
SKIPINST=""
KEEPTAR=""
PNOPTS=""
BUILDID=""
OUTDIR="."
SFX="png"
ERROR=""
ADBOPTS=""
YESTOALL=""

cleanup(){
    [ -n "$ERROR" ] && echo "Error: $ERROR"
	[ -d "$TMPDIR" ] && rm -rf $TMPDIR >/dev/null 2>&1
}

usage(){
cat <<EOF 
Usage: $(basename $0) (Options) [build id]

Options:
	-n|--noinstall				do not perform installation
	-k|--keeptarball			keep tarball around
	-w|--wlancfg <path to wlan file>	point to wlan config
	-o|--outdir <path to output directory>	define (and create) output dir
	-f|--format (png|svg)			output format (default: png)
	-s|--serial <adb serial>		operate on certain adb device
	-y|--yes-to-all				do not ask when wiping (for automation)

	If [build id] is provided this image will be used for installation
EOF
	exit 0
}

reboot_and_wait(){
	adb $ADBOPTS reboot && adb $ADBOPTS wait-for-device
	[ -n "$1" ] && sleep $1
}

do_install(){
	if [ -z "$YESTOALL" ]; then
		echo -n "This will wipe all data on the device, proceed ? (y/n) "
		read yesno
		[ "$yesno" != "y" ] && exit 1
	fi
	echo "Installing Image $1"
	if [ -z "$(fastboot devices)" ]; then
		adb $ADBOPTS reboot bootloader || \
			ERROR="can not reboot to bootloader" exit 1
	fi
	FLASHOPTS="--channel devel-proposed --bootstrap --wipe"
	[ -n "$1" ] && FLASHOPTS="${FLASHOPTS} --revision $1"
	ubuntu-device-flash $FLASHOPTS
	adb $ADBOPTS wait-for-device
}

trap cleanup EXIT INT QUIT ILL KILL SEGV TERM

[ -x /usr/bin/pybootchartgui ] || ERROR="please install pybootchartgui" exit 1

while [ $# -gt 0 ]; do
	case "$1" in
		-n|--noinstall)
			SKIPINST=1
			;;
		-y|--yes-to-all)
			YESTOALL=1
			;;
		-k|--keeptarball)
			KEEPTAR=1
			;;
		-w|--wlancfg)
		 	[ -n "$2" ] && PNOPTS="-n $2" shift || \
				ERROR="-w needs a file path" exit 1
		 	;;
		-s|--serial)
			[ -n "$2" ] && ADBOPTS="$ADBOPTS -s $2" shift || \
				ERROR="-s needs serial" exit 1
			;;
		-f|--format)
		 	if [ -n "$2" ]; then
				SFX="$2" shift || \
					ERROR="-f needs png or svg as argument" exit 1
				CHARTOPTS="$CHARTOPTS -f $SFX"
			fi
		 	;;
		-o|--outdir)
			[ -n "$2" ] && OUTDIR="$2" shift || \
				ERROR="-o needs a path" exit 1
			[ -d "$OUTDIR" ] || mkdir -p $OUTDIR
			;;
		-h|--help)
			usage
			;;
		*[0-9])
			BUILDID=$1
			;;
		*)
			ERROR="$1 is unknown" exit 1
			;;
	esac
	shift
done

[ -z "$SKIPINST" ] && do_install "$BUILDID"
echo "Enabling network"
phablet-network $PNOPTS || \
	ERROR="need working network on the device" exit 1

echo "Making Image writable and installing bootchart"
phablet-config writable-image -p bootchart || \
	ERROR="could not install bootchart package" exit 1
reboot_and_wait 30

echo "Disabling Intro Demo"
phablet-config edges-intro --disable || \
	ERROR="could disable intro" exit 1

echo "Rebooting three times to get accurate data"
reboot_and_wait 60
reboot_and_wait 60
reboot_and_wait 1
adb $ADBOPTS shell rm -rf /var/log/bootchart/*
sleep 60

echo "Pulling and processing bootchart tarball"
while [ -n "$(adb $ADBOPTS shell pgrep collector)" ]; do
	sleep 30
done
sleep 30
adb $ADBOPTS pull /var/log/bootchart $TMPDIR/

VER="$(adb $ADBOPTS shell system-image-cli -i | grep "^.* ver" | tr -d '[:cntrl:]')"
VER="${VER##* }"
SUITE="$(adb $ADBOPTS shell lsb_release -cs | tr -d '[:cntrl:]')"

echo "Creating $SFX for $VER."
OUTNAME="$OUTDIR/$PREFIX-$SUITE-$VER"
CHARTOPTS="$CHARTOPTS -o $OUTNAME.$SFX"
pybootchartgui -q $CHARTOPTS $TMPDIR/*.tgz >/dev/null 2>&1

if [ -n "$KEEPTAR" ]; then
	cp $TMPDIR/*.tgz $OUTNAME.tgz
	echo "Keeping tarball at $OUTNAME.tgz"
fi
echo "Created $OUTNAME.$SFX"
