#! /bin/bash -norc
#
#==============================================================
#  Abs:  update feedback actuator reference values with current setting
#
#  Name: update_actuator_refs
#
#  Facility:  SLAC
#
# Author:  N. Lipkowitz 7/9/09
#
# Modified: 07/22/2009 D. Fairley (dfairley)
#    added INL0, L3L feedback actuators, enhanced message, added exit function call
#           29/11/2010 W. Colocho (colocho)
#    input 2 passes ANSWER so that update_all_act_refs can run
#
# This script takes a feedback name i.e. "INL1" or "B1L0" or whatever as an argument
# and updates the actuator reference PVs for all of its actuators. It works only for 
# the transverse feedbacks.
#
# It can be called from the command line, eg: >./update_act_refs B1L0
# and is called from the Update Act Refs button on the 
# feedback EDM screens.
#
# This just cagets the current value of e.g. XCOR:IN20:221:BACT and 
# caputs it into the pv called XCOR:IN20:221:BFBCKB5L0RSTR
#
# Only set up for feedbacks that have magnet actuators, i.e. those
# with "<pv>:BACT" as the actuator.
#
# This is a temporary solution for now.
# Rather than explicitly define the names of the actuators below,
# it would probably be preferable to parse the XML config file for
# the feedback and extract the names from that.  This would make this script
# impervious to changes in feedback configuration.
#
#==============================================================
B5L0ACTS='XCOR:IN20:221 YCOR:IN20:222'
INL0ACTS='XCOR:IN20:381 YCOR:IN20:382 XCOR:IN20:521 YCOR:IN20:522'
INL1ACTS='XCOR:IN20:381 YCOR:IN20:382 XCOR:IN20:521 YCOR:IN20:522'
INL2ACTS='XCOR:IN20:381 YCOR:IN20:382'
INL3ACTS='XCOR:IN20:521 YCOR:IN20:522'
INL4ACTS='XCOR:IN20:721 YCOR:IN20:722'
B1L0ACTS='XCOR:LI21:101 YCOR:LI21:136'
L2L0ACTS='XCOR:LI21:275 YCOR:LI21:276 XCOR:LI21:302 YCOR:LI21:303'
L3L0ACTS='XCOR:LI25:202 YCOR:LI24:900 XCOR:LI25:602 YCOR:LI25:503'
L280ACTS='XCOR:LI28:202 YCOR:LI27:900 XCOR:LI28:602 YCOR:LI28:503'
DL20ACTS='XCOR:LTUH:115 YCOR:LTUH:105'
LTU0ACTS='XCOR:LTUH:288 YCOR:LTUH:293'
LTL0ACTS='XCOR:LTUH:488 YCOR:LTUH:493 XCOR:LTUH:558 YCOR:LTUH:593'
UND0ACTS='XCOR:LTUH:758 YCOR:LTUH:747 XCOR:LTUH:858 YCOR:LTUH:877'
ACTUATOR_LIST="${1}ACTS"

for ACTUATOR in ${!ACTUATOR_LIST}
do
  ACT_CTRL_PV="${ACTUATOR}:BACT"
  ACT_REF_PV="${ACTUATOR}:BFBCK${1}RSTR"
  ACT_CURR=$(caget -t $ACT_CTRL_PV)
  ACT_REF_OLD=$(caget -t $ACT_REF_PV)
  echo "replacing ${1} actuator reference for ${ACTUATOR}: $ACT_CURR replaces $ACT_REF_OLD"
done

if test "${2}" != "y"
then
echo "Press y (yes) to replace values, or return to abort ..."

read ANSWER

else
  ANSWER="${2}"	
fi

if test "$ANSWER" == "y"
then 
  for ACTUATOR in ${!ACTUATOR_LIST}
   do
	ACT_CTRL_PV="${ACTUATOR}:BACT"
	ACT_REF_PV="${ACTUATOR}:BFBCK${1}RSTR"
	ACT_CURR=$(caget -t $ACT_CTRL_PV)
	ACT_REF_OLD=$(caget -t $ACT_REF_PV)
    # uncomment the following line to make this script actually do something
    caput $ACT_REF_PV $ACT_CURR
	done
  echo "reference values replaced"
else
  echo "aborting: no references were replaced"
fi
exit
