#!/bin/sh
#
# This package is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

set -e

VM=kvm
SUITE=hardy
ARCH=lpia
COMPS=main,restricted,universe
USER=ume
SELF="$(basename "$0")"
KVM_OPTS="-soundhw all"
DATADIR="$(pwd)/data"

usage () {
    usage=$(cat <<EOF
usage: $SELF
                            [--platform PLATFORM]
                            [--fset FSET [--fset FSET...]]
                            [--sources SOURCES]
                            [<vm> <suite>]

vm                 Generate image for the specified virtualization software.
                   Valid choices are: vmw6 vmserver vbox qemu kvm
suite              Use the specified Ubuntu suite.
                   Valid options: hardy, gutsy, feisty, edgy, dapper
--platform         Use the specified moblin-image-creator platform (see available platforms with "image-creator -c list-platforms")
--fset             Add the specified moblin-image-creator fset to guest (can be specified multiple times, requires a valid platform)
--sources          Add sources.list (full path required) to guests /etc/apt/sources.list.d (can be specified multiple times)
EOF
)
    # if this function was called with a non-empty argument, exit with this
    # code after output; otherwise, just output to stdout and return
    exit_code="${1:-}"
    if [ -z "$exit_code" ]; then
        echo "$usage"
        return
    fi
    # if exit code is zero, output to stdout, otherwise to stderr
    if [ $exit_code -eq 0 ]; then
        echo "$usage"
    else
        echo "$usage" >&2
    fi
    exit $exit_code
}

log() {
    echo "$@" >&2
}

log_e() {
    log "E:" "$@"
}

log_c() {
    log "C:" "$@"
    exit 1
}

if TEMP=`getopt -o f:p:s: --long fset:,platform:,sources:,help -- "$@"`; then
    eval set -- "$TEMP"
else
    log_e "Couldn't parse options"
    usage 1
fi

# FIXME: ideally we would have "ubuntu-mobile ubuntu-minimal"
#        here. but mobile uses meta-packages that much yet
PKGS="ubuntu-mobile ume-config-common gnome-icon-theme gnome-menus xserver-xorg-video-cirrus xfonts-base"
PLATFORM=""
FSET=""

while :; do
	case "$1" in
		--help)
			usage 0
		;;
		--platform)
			PLATFORM="$2"
			shift 2
		;;
		--fset)
			FSET="$FSET $2"
			shift 2
		;;
		--sources)
			SOURCES="$SOURCES\n$2"
			shift 2
		;;
		--)
			shift
			break
	        ;;
		*)
			log_e "Unknown option: $1"
			usage 1
		;;
        esac
done

# show help if no arguments are given
case $# in
  0)
    # use defaults
    :
    ;;
  2)
    VM="$1"
    SUITE="$2"
    shift 2
  ;;
  *)
    usage 1
  ;;
esac

tmpdir=""
cleanup() {
    if [ -n "$tmpdir" ]; then
        rm -rf "$tmpdir"
    fi
}
trap cleanup EXIT HUP INT QUIT TERM
tmpdir=$(mktemp -dt "$SELF.XXXXXXXXXX")

cp -f "$DATADIR"/* "$tmpdir"

# FIXME: this sucks, but currently we can not give args to the --exec script
/bin/echo -e "$SOURCES" >"$tmpdir/SOURCES"
/bin/echo -e "$PKGS" >"$tmpdir/PKGS"

if [ -n "$PLATFORM" ] || [ -n "$FSET" ]; then
    if ! which image-creator >/dev/null; then
        log_c "You need moblin-image-creator for --platform and --fsets"
    fi
fi
if [ -n "$PLATFORM" ]; then
    image-creator --platform-name=$PLATFORM -c list-sources-files >>"$tmpdir/SOURCES"
    if [ $? -ne 0 ]; then
        log_c "Couldn't query sources files with image-creator"
    fi
fi

if [ -n "$PLATFORM" ] && [ -n "$FSET" ]; then
    image-creator --platform-name=$PLATFORM \
                  $(echo "$FSET" | sed -r 's/(^| )(.)/\1--fset-name=\2/g') \
                  -c list-pkgs \
                  >>"$tmpdir/PKGS"
    if [ $? -ne 0 ]; then
        log_c "Couldn't query packages with image-creator"
    fi
fi

ubuntu-vm-builder $VM $SUITE \
    --arch $ARCH \
    --user $USER \
    --components $COMPS \
    --exec "$tmpdir/mobile-fixup.sh"

# enable sound in kvm
F="ubuntu-vm-$SUITE-$ARCH/ubuntu.kvm"
if [ -e $F ]; then
    sed -i "s/^kvm /kvm $KVM_OPTS /" $F
fi

