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

# Runs on the George master when performing a rolling upgrade from Orlando

set -e

. @INVENTORY@
XE="@BINDIR@/xe"

# Clean all unreferenced patches out of the db now that the upgrade is complete.
# When upgrading to 5.0, this should clean out all 4.1 patches at the end.
function clean_patches {
    IFS=,
    for pool_patch in $(xe patch-list --minimal params=uuid hosts=) ; do
        xe patch-destroy uuid=${pool_patch}
    done
}

case "$1" in
  start)
    
  ;;
  stop)
    for i in $($XE sr-list other-config:xenserver_tools_sr=true params=uuid --minimal | sed 's/,/ /g'); do
      echo Renaming $i
      $XE sr-param-set uuid=$i name-label="XenServer Tools" name-description="XenServer Tools ISOs"
      echo Forgetting all missing VDIs
      # Note any suspended guest using one of these VDIs will be confused
      for j in $($XE vdi-list sr-uuid=$i missing=true params=uuid --minimal | sed 's/,/ /g'); do
	  $XE vdi-forget uuid=$j
      done
    done

    # Clean up patches
    clean_patches
  ;;
esac
