#!/bin/bash

FB_IOC="none"
DEST="none"
AREA="sys0"
SIMULATE="no"
VERBOSE="no"
DEVELOPMENT="no"

LINUXRT_PV_ON_STARTUP_FILE=st-on.cmd
LINUXRT_PV_OFF_STARTUP_FILE=st-off.cmd
LINUXRT_STARTUP_FILE=st.cmd

VME_PV_ON_STARTUP_FILE=st-rtems-on.cmd
VME_PV_OFF_STARTUP_FILE=st-rtems-off.cmd
VME_STARTUP_FILE=st-rtems.cmd

# ==============================================================================
# Get the current VME/LinuxRT status stored in the Matlab PVs
# ==============================================================================
function getCurrentStatus {
    let last=${#STATUS_PV[@]}-1
    for i in `seq 0 $last`; do
	pv=SIOC:SYS0:ML03:${STATUS_PV[$i]}
	cmd="caget -t $pv"
	CURRENT_STATUS[$i]=`$cmd`
	pv=SIOC:SYS0:ML03:${STATUS_PV[$i]}.DESC
	cmd="caget -t $pv"
	CURRENT_PLATFORM[$i]=`$cmd`
	if [ "$VERBOSE" == "yes" ]; then
	    echo "# INFO: ${FB_NAME[$i]} running on ${CURRENT_PLATFORM[$i]}"
	fi
    done

    # Check if all feedbacks are running on VME or LinuxRT
    status=${CURRENT_STATUS[0]}
    different=0
    for i in `seq 1 $last`; do
	if [ "$status" != "${CURRENT_STATUS[$i]}" ]; then
	    different=1
	fi
    done

    # Print warning and exit if there are feedbacks running on different platforms
    if [ "$different" == "1" ]; then
	fbcks=""
	for i in `seq 0 $last`; do
	    fbcks=$fbcks${FB_NAME[$i]}" running on "${CURRENT_PLATFORM[$i]}
	    fbcks=$fbcks"\n"
	    echo $fbcks
	done
	zenity --warning --title "Warning" --text="Detected feedbacks running on different platforms:\n\n$fbcks"

	# Let user decide with platform to enable
	CURRENT_PLATFORM[0]="select"
    fi
}

# ==============================================================================
# Update Matlab PVs indicating which feedbacks are enabled - these PVs are
# used by EDM panel.
# ==============================================================================
function updateStatusPVs {
    COMMENT_VAL="switchFF.sh 1=VME 2=LinuxRT"
    if [ "$1" == "VME" ]; then
	STATUS_VAL=1
	STATUS_DESC="VME"
    else
	STATUS_VAL=2
	STATUS_DESC="LinuxRT"
    fi

    let last=${#STATUS_PV[@]}-1
    for i in `seq 0 $last`; do
	pv=SIOC:SYS0:ML03:${STATUS_PV[$i]}
	descPv=$pv.DESC
	commentPv=SIOC:SYS0:ML03:${COMMENT_PV[$i]}
	cmd1="caput $pv $STATUS_VAL"
	cmd2="caput $descPv $STATUS_DESC"
	cmd3="caput $commentPv $COMMENT_VAL"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd1; $cmd2; $cmd3] Set MatlabPV feedback switch status"
	else
	    $cmd1
	    $cmd2
	    $cmd3
	fi
    done
}

# ==============================================================================
# Check if PV to reboot the VME IOC is available and whether the CPUs running 
# the VIOCs are responding to ping commands. If either one fails then the reboot
# does not happen and the switch command fails.
# ==============================================================================
function checkReboot {
    res=0

    pv=ioc-${AREA}-$FB_IOC-SYSRESET
    pv=`echo $pv | tr - : | tr [:lower:] [:upper:]`

    # The VME PV must exist in order to proceed with the reboot
    x=`caget -t $pv`
    if [ "$?" != "0" ]; then
	echo "#ERROR: Failed to find PV to reboot VME IOC"
	zenity --error --text="ERROR: Failed to find $pv to reboot ioc-sys0-$FB_IOC"
	res=1
    # If VME PV exists, then check if VIOC CPUs are pinging
    else
	let last=${#FB_PV[@]}-1
	for i in `seq 0 $last`; do
	    vioc=vioc-${LINUX_VIOC[$i]}
	    cpu=${LINUX_CPU[$i]}
	    `ping -c 1 $cpu > /dev/null`
	    if [ "$?" != "0" ]; then
		echo "# ERROR: Failed to ping $cpu host for $vioc"
		zenity --error --text="ERROR: Failed to ping $cpu host for $vioc (${FB_NAME[$i]})"
		res=1
	    fi
	done
    fi
    proceedWithReboot=$res
}

# ==============================================================================
# Reboot VME IOC
# ==============================================================================
function rebootVME {
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Rebooting VME IOC"
    fi
    pv=ioc-${AREA}-$FB_IOC-SYSRESET
    pv=`echo $pv | tr - : | tr [:lower:] [:upper:]`

    cmd="caput -t $pv 1"
    if [ "$SIMULATE" == "yes" ]; then
	echo "### [$cmd] Would reboot VME IOC"
    else
	$cmd >& /dev/null &
    fi
}

# ==============================================================================  
# Reboot LinuxRT VIOCs
# ==============================================================================
function rebootLinuxRT {
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Rebooting LinuxRT IOCs"
    fi
    let last=${#FB_PV[@]}-1
    for i in `seq 0 $last`; do
	vioc=vioc-${LINUX_VIOC[$i]}
	cpu=${LINUX_CPU[$i]}
	if [ "$VERBOSE" == "yes" ]; then
	    echo "# INFO: Rebooting $vioc on $cpu"
	fi
#	cmd="$IOC/common/linuxRT_remote_restart_vioc.sh -v $vioc -u laci -c $cpu"
	cmd="viocRestart -v $vioc -u laci -c $cpu"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd] Would reboot vioc-${LINUX_VIOC[$i]}"
	else
	    $cmd
	fi
    done
}

# ==============================================================================
# Reboot all IOC to complete the switch
#
# $1 - Platform to be rebooted second
# ==============================================================================
function rebootIOCs {
    if [ "$SIMULATE" == "yes" ]; then
	proceedWithReboot=0
    else
	checkReboot
    fi

    if [ "$proceedWithReboot" == "0" ]; then
	if [ "$1" == "VME" ]; then
	    rebootVME
	    rebootLinuxRT
	else
	    rebootLinuxRT
	    rebootVME
	fi
    fi
}

# ==============================================================================
# $1 - startup file (e.g. st.cmd)
# $2 - link to (e.g. st-on.cmd)
#
# Backs up the $1 file (e.g. st.cmd.SAVE) and creates a symLink (e.g.
# st.cmd -> st-on.cmd)
# ==============================================================================
function swapSymLink {
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Saving copy of startup file ($1.SAVE)"
    fi
    if [ -f "$1" ]; then
	cmd="mv $1 $1.SAVE"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd] Would save a copy of original startup file"
	else
            `$cmd`
	fi
    fi

    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Create link to startup file"
    fi
    if [ -f "$2" ]; then
	cmd="ln -s $2 $1"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd] Create link to startup file"
	else
            `$cmd`
	fi
    fi
}

