#!/bin/bash
#
# build-appliance - build or update a test appliance
#
# For details, see usage() and Documentation/building-rootfs.md

set -e -u

gen_image_args=
gce_image_args=
OUT_TAR=
DATECODE=
BUILD_ENV=
SUDO_ENV=sudo
DO_GCE=
NO_BUILD=
ADD_PACKAGES=

d=$(dirname "$0")
if test $d = "." ; then
    DIR=$(pwd)
else
    DIR=$(realpath $(dirname "$0"))
fi
cd "$DIR"

if test -f config.custom ; then
    . config.custom
else
    . config
fi

. run-fstests/util/arch-funcs

set_default_arch

usage()
{
    cat <<EOF
Usage: build-appliance [OPTION]...
Build or update a test appliance.

Options:
    --add-package PACKAGE
                    Install the package or comma-delimited list of packages
                    as additional packages in the test appliance
    --chroot CHROOT Use the specified build chroot, where CHROOT is the chroot
                    name in /etc/schroot/schroot.conf, e.g. "stretch-amd64".
    --gce           Build a GCE test appliance as well as a kvm/android
                    test appliance
    --out-tar       Build a root_fs.tar.gz, not a root_fs.img
    --out-both      Build both the root_Fs.img as well as the root_fs.tar.gz
    --no-out-tar    Build/update a root_fs.img, not a root_fs.tar.gz
    --update        Update only; don't do a clean build.  (Only supported with
                    .img output currently.)
EOF
}

UPDATE=false

longopts="help"
longopts+=",add-package:"
longopts+=",chroot:"
longopts+=",out-tar"
longopts+=",out-both"
longopts+=",no-build"
longopts+=",no-out-tar"
longopts+=",update"
longopts+=",gce"
longopts+=",datecode:"

if ! options=$(getopt -o "" -l "$longopts" -- "$@"); then
    usage 1>&2
    exit 2
fi

eval set -- "$options"
while (( $# >= 0 )); do
    case "$1" in
    --help)
	usage
	exit 0
	;;
    --add-package)
	ADD_PACKAGES="$ADD_PACKAGES $2"
	shift
	;;
    --chroot)
	BUILD_ENV="schroot -c $2 --"
	SUDO_ENV="schroot -c $2 -u root --"
	MY_ARCH=$(schroot -c $2 -- dpkg --print-architecture)
	shift
	;;
    --out-tar)
	OUT_TAR=yes
	;;
    --out-both)
	OUT_TAR=both
	;;
    --no-build)
	NO_BUILD=yes
	;;
    --no-out-tar)
	OUT_TAR=
	;;
    --update)
	UPDATE=true
	;;
    --gce)
	DO_GCE=yes
	;;
    --datecode) shift
	DATECODE="$1"
	;;
    --)
	shift
	break
	;;
    *)
	echo 1>&2 "Invalid option: \"$1\""
	usage 1>&2
	exit 2
	;;
    esac
    shift
done

if $UPDATE && [ "$OUT_TAR" = "yes" ]; then
    echo 1>&2 "--update is only supported with .img output currently!"
    exit 1
fi

set_my_arch
set_canonicalized_arch "$MY_ARCH"

cd fstests-bld

if $UPDATE; then
    $BUILD_ENV ./update-all
    gen_image_args+=" --update"
else
    if test -z "$NO_BUILD" ; then
	$BUILD_ENV make all-clean-first
    fi
fi
$BUILD_ENV ./gen-tarball --arch "$ARCH"

case "$OUT_TAR" in
    both) gen_image_args+=" --both" ;;
    yes)  gen_image_args+=" --out-tar" ;;
esac

if test -n "$ADD_PACKAGES" ; then
    a=$(echo $ADD_PACKAGES | sed -e 's/ /,/')
    gen_image_args+=" -a $a"
    gce_image_args+= "--add-package $a"
    unset a
fi

cd ../run-fstests
if test -n "$DO_GCE"; then
    d=
    if test -n "$DATECODE" ; then
	d="--datecode $DATECODE"
    fi
    ./gce-xfstests create-image $gce_image_args --arch=$ARCH $d \
		   >& /tmp/gce-xfstests-create.$$ &
fi

cd ../test-appliance
$SUDO_ENV ./gen-image $gen_image_args --src_date @$(git log -1 --pretty=%ct)

if test -n "$DO_GCE"; then
    tail -n +1 -f /tmp/gce-xfstests-create.$$ &
    wait %./gce-xfstests
    kill %tail
    rm -f /tmp/gce-xfstests-create.$$
fi
