#!/bin/sh
# This script is designed to check if the PGP PVs are correct after a restart.
# Setup Guide: DEVICE PGP CH

# Wait to Ensure Values are Set
sleep 5s

# Reset Timing Settings
sctiming=$(caget -t $1:CAM_SC_TIMING)
nctiming=$(caget -t $1:CAM_NC_TIMING)
temp1=$(caget -t $1:$sctiming)
temp2=$(caget -t $1:$nctiming)

if ($1:$sctiming -eq 0)
then
	zenity --list --window-icon=warning --title="Event Code set to default" --text="The Event Code has been reset." \ 
		"Please check Archive Viewer for correct value to input." &
else
caput $1:$sctiming 0
sleep 1s
caput $1:$sctiming "$temp1"
fi

if ($1:$nctiming -eq 0)
then
	zenity --list --window-icon=warning --title="Event Code set to default" --text="The Event Code has been reset." \ 
		"Please input 155 for HXR, 165 for SXR, 159 for everywhere else." &
else
	caput $1:$nctiming 0
	
sleep 1s
	caput $1:$nctiming "$temp2"
fi

# Check if Acquire Time is set to default
if ($1:AcquireTime -eq 3.2e-5)
then
	zenity --list --window-icon=warning --title="Exposure time set to default" --text="The Exposure time has been reset to default (3.2e-5)." \ 
		"Please check Archive Viewer for correct value to input." &
elif ($1:AcquireTime -eq 1.0e-6)
then
	zenity --list --window-icon=warning --title="Exposure time set to default" --text="The Exposure time has been reset to default (3.2e-5)." \ 
		"Please check Archive Viewer for correct value to input." &
else
	echo Acquire Time Set Correctly
fi

# Check if Acquire Time is the same
if ($1:AcquireTime != $1:AcquireTime_RBV)
then
	echo Acquire Time Failed to Match
	temp=$(caget -t $1:AcquireTime.VAL)
	caput $1:AcquireTime 0
	sleep 1s
	caput $1:AcquireTime "$temp"
else
	echo Acquire Time Matches
fi

# Check if Camera Trigger Delay is the same
if ($1:CamTrigDelay != $1:CamTrigDelay_RBV)
then
	echo Camera Trigger Delay Failed to Match
	temp=$1:CamTrigDelay.VAL
	caput $1:CamTrigDelay 0
	sleep 1s
	caput $1:CamTrigDelay "$temp"
else
	echo Camera Trigger Delay Matches
fi

# Check if Trigger Source is the same
if ($2:Ch$3:Timing:TriggerSource != $2:Ch$3:Timing:TriggerSource_RBV)
then
	echo Trigger Source Failed to Match
	temp=$(caget -t $2:Ch$3:Timing:TriggerSource)
	caput $2:Ch$3:Timing:TriggerSource "$temp"
else
	echo Trigger Souce Matches
fi

# Check if Rate Type is the same
if ($2:Ch$3:EvrV2:RateType != $2:Ch$3:EvrV2:RateType_RBV)
then
	echo Rate Type Failed to Match
	temp=$(caget -t $2:Ch$3:EvrV2:RateType)
	caput $2:Ch$3:EvrV2:RateType "$temp"
else
	echo Rate Type Matches
fi
