#!/bin/bash
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#

set -e

usage () {
  echo Usage: $0 host:/directory [mount options]
  echo Create a shared SR which mounts a remote ISO repository.
  exit 1
}

if [ -z "$1" ]; then
  usage
else
  LOCATION="$1"
fi
shift 1
OPTIONS=$*

XE="@BASE_PATH@/bin/xe"
UUID=$(uuidgen)

SR=$(${XE} sr-introduce name-label="Remote ISO Library on: ${LOCATION}" content-type=iso shared=true type=iso uuid=${UUID} physical-size=0)
${XE} sr-param-set other-config:auto-scan=true uuid=${UUID}

. /etc/xensource-inventory 

PBD=$(${XE} pbd-create host-uuid=${INSTALLATION_UUID} sr-uuid=$SR device-config:location="${LOCATION}" device-config:options="${OPTIONS}")

destroy () {
    xe pbd-destroy uuid=${PBD}
    xe sr-forget uuid=${SR}
    exit 1
}

xe pbd-plug uuid=${PBD} &>/dev/null || destroy