# ==============================================================================
# $1 - startup file to be replaced (e.g. st.cmd)
# $2 - startup file to replace it (e.g. st-on.cmd)
#
# Backs up the $1 file (e.g. st.cmd.SAVE) and creates copies the $2 file to it
# (cp st-on.cmd st.cmd)
# ==============================================================================
function copyStartup {
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Saving copy of startup file ($1.SAVE)"
    fi
    if [ -f "$1" ]; then
	cmd="mv $1 $1.SAVE"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd] Would save a copy of original startup file"
	else
            `$cmd`
	fi
    fi

    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Copying $2 to $1"
    fi
    if [ -f "$2" ]; then
	cmd="cp $2 $1"
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [$cmd] Create link to startup file"
	else
            `$cmd`
	fi
    fi
}

# ==============================================================================
# Change the startup scripts
# $1 - Platform to enable (PV ON)
# ==============================================================================
function changeScripts {
    ENABLE=$1
    if [ "$ENABLE" == VME ]; then
	VME_LINK_TO=$VME_PV_ON_STARTUP_FILE
	LINUX_LINK_TO=$LINUXRT_PV_OFF_STARTUP_FILE
    else
	VME_LINK_TO=$VME_PV_OFF_STARTUP_FILE
	LINUX_LINK_TO=$LINUXRT_PV_ON_STARTUP_FILE
    fi

    # Swap VME links
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Changing symLinks for VME IOC $FB_IOC"
    fi
    IOC_NAME=ioc-${AREA}-`echo $FB_IOC | tr [:upper:] [:lower:]`
    VME_DIR=$IOC/$IOC_NAME/iocSpecificRelease/iocBoot/$IOC_NAME
    copyStartup $VME_DIR/$VME_STARTUP_FILE $VME_DIR/$VME_LINK_TO

    # Swap LinuxRT links
    let last=${#FB_PV[@]}-1
    for i in `seq 0 $last`; do
	VIOC_NAME=vioc-${LINUX_VIOC[$i]}
	LINUX_DIR=$IOC/$VIOC_NAME/iocSpecificRelease/iocBoot/$VIOC_NAME
	if [ "$VERBOSE" == "yes" ]; then
	    echo "# INFO: Changing symLinks for LinuxRT VIOC $VIOC_NAME (${FB_NAME[$i]})"
	fi
#	swapSymLink $LINUX_DIR/$LINUXRT_STARTUP_FILE $LINUX_DIR/$LINUX_LINK_TO
	copyStartup $LINUX_DIR/$LINUXRT_STARTUP_FILE $LINUX_DIR/$LINUX_LINK_TO
    done
}

# ==============================================================================
# Check if all startup scripts exist. There must be two startup scripts
# for each platform. One that loads the feedback PVs and another one 
# that does not load. Both IOCs will be up at the same time, but only one
# will be functional. 
# ==============================================================================
function checkStartupScripts {
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Checking if all startup scripts exist"
    fi

    # First the LinuxRT scripts
    let last=${#FB_PV[@]}-1
    for i in `seq 0 $last`; do
	VIOC_NAME=vioc-${LINUX_VIOC[$i]}
	LINUX_DIR=$IOC/$VIOC_NAME
	if [ "$VERBOSE" == "yes" ]; then
	    echo "# INFO: Checking if directory $LINUX_DIR exists for ${FB_NAME[$i]} feedback"
	fi
	if [ ! -d $LINUX_DIR ]; then
	    echo "ERROR: Directory $LINUX_DIR is missing, try again"
	    exit 1
	fi
	IOCBOOT_DIR=$LINUX_DIR/iocSpecificRelease/iocBoot/$VIOC_NAME
	if [ ! -f $IOCBOOT_DIR/$LINUXRT_PV_ON_STARTUP_FILE ]; then
	    echo "ERROR: Missing $IOCBOOT_DIR/$LINUXRT_PV_ON_STARTUP_FILE, try again"
	    exit 1
	fi
	if [ ! -f $IOCBOOT_DIR/$LINUXRT_PV_OFF_STARTUP_FILE ]; then
	    echo "ERROR: Missing $IOCBOOT_DIR/$LINUXRT_PV_ON_STARTUP_FILE, try again"
	    exit 1
	fi
    done

    # Check VME scripts
    IOC_NAME=ioc-${AREA}-`echo $FB_IOC | tr [:upper:] [:lower:]`
    VME_DIR=$IOC/$IOC_NAME
    if [ "$VERBOSE" == "yes" ]; then
	echo "# INFO: Checking if directory $VME_DIR exists for $FB_IOC feedbacks"
    fi
    if [ ! -d $VME_DIR ]; then
	echo "ERROR: Directory $VME_DIR is missing, try again"
	exit 1
    fi
    IOCBOOT_DIR=$VME_DIR/iocSpecificRelease/iocBoot/$IOC_NAME
    if [ ! -f $IOCBOOT_DIR/$VME_PV_ON_STARTUP_FILE ]; then
	echo "ERROR: Missing $IOCBOOT_DIR/$VME_PV_ON_STARTUP_FILE, try again"
	exit 1
    fi
    if [ ! -f $IOCBOOT_DIR/$VME_PV_OFF_STARTUP_FILE ]; then
	echo "ERROR: Missing $IOCBOOT_DIR/$VME_PV_ON_STARTUP_FILE, try again"
	exit 1
    fi
}

function turnOffFeedbacks {
    turnOffFeedbackSet
    
    # If FB01 VME IOC will be rebooted we first need to turn feedbacks
    # running on FB02/03/04 off, because they share the crate
    if [ "$FB_IOC" == "FB01" ]; then
	FB01_last=$last
	let last=${#FB02_FB_NAME[@]}-1
	FB_NAME=( "${FB02_FB_NAME[@]}" )
	FB_PV=( "${FB02_FB_PV[@]}" )
	turnOffFeedbackSet

	let last=${#FB03_FB_NAME[@]}-1
	FB_NAME=( "${FB03_FB_NAME[@]}" )
	FB_PV=( "${FB03_FB_PV[@]}" )
	turnOffFeedbackSet

	let last=${#FB04_FB_NAME[@]}-1
	FB_NAME=( "${FB04_FB_NAME[@]}" )
	FB_PV=( "${FB04_FB_PV[@]}" )
	turnOffFeedbackSet

	last=$FB01_last
	FB_NAME=( "${FB01_FB_NAME[@]}" )
	FB_PV=( "${FB01_FB_PV[@]}" )
    fi
}

# ==============================================================================
# Turn feedbacks off - they may be running either on VME or LinuxRT.
# The PVs are the same.
# ==============================================================================
function turnOffFeedbackSet {
    echo "LAST $last"
    # First check if all feedback PVs are available
    if [ "$SIMULATE" == "no" ]; then
	echo "# SIMULATE: [$cmd] Create link to startup file"
	for i in `seq 0 $last`; do
	    modePv="FBCK:${FB_PV[$i]}:MODE"
	    statePv="FBCK:${FB_PV[$i]}:STATE"
	    x=`caget -t $modePv`
	    if [ "$?" != "0" ]; then
		zenity --error --text="ERROR: Failed to find $modePv PV to turn off feedback ${FB_NAME[$i]}. Feedback switch cannot proceed, exiting."
		exit 1
	    fi
	    x=`caget -t $statePv`
	    if [ "$?" != "0" ]; then
		zenity --error --text="ERROR: Failed to find $statePv PV to turn off feedback ${FB_NAME[$i]}. Feedback switch cannot proceed, exiting."
		exit 1
	    fi
	done
    fi

    for i in `seq 0 $last`; do
	modePv="FBCK:${FB_PV[$i]}:MODE"
	statePv="FBCK:${FB_PV[$i]}:STATE"
	if [ "$VERBOSE" == "yes" ]; then
	    echo "# INFO: ${FB_NAME[$i]} -> OFF"
	fi
	if [ "$SIMULATE" == "yes" ]; then
	    echo "# SIMULATE: [caput $modePv 0] Would set $modePv to COMPUTE mode "
	    echo "# SIMULATE: [caput $statePv 0] Would set $statePv to OFF mode"
	else
	    caput $modePv 0
	    caput $statePv 0
	fi
    done
}

function printUsage {
    echo "Usage: switchFF.sh [-h] [-i <FB_IOC>] [-d <platform>] [-s] [-v] [-e]"
    echo "  -h  : print this help"
    echo "  -i  : specify which feedbacks IOC (FB01 FB02 FB03 FB04 FB05) to switch"
    echo "  -d  : enable the VME or LinuxRT platform"
    echo "  -v  : verbose mode"
    echo "  -s  : simulate, don't actually switch feedbacks, just print actions that would be taken"
    echo "  -e  : use when running on the development environment"
}

# ==============================================================================
# MAIN
# ==============================================================================
if [ -z "$IOC" ]; then
    echo "Need to set $IOC variable"
    exit 5
fi

while getopts "hi:d:a:sve" arg; do
    case $arg in
	h) 
	    printUsage
	    exit 0
	    ;;
	i)
	    FB_IOC=$OPTARG
	    ;;
	d)
	    DEST=$OPTARG
	    ;;
	a)
	    AREA=$OPTARG
	    ;;
	s)
	    SIMULATE="yes"
	    ;;
	v)
	    VERBOSE="yes"
	    ;;
	e)
	    DEVELOPMENT="yes"
	    ;;
    esac
