#!/bin/bash

set -- $(getopt h?H:D?s:B:M:C:R:i:g:n:I:G:N: "$@")

src_image=""
size="1G"
dhcp="no"
hostname="default"
bridge="br0"
cpus="1"
mem="1024"
repo="git://code.mnt.se/mnt-cosmos.git"
tag="eduid-cosmos"
ip=""
gateway=""
netmask=""
resolver=""
ip6=""
gateway6=""
netmask6="64"

while [ $# -gt 0 ]; do
   case "$1" in
      (-h) echo "Usage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 0;;
      (-H) hostname="$2"; shift;; 
      (-s) src_image="$2"; shift;;
      (-D) dhcp="yes" ;;
      (-S) size="$2"; shift;;
      (-B) bridge="$2"; shift;;
      (-M) mem="$2"; shift;;
      (-C) cpus="$2"; shift;;
      (-R) resolver="$2"; shift;;
      (-i) ip="$2"; shift;;
      (-g) gateway="$2"; shift;;
      (-n) netmask="$2"; shift;;
      (-I) ip6="$2"; shift;;
      (-G) gateway6="$2"; shift;;
      (-N) netmask6="$2"; shift;;
      (--) shift; break;;
      (-*) echo "Unknown option $1\nUsage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 1;;
      (*) break;;
   esac
   shift
done

id=$(uuidgen)

seed=${id}_seed.img
disk=${id}.img

rm -f ${seed}
truncate --size 2M ${seed}
mkfs.vfat -n cidata ${seed} 2>/dev/null

user_data=$(mktemp)
cat > ${user_data} <<EOF
#cloud-config
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True
ssh_authorized_keys:
   - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVvB4gdJ6EWRmx8xUSxrhoUNnWxEf8ZwAqhzC1+7XBY/hSd/cbEotLB9gxgqt0CLW56VU4FPLTw8snD8tgsyZN6KH1Da7UXno8oMk8tJdwLQM0Ggx3aWuztItkDfBc3Lfvq5T07YfphqJO7rcSGbS4QQdflXuOM9JLi6NStVao0ia4aE6Tj68pVVb3++XYvqvbU6NtEICvkTxEY93YpnRSfeAi64hsbaqSTN4kpeltzoSD1Rikz2aQFtFXE03ZC48HtGGhdMFA/Ade6KWBDaXxHGARVQ9/UccfhaR2XSjVxSZ8FBNOzNsH4k9cQIb2ndkEOXZXnjF5ZjdI4ZU0F+t7 leifj+00060AD478D6@sunet.se
runcmd:
   - ["mkdir","/tmp/seed"]
   - ["mount","/dev/vdb","/tmp/seed"]
   - ["cp","/tmp/seed/bootstrap-cosmos.sh","/tmp/seed/cosmos_1.2-2_all.deb","/root"]
   - ["cd","/root"]
   - "cd /root && /root/bootstrap-cosmos.sh ${hostname} ${repo} ${tag}"

EOF

meta_data=$(mktemp)
cat > ${meta_data} <<EOF
#cloud-config
instance-id: iid-${id}
local-hostname: ${hostname}
EOF

if [ "x${dhcp}" = "xyes" ]; then
cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet dhcp
EOF
else
  if [ "x${ip}" != "x" ]; then
     cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet static
        address ${ip}
        netmask ${netmask}
        gateway ${gateway}
        dns-nameservers ${resolver}

EOF
  fi

  if [ "x${ip6}" != "x" ]; then
     cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet6 static
        address ${ip6}
        netmask ${netmask6}
        gateway ${gateway6}
        dns-nameservers ${resolver}

EOF
  fi
fi

mcopy -i ${seed} ${user_data} ::user-data 2>/dev/null
mcopy -i ${seed} ${meta_data} ::meta-data 2>/dev/null
mcopy -i ${seed} /etc/cosmos/apt/bootstrap-cosmos.sh /etc/cosmos/apt/cosmos_1.2-2_all.deb ::
mv ${seed} /var/lib/libvirt/images/
virsh pool-refresh default

virsh vol-clone --pool default ${src_image} ${disk}

virt-install -r ${mem} -n ${hostname} --vcpus=${cpus} --autostart --memballoon virtio --network bridge=${bridge} --boot hd --disk vol=default/${disk},format=qcow2,bus=virtio --disk vol=default/${seed},bus=virtio

rm -f ${user_data}
rm -f ${meta_data}