#!/bin/bash
#==============================================================
#  Name:  lu_PV_translator
#
#  Rem:   
# 
#  Usage: 
#
#  Auth:  11-Nov-2010,  S. Hoobler (SONYA)
#  Rev:   dd-mmm-yyyy,  Whoever    (user):
#
#==============================================================

function userhelp {
    echo "    "
    echo "#==============================================================" 
    echo "    Help:"
    echo "         Name:  lu_PV_translator"
    echo "         Usage: lu_PV_translator"
    echo "         Arg:   None"
    echo "         Ret:   None"
    echo "    "
    echo "    "
    echo "    -Translates Linac Upgrade signal names from Old (SLC) signal name"
    echo "      to New (EPICS) signal name and vice versa."
    echo "    -You will be asked if you are translating Old to New. If you answer n,"
    echo "      it will be assumed that you are translating New to Old."
    echo "    -If translating Old to New, you will enter either the SLCCAS PV name"
    echo "      OR the SLC primary, micro, secondary/channel_name. The EPICS PV name will be"
    echo "      returned."
    echo "    -If translating New to Old, you will enter the EPICS PV name."
    echo "      The SLCCAS PV name will be returned."
    echo "    "
    echo "    Here's an example:"
    echo "    "
    echo "        \$Welcome to the Linac Upgrade PV name decoder ring."
    echo "        \$Type help at the prompt for instructions and examples."
    echo "        \$"
    echo "        \$Are you translating Old to New (y/n/help)? y"
    echo "        \$Enter the SLCCAS PV name OR the primary, micro secondary/channel_name separated by spaces:"
    echo "        \$ACCC LI25 DC"
    echo "        \$"
    echo "        \$Searching for ACCC LI25 DC"
    echo "        \$Results:"
    echo "        \$KLYS:LI25:1:FOCUSBULK_OFFCMD"
    echo "        \$KLYS:LI25:1:FOCUSBULK_ONCMD"
    echo "        \$KLYS:LI25:1:FOCUSBULK_STATE"
    echo "        \$Done"
    echo "        \$"
    echo "        \$Would you like to translate another PV (y/n/help)? y"
    echo "        \$Are you translating Old to New (y/n/help)? y"
    echo "        \$Enter the SLCCAS PV name OR the primary, micro secondary/channel_name separated by spaces:"
    echo "        \$LI27:ASTS:DL_WG_01"
    echo "        \$"
    echo "        \$Searching for LI27:ASTS:DL_WG_01"
    echo "        \$Results:"
    echo "        \$KLYS:LI27:11:DL_WG_TEMP"
    echo "        \$Done"
    echo "        \$"
    echo "        \$Would you like to translate another PV (y/n/help)? y"
    echo "        \$Are you translating Old to New (y/n/help)? n"
    echo "        \$Enter EPICS PV Name: MMG:LI26:1:P"
    echo "        \$Searching for MMG:LI26:1:P..."
    echo "        \$"
    echo "        \$Results:"
    echo "        \$LI26:ASTS:MMG_VAC"
    echo "        \$Done"
    echo "        \$"
    echo "        \$Would you like to translate another PV (y/n/help)? N"
    echo "        \$Exiting"
    echo "    "
    echo "    If you encounter any problems or missing signals, please"
    echo "    notify Sonya Hoobler, sonya@slac.stanford.edu."
    echo "#==============================================================" 
    echo "    "
}

function translate {
    echo -n 'Are you translating Old to New (y/n/help)? '
    read reply
    reply=`echo ${reply} | tr '[:lower:]' '[:upper:]'`
    
    if [ "${reply}" = "HELP" ]; then
	userhelp
    else

	if [ "${reply}" = "Y" ]; then 

	    echo 'Enter the SLCCAS PV name OR the primary, micro, secondary/channel_name separated by spaces:'
	    read prim micr name
	    prim=`echo ${prim} | tr '[:lower:]' '[:upper:]'`
	    micr=`echo ${micr} | tr '[:lower:]' '[:upper:]'`
	    name=`echo ${name} | tr '[:lower:]' '[:upper:]'`

	    echo

	    if [ "${micr}" = "" -a "${name}" = "" ]; then
		slccas=${prim}
		echo Searching for ${slccas}
		match=`grep "ALIAS.*${slccas}" $TOOLS/script/lu_PV_translation_table | awk ' {FS = "ALIAS" ; print $1 } '`
	    else
		echo Searching for ${prim} ${micr} ${name}
		match=`grep "${micr}:${prim}.*${name}" $TOOLS/script/lu_PV_translation_table | awk ' {FS = "ALIAS" ;  print $1 } '`
	    fi
	    if [ "${match}" = "" ]; then
		echo No match found
	    else
		echo Results:
		echo ${match} | tr ' ' '\n'
		    echo Done; echo
	    fi
	else

	    if [ "${reply}" = "N" ]; then

		echo -n 'Enter EPICS PV Name: '
		read epicspv
		epicspv=`echo ${epicspv} | tr '[:lower:]' '[:upper:]'`
		echo Searching for ${epicspv}...; echo
		match=`grep "${epicspv}.*ALIAS" $TOOLS/script/lu_PV_translation_table | awk ' {FS = "ALIAS" ;  print $3 } '`

		if [ "${match}" = "" ]; then
		    echo No match found; echo
		else
		    echo Results:
		    echo ${match} | tr ' ' '\n'
		    echo Done; echo
		fi
	    else
		echo "Did not understand reply. Let's try again."
		translate
	    fi
	fi
    fi


}


function more {

  echo -n "Would you like to translate another PV (y/n/help)? "
  read reply
  reply=`echo ${reply} | tr '[:lower:]' '[:upper:]'`

  if [ "${reply}" = "HELP" ]; then
      userhelp
      more
  else
      if [ "${reply}" = "Y" ]; then 
	  return
      else
	  if [ "${reply}" = "N" ]; then 
	      echo Exiting
	      exit 0
	  else
	      echo Did not understand reply.
	      more
	  fi
      fi
  fi
}

    
# Main program

echo 
echo Welcome to the Linac Upgrade PV name decoder ring.
echo Type help at the prompt for instructions and examples.
echo

while [ true ]
do
  translate
  more
done


