#!/bin/bash
#-*-sh-*-
function usage {
echo "
  aidaqc_show shows the AIDA klystron service presently assigned for each 
  klystron in a given sector. 

  Syntax:
     aidaqc_show -dpqlah <sector>

  aidaqc_show is used as part of the Linac Upgrade quickchangeover, to 
  see whether AIDA thinks it should be using SLCKlys (SLC klys control) 
  or EPICSKlys (EPICS klys control) for the klystron control of the
  given sector.

  Options:

  -d development (AIDADEV@SLACPROD) the db for AIDADEV network
  -p production  (AIDAPROD@SLACPROD) the db for AIDAPROD network
  -q QA          (AIDA@MCCQA) staging for AIDALCLS network
  -l LCLS        (AIDA@MCCO) the db for AIDALCLS network
  -a ALL         show klys control as specified in all AIDA databases above. 

  Examples: 
     aidaqc_show -a li28          - shows li28 klystron control in all 
                                    AIDA oracle databases
     aidaqc_show -lq li27         - shows only what is in AIDA databases 
                                    MCCQA and MCCO (for klsytron control 
                                    in sector LI27). 
"
}
#!============================================================
#! Auth: Greg White (greg)
#! Mod:  4-May-2011, greg, Removed assignment to TWO_TASK. For 
#!       AIDADEV and AIDAPROD it was wrong anyway. Leave connection string
#!       using fully qualified URI, so doesn't use TWO_TASK
#!============================================================
dev=0;
prod=0;
qa=0;
lcls=0;
all=1;

while getopts dpqlah opt
do
   case "$opt" in
       h) usage; exit 0 ;;
       d) dev=1; all=0 ;;
       p) prod=1; all=0 ;;
       q) qa=1; all=0 ;;
       l) lcls=1; all=0 ;;
       a) all=1;;
       *) echo "Unknown option, see aidaqc_show -h"; exit 1;;
   esac
done
shift $((OPTIND-1))
if [ $# -lt 1 ]; then
   echo "Too few arguments. There must be one argument, giving the sector name"
   echo "and optionally an option giving the databases. Eg aidaqc_show -a LI28"
   exit 1;
fi

sector=`(echo $1|tr '[[:lower:]]' '[[:upper:]]')`

echo "Getting password - takes weirdly long time!"
pwd=`getPwd aida`


if [ $dev -eq 1 -o $all -eq 1 ]; then
  echo AIDADEV - Show AIDA Control of sector $sector ...
  export TWO_TASK=SLACPROD
  export TNS_ADMIN=/usr/local/lcls/tools/oracle/wallets/aidadev
  sqlplus -S /@$TWO_TASK <<EOF
####  sqlplus -S aidadev/${pwd}@slacprod <<EOF 
     @aidaqc_show $sector
     quit
EOF
fi

if [ $prod -eq 1 -o $all -eq 1 ]; then
  echo AIDAPROD - Show AIDA Control of sector $sector ...
  export TWO_TASK=SLACPROD
  export TNS_ADMIN=/usr/local/lcls/tools/oracle/wallets/aidaprod
  sqlplus -S /@$TWO_TASK <<EOF
####  sqlplus -S aidaprod/${pwd}@slacprod <<EOF 
     @aidaqc_show $sector
     quit
EOF
fi

if [ $qa -eq 1 -o $all -eq 1 ]; then
  echo MCCQA - Show AIDA Control of sector $sector ...
  export TWO_TASK=MCCQA
  export TNS_ADMIN=/usr/local/lcls/tools/oracle/wallets/aida
  sqlplus -S /@$TWO_TASK <<EOF
#######  sqlplus -S AIDA/${pwd}@MCCQA <<EOF 
     @aidaqc_show $sector
     quit
EOF
fi

if [ $lcls -eq 1 -o $all -eq 1 ]; then
  echo MCCO - Show AIDA Control of sector $sector ...
  export TWO_TASK=MCCO
  export TNS_ADMIN=/usr/local/lcls/tools/oracle/wallets/aida
  sqlplus -S /@$TWO_TASK <<EOF
####  sqlplus -S AIDA/${pwd}@MCCO <<EOF 
     @aidaqc_show $sector
     quit
EOF
fi

exit 0;
