#!/bin/bash

set -e

. ${XENSOURCE_INVENTORY}

CONFIGURATION="${FIRSTBOOT_DATA_DIR}/default-storage.conf"
UPGRADE="false"
[ -r ${FIRSTBOOT_DATA_DIR}/host.conf ] && . ${FIRSTBOOT_DATA_DIR}/host.conf

start() {
    [ "$UPGRADE" = true ] && return 0
    if [ -e ${CONFIGURATION} ]; then
        source ${CONFIGURATION}
        SR=$($XE sr-list type=$TYPE params=uuid --minimal | cut -f1 -d,)
        POOL_UUID=$($XE pool-list params=uuid --minimal | cut -f1 -d,)
        
        $XE pool-param-set uuid=${POOL_UUID} default-SR=${SR}
        $XE host-param-set uuid=${INSTALLATION_UUID} crash-dump-sr-uuid=${SR}
        $XE host-param-set uuid=${INSTALLATION_UUID} suspend-image-sr-uuid=${SR}

	if [ "$TYPE" = "ext" ]; then
		# Wait for the host to enable itself, then configure caching.
		$XE event-wait class=host uuid=${INSTALLATION_UUID} enabled=true
		$XE host-disable uuid=${INSTALLATION_UUID}
		$XE host-enable-local-storage-caching uuid=${INSTALLATION_UUID} sr-uuid=${SR} || true
		$XE host-enable uuid=${INSTALLATION_UUID}
	fi
    fi
}

case $1 in 
    start)  start ;;
esac