done

if [ "$FB_IOC" == "none" ]; then
    ans=$(zenity  --list  --text "Select feedbacks to switch:" --radiolist --width=750 --height=250 --column "" --column "Feedbacks" TRUE "FB01: GunLaunch/InjectorLaunch/XCAV/L2/BSY-XLaunch (This reboots ALL VME feedbacks)" FALSE "FB02: BunchCharge/L3/LI28/SlowLTU1/SlowLTU2" FALSE "FB03: LTU/UndulatorLaunch" FALSE "FB04: Longitudinal" FALSE "FB05: UndulatorLaunch120/BSY-YLaunch");

    if [ "$ans" == "" ]; then
	echo "Action cancelled"
	exit 1
    fi
    FB_IOC=`echo $ans | cut -d":" -f1`
fi

if [ "$FB_IOC" != "FB01" -a "$FB_IOC" != "FB02" -a "$FB_IOC" != "FB03" -a "$FB_IOC" != "FB04" -a "$FB_IOC" != "FB05" ]; then
    echo "ERROR: Invalid feedback $FB_IOC, try again"
    exit 2
fi

CURRENT_PLATFORM=(none none none none none)
CURRENT_STATUS=(0 0 0 0 0)

FB01_FB_NAME=(GunLaunch InjectorLaunch XCAV L2 BSY-XLaunch)
FB01_FB_PV=(FB01:TR01 FB01:TR02 FB01:TR03 FB01:TR04 FB01:TR05) 

