#!/bin/sh -e

help () {
    cat <<EOF
$0 [OPTION] ... <release>: build a landscape-client VM
for the specified release. Available options:

  -m, --mirror <mirror>      The Ubuntu mirror to use to build the VM, and
                             for the APT sources inside the VM itself.
                             If you want to use apt-proxy, you have to
                             modify the apt-proxy-v2.conf file to make
                             apt-proxy listen to your actual network
                             interface address, instead of the loopback one.
                             For example:
                                 ;; Server IP to listen on
                                 address = 192.168.1.162
  -s, --server <host>        The hostname of the Landscape server the client
                             should connect to.
  -a, --account <account>    The name of the Landscape account to use.
  -p, --password <password>  The password for the Landscape account.
  -P, --profile <profile>    Package profile to use, can be server or
                             desktop.
  -k, --ssl-key <key>        Specify an SSL key to be used in the client
                             config.
  -r, --repository <ppa>     Specify the PPA holding the client packages.
  -b, --build <yes|no>       If yes, the landscape-client packages from this
                             branch will be built and installed inside the
                             VM, otherwise they will be pulled from the APT
                             repositories.

For example, this script can be invoked like this:

./dev/landscape-client-vm --password <LANDSCAPE_DEVEL_ACCOUNT_PW> intrepid

where <LANDSCAPE_DEVEL_ACCOUNT_PW> is the account password of the
landscape-devel account on the Landscape staging server (or you can specify
another account with the --account parameter).

The built VM will be stored under ./build/intrepid along with some other
files. To launch the VM, cd to ./build/intrepid and issue: 
$ ./run
Once it's booted you can log into it with:
$ ./ssh
EOF
}

OPTS=$(getopt -o hm:s:a:p:P:k:r:b: --long help,mirror:,server:,account:,password:,profile:,ssl-key:,repository:,build: -- "$@")

if [ $? != 0 ]; then
    exit 1
fi

eval set -- "$OPTS"

MIRROR=http://archive.ubuntu.com/ubuntu
SERVER=staging.landscape.canonical.com
ACCOUNT=landscape-devel
PASSWORD=
PROFILE=server
SSL_KEY=
PPA=landscape/trunk
BUILD=yes

while true ; do
    case "$1" in
	-h|--help) help; exit 1; shift ;;
	-m|--mirror) MIRROR=$2; shift 2 ;;
	-s|--server) SERVER=$2; shift 2;;
	-a|--account) ACCOUNT=$2; shift 2;;
	-p|--password) PASSWORD=$2; shift 2;;
	-P|--profile) PROFILE=$2; shift 2;;
	-S|--ssl-key) SSL_KEY=$2; shift 2;;
	-r|--repository) PPA=$2; shift 2;;
        -b|--build) BUILD=$2; shift 2;;
	--) shift ; break ;;
	*) echo "Internal error!" ; exit 1 ;;
    esac
done

if [ "$1" = "" ]; then
    help
    exit
fi

RELEASE=$1
TOPDIR=$(pwd)/build/${RELEASE}-${PROFILE}
SSH_KEY=$TOPDIR/ssh_key
SSH_PORT=3322
ROOTSIZE=8192

rm -fR $TOPDIR
mkdir -p $TOPDIR
ssh-keygen -N '' -f $SSH_KEY

cd $TOPDIR

cat > config <<EOF
[client]
url = https://${SERVER}/message-system
computer_title = ${RELEASE} test VM $$
data_path = /var/lib/landscape/client
script_users = ALL
ping_url = http://${SERVER}/ping
include_manager_plugins = ScriptExecution
account_name = ${ACCOUNT}
registration_password = ${PASSWORD}
#log_level = debug
EOF

if ! [ "$WITH_SSL" = "" ]; then
    echo ssl_public_key = $SSL_KEY >> config
fi

cat > script-wrapper <<EOF
#!/bin/sh -e
chroot \$1 /root/script
EOF
chmod 755 script-wrapper

cat> ppa-key <<EOF
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.0.10

mI0ESXN/egEEAOgRYISU9dnQm4BB5ZEEwKT+NKUDNd/DhMYdtBMw9Yk7S5cyoqpbtwoPJVzK
AXxq+ng5e3yYypSv98pLMr5UF09FGaeyGlD4s1uaVFWkFCO4jsTg7pWIY6qzO/jMxB5+Yu/G
0GjWQMNKxFk0oHMa0PhNBZtdPacVz65mOVmCsh/lABEBAAG0G0xhdW5jaHBhZCBQUEEgZm9y
IExhbmRzY2FwZYi2BBMBAgAgBQJJc396AhsDBgsJCAcDAgQVAggDBBYCAwECHgECF4AACgkQ
boWobkZStOb+rwP+ONKUWeX+MTIPqGWkknBPV7jm8nyyIUojC4IhS+9YR6GYnn0hMABSkEHm
IV73feKmrT2GESYI1UdYeKiOkWsPN/JyBk+eTvKet0qsw5TluqiHSW+LEi/+zUyrS3dDMX3o
yaLgYa+UkjIyxnaKLkQuCiS+D+fYwnJulIkhaKObtdE=
=UwRd
-----END PGP PUBLIC KEY BLOCK-----
EOF

cat > script <<EOF
#!/bin/sh -e
chown landscape /etc/landscape/client.conf
chmod 600 /etc/landscape/client.conf
apt-key add /root/ppa-key
echo "RUN=1" > /etc/default/landscape-client
EOF
chmod 755 script

cat > manifest <<EOF
ppa-key /root
script /root/script
config /etc/landscape/client.conf
EOF

if [ "$WITH_SSL" = "yes" ]; then
    echo /etc/landscape/certs/sample_ca.crt /etc/landscape/client.conf.ssl_public_key >> manifest
fi

cat > ssh <<EOF
#!/bin/sh
exec ssh -p $SSH_PORT -i $SSH_KEY -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost
EOF
chmod 755 ssh

cat > run <<EOF
#!/bin/sh
exec kvm --redir tcp:$SSH_PORT::22 -m 1280 -smp 1 -drive file=$TOPDIR/ubuntu-kvm/disk0.qcow2 "$@"
EOF
chmod 755 run

ADDPKGS="--addpkg=landscape-client --addpkg=openssh-server"

if [ "$PROFILE" = "server" ]; then
    ADDPKGS="$ADDPKGS --addpkg apache2-mpm-prefork --addpkg postgresql --addpkg postfix"
else
    ADDPKGS="$ADDPKGS --addpkg ubuntu-desktop"
fi

if [ -n "$TMPDIR" ]; then
    TEMP_DIR_OPT="-t $TMPDIR"
fi

sudo ubuntu-vm-builder kvm "$RELEASE" --rootsize=$ROOTSIZE --mirror="$MIRROR" --ppa="$PPA" $ADDPKGS --execscript=./script-wrapper --copy=manifest --ssh-key=$SSH_KEY.pub $TEMP_DIR_OPT
