#!/bin/sh

OSTYPEID=`cmk list ostypes keyword="Debian GNU/Linux 8 (64-bit)" | jq -r .ostype[0].id`
HVM=true

if which systemd-detect-virt > /dev/null; then
    if [ $(systemd-detect-virt) = "kvm" ]; then
        OSTYPEID=`cmk list ostypes keyword="Debian GNU/Linux 7(64-bit)" | jq -r .ostype[0].id`
        HVM=false
    fi
fi

TEMPLATERESULT=`cmk register template ostypeid=$OSTYPEID url="http://templates.cs.icloudhosting.com/systemVM/amito-systemvm-4.11-20190318082735.vhd" zoneid=-1 name="amito-systemvm-4.11-20190318082735" displaytext="amito-systemvm-4.11-20190318082735" format=VHD passwordenabled=false hypervisor=XenServer ispublic=false isfeatured=false requireshvm=$HVM`
TEMPLATEID=`echo "$TEMPLATERESULT" | jq -r '.template[0].id'`
cmk update configuration name=router.template.xenserver value=amito-systemvm-4.11-20190318082735 

echo "$TEMPLATERESULT" | jq '.'

echo
echo "When finished, run the following on the DB server for this CloudStack instance"
echo "UPDATE vm_template SET type = 'SYSTEM' WHERE uuid = '$TEMPLATEID' AND type = 'USER';"