FB02_FB_NAME=(BunchCharge L3 LI28 SLOWLTU SLOWLTU2)
FB02_FB_PV=(FB02:GN01 FB02:TR01 FB02:TR02 FB02:TR03 FB02:TR04)

FB03_FB_NAME=(LTU UndulatorLaunch)
FB03_FB_PV=(FB03:TR01 FB03:TR04)

FB04_FB_NAME=(Longitudinal)
FB04_FB_PV=(FB04:LG01)

FB05_FB_NAME=(UndulatorLaunch120 BSY-XLaunch)
FB05_FB_PV=(FB05:TR05 FB05:TR01)

case $FB_IOC in
    FB01)
	FB_NAME=( "${FB01_FB_NAME[@]}" )
	LINUX_VIOC=(in20-tr01 in20-tr02 li21-tr01 li21-tr02 bsy0-tr01)
	if [ "$DEVELOPMENT" == "yes" ]; then
	    LINUX_CPU=(cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01)
	else
	    LINUX_CPU=(cpu-sys0-fb02 cpu-sys0-fb02 cpu-sys0-fb01 cpu-sys0-fb01 cpu-sys0-fb01)
	fi
	FB_PV=( "${FB01_FB_PV[@]}" )
	STATUS_PV=(AO160 AO161 AO162 AO163 AO164)
	COMMENT_PV=(SO0160 SO0161 SO0162 SO0163 SO0164)
	zenity --question --text="Warning: switching FB01 feedbacks require rebooting all VME feedbacks, proceed?"
	ans=$?
	if [ "$ans" == "1" -o "$ans" == "" ]; then
	    echo "Action cancelled"
	    exit 3
	fi
	;;
    FB02)
	FB_NAME=( "${FB02_FB_NAME[@]}" )
	LINUX_VIOC=(sys0-gn01 li24-tr01 li28-tr01 ltu1-tr01 ltu1-tr02)
	if [ "$DEVELOPMENT" == "yes" ]; then
	    LINUX_CPU=(cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01 cpu-b34-fb01)
	else
	    LINUX_CPU=(cpu-sys0-fb02 cpu-sys0-fb01 cpu-sys0-fb01 cpu-sys0-fb01 cpu-sys0-fb01)
	fi
	FB_PV=( "${FB02_FB_PV[@]}" )
	STATUS_PV=(AO165 AO166 AO167 AO168 AO169)
	COMMENT_PV=(SO0165 SO0166 SO0167 SO0168 SO0169)
	;;
    FB03)
	FB_NAME=( "${FB03_FB_NAME[@]}" )
	LINUX_VIOC=(ltu1-tr03 und1-tr01)
	if [ "$DEVELOPMENT" == "yes" ]; then
	    LINUX_CPU=(cpu-b34-fb01 cpu-b34-fb01)
	else
	    LINUX_CPU=(cpu-sys0-fb01 cpu-sys0-fb02)
	fi
	FB_PV=( "${FB03_FB_PV[@]}" )
	STATUS_PV=(AO170 AO171)
	COMMENT_PV=(SO0170 SO0171)
	;;
    FB04)
	FB_NAME=( "${FB04_FB_NAME[@]}" )
	LINUX_VIOC=(sys0-lg01)
	if [ "$DEVELOPMENT" == "yes" ]; then
	    LINUX_CPU=(cpu-b34-fb01)
	else
	    LINUX_CPU=(cpu-sys0-fb02)
	fi
	FB_PV=( "${FB04_FB_PV[@]}" )
	STATUS_PV=(AO172)
	COMMENT_PV=(SO0172)
	;;
    FB05)
	FB_NAME=( "${FB05_FB_NAME[@]}" )
	LINUX_VIOC=(und1-tr02 bsy0-tr02)
	if [ "$DEVELOPMENT" == "yes" ]; then
	    LINUX_CPU=(cpu-b34-fb02 cpu-b34-fb02)
	else
	    LINUX_CPU=(cpu-sys0-fb02 cpu-sys0-fb02)
	fi
	FB_PV=( "${FB05_FB_PV[@]}" )
	STATUS_PV=(AO173 AO187)
	COMMENT_PV=(SO0173 SO187)
	;;
