#!/bin/bash
## makeubuntu.sh, this creates Ubuntu server 10.04 32 and 64 bit templates on Xenserver 5.6. Net install only
## Author: David Markey <david.markey@citrix.com>
## This is not an officially supported guest OS on XenServer 5.6

LENNY=$(xe template-list name-label=Debian\ Lenny\ 5.0\ \(32-bit\) --minimal)

if [[ -z $LENNY ]] ; then
    echo "Cant find lenny 32bit template, is this on 5.6?"
    exit 1
fi

url=http://www-uxsup.csx.cam.ac.uk/pub/linux/ubuntu # or url=http://www-test.cl.cam.ac.uk/deb/ubuntu # or url=http://archive.ubuntu.net/ubuntu
case "$1" in
''|precise|12.04)	distro="Ubuntu 12.04" rel=precise;;
''|oneiric|11.10)	distro="Ubuntu 11.10" rel=oneiric;;
''|lucid|10.04)		distro="Ubuntu 10.04" rel=lucid;;
*)			echo $0: Unknown release: $1 2>&2;; exit 1;;
esac
arches=("32-bit" "64-bit")


for arch in ${arches[@]} ; do
    echo "Attempting $distro ($arch)"
    OLD=$(xe template-list name-label="$distro ($arch)" params=uuid --minimal)
    if [[ -n "$OLD" ]] ; then
        echo "$distro ($arch)" already exists "($OLD)", Skipping
	echo "... or:   sudo xe template-uninstall template-uuid=$OLD
    else

        NEWUUID=$(xe vm-clone uuid=$LENNY new-name-label="$distro ($arch)")
        xe template-param-set uuid=$NEWUUID other-config:install-methods=http,ftp \
         other-config:install-repository=$url \
         PV-args="-- quiet console=hvc0 partman/default_filesystem=ext3" \
         other-config:debian-release=$rel \
         other-config:disks='<provision><disk device="0" size="8589934592" sr="" bootable="true" type="system"/></provision>'
         other-config:default_template=true 

        if [[ "$arch" == "32-bit" ]] ; then
            xe template-param-set uuid=$NEWUUID other-config:install-arch="i386"
        else
            xe template-param-set uuid=$NEWUUID other-config:install-arch="amd64"
        fi
        echo "Success"
    fi

    

done


echo "Done"