esac

getCurrentStatus

if [ "$CURRENT_PLATFORM[0]" == "select" ]; then
    ans=$(zenity  --list  --text "Switch to:" --radiolist --width=250 --height=200 --column "" --column "Platform" TRUE "VME" FALSE "LinuxRT");
	
    if [ "$ans" == "" ]; then
	echo "Action cancelled"
	exit 3
    fi
    DEST=$ans
else
    if [ "${CURRENT_PLATFORM[0]}" == "VME" ]; then
	question="VME feedbacks are currently enabled, do you want to enable LinuxRT?"
	DEST="LinuxRT"
    else
	question="LinuxRT feedbacks are currently enabled, do you want to enable VME?"
	DEST="VME"
    fi
    `zenity --question --text "$question"`
    ans=$?
    if [ "$ans" == "1" -o "$ans" == "" ]; then
	echo "Action cancelled"
	exit 3
    fi
fi

if [ "$DEST" != "VME" -a "$DEST" != "LinuxRT" ]; then
    echo "ERROR: Invalid platfrom, try again. Use either VME or LinuxRT"
    exit 4
fi

echo "Switching feedback $FB_IOC to $DEST"

VME_DIR=$IOC/ioc-$AREA-`echo $FB_IOC | tr [:upper:] [:lower:]`

if [ "$SIMULATE" == "yes" ]; then
    echo "# SIMULATE: Test if directory $VME_DIR exists"
fi

if [ ! -d $VME_DIR ]; then
    echo "ERROR: IOC directory $VME_DIR does not exist, try again"
    if [ "$SIMULATE" == "no" ]; then
	exit 6
    fi
fi

if [ "$VERBOSE" == "yes" ]; then
    echo "# INFO: Number of feedbacks: ${#LINUX_VIOC[@]}"
    echo "# INFO: LinuxRT feedbacks: ${LINUX_VIOC[@]}"
    echo "# INFO: PVs: ${FB_PV[@]}"
fi

# First check if all startup scripts exist
checkStartupScripts

# Then turn off feedbacks before making the switch
turnOffFeedbacks

# Change scripts, disabling PVs from one platform and enabling on the other
case $DEST in
    VME)
	changeScripts VME # Enable VME, Disable LinuxRT
	;;
    LinuxRT)
	changeScripts LinuxRT # Enable LinuxRT, Disable LinuxRT
	;;
esac

# Now reboot all IOC/VIOCs
rebootIOCs $DEST

# If rebooting failed, restore the startup scripts
if [ "$proceedWithReboot" != "0" ]; then
    case $DEST in
	VME)
	    changeScripts LinuxRT # Enable LinuxRT, Disable LinuxRT
	    FROM="LinuxRT"
	    ;;
	LinuxRT)
	    changeScripts VME # Enable VME, Disable LinuxRT
	    FROM="VME"
	    ;;
    esac
    zenity --error --text="Switch failed, no IOCs were rebooted.\nStill running $FROM feedbacks."
    exit 1
fi
    
# Update the STATUS_PV and COMMENT_PV Matlab PVs
updateStatusPVs $DEST

if [ "$DEST" == "VME" ]; then
    zenity --info --text="Switch completed, the VME IOC takes a couple of minutes to start up"
else
    zenity --info --text="Switch completed, LinuxRT feedbacks should be ready for use"
fi
