#!/bin/bash
#-*-sh-*-
#==============================================================
#
#  Abs:   Lists "names" in the aida schema matching the given pattern.
#
#  Name:  aidalist
#
#  Rem:   This script returns the "instance" (and optionally "attributes"
#         too) matching the sqlplus pattern given in the arguments. It can
#         also tell you from where AIDA would get the data, using the -m arg. 
#
#         aidalist examines the value of AIDA_MODE to see which aida
#         directory to look in.  
# 
#  Usage: aidalist [-help or -m] <instance-pattern> [<attribute-pattern>]
#         
#         See help for examples.
# 
#  Side:  Accesses Oracle
#
#  Auth:  03-Feb-2005, Greg White (greg)
#  Rev:   dd-mmm-yyyy, Whoever    (user):
#
#--------------------------------------------------------------
# Mods:
#         06-May-2015, Bob Hall
#            Modified to setup Oracle correctly in DEV environments
#            (access to AFS) as well as production environments.
#         03-Dec-2014, Greg White
#            Switched from AIDALCLS db to AIDAPROD db
#         21-Sep-2011, Bob Hall
#            Changed Oracle password.
#         04-Apr-2011, Bob Hall
#            Changed Oracle password.
#         15-Sep-2010, Bob Hall
#            Changed Oracle password. 
#         23-Mar-2010, Bob Hall
#            Changed Oracle password. 
#         12-Mar-2010, Greg White
#            Switch this aidalist to using MCCO, and convert tabs to spaces
#            in output
#         28-Oct-2009, Greg White
#            Added support for metadata.
#         26-Aug-2009, Bob Hall
#            Changed Oracle password. 
#         25-Aug-2008, Bob Hall
#            Changed Oracle password. 
#         13-Feb-2008, Bob Hall
#            Changed Oracle password. 
#         05-Dec-2007, Greg White
#            Converted to bash, ported to LCLS CA.
#         21-Jun-2007, Bob Hall
#            Changed Oracle password. 
#         07-May-2005, Greg White (greg)
#            Added -help arg processing.
#         14-Feb-2005, Greg White (greg)
#            Fix comparator for checking whether 2nd arg is null.
#
#
#============================================================== 

function usage {
    echo "
Usage: `basename $0` [-h][-m] instance attribute
   -h   Show this help
   -m   Show aida metadata, presently shows from where AIDA would get value 

   \"instance\" is an AIDA instance name, such as a device name
   \"attribute\" is an AIDA attribute, such as a device field name"

    echo "
aidalist is used to get the names of things that the Accelerator
Independent Data Access (AIDA) system knows about. In general these
things are accelerator devices or aggregates of devices, and AIDA can
be used to get the reading value, or in some cases to set the value,
of such devices. aidalist can be used in combination with aidaget to 
get the present value.

Examples: 
            aidalist XCOR:PR10:9042       - Confirms XCOR:PR10:9042 is known
            aidalist XCOR:PR10:9%         - All \"9 something\" XCOR in PR10
            aidalist XCOR:PR10:9042 X%    - All X attributes of XCOR:PR10:9042
            aidalist XCOR:PR10:9042 %     - All attributes of XCOR:PR10:9042
            aidalist XCOR% %              - All XCOR with all their attributes
            aidalist XCOR:LI%:502 twiss   - Which linac 502 units have twiss 
            aidalist -m XCOR:LI27:502 effective_length
                                          - Shows how AIDA gets the 
                                            effective_length of this device.
            aidalist -m XCOR:LI27:502 %  Shows what AIDA knows about the
                                            attributes of this single SLC 
                                            device. Only instances describing 
                                            SLC devices are complete when only
                                            the first 3 fields are given.
            aidalist -m XCOR:LI27:502% - Lists what AIDA knows about all
                                            attributes of XCOR:LI27:502, from
                                            EPICS, SLC or anywhere else.
            aidalist -m XCOR:LI27:%    - Shows what AIDA knows about all 
                                          - attributes of all instances 
                                            matching the pattern XCOR:LI27*."
    echo
    exit 0
}   

# Process options and arguments. There is only one option, -h, meaning help.
# There must be 1 or 2 arguments. The 1st is the instance name. An attribute 
# name may follow the instance name. 
#
m=
while getopts hm opt; do
    case $opt in
	h) usage ;;
        m) m=1 ;shift;;
	*) printf "option \"$opt\" not recognized. Try aidalist -h for help\n" ;;
    esac
done

if [ $# -le 0 -o $# -gt 2 ]; then 
    echo "At least the instance name must be given, it may be followed"
    echo "by an attribute. Try aidalist -h for help."
    exit 1
fi

# Set up oracle
#
if [ -d /afs/slac/g/lcls ]; then
    source /afs/slac/g/lcls/tools/oracle/oracleSetup_11g.bash
    export TNS_ADMIN=/afs/slac/g/lcls/tools/oracle/wallets/aidaprod
else
    if [ -f /usr/local/lcls/epics/setup/oracleSetup.bash ]; then
        source /usr/local/lcls/epics/setup/oracleSetup.bash
    fi 
    export TNS_ADMIN=/usr/local/lcls/tools/oracle/wallets/aidaprod
fi

# Set the AIDA db specifics; we are talking to the SLACPROD instance,
# using schema (aka user) aidaprod (or aidadev if the AIDA_MODE is development)
#
export TWO_TASK=SLACPROD
schema=aidaprod


DB=$schema/gver3d_i

if [ -z $m ]; then
    if [ x$2 != x"" ]; then
	export cmd="@aidalistIA $*"
    else
	export cmd="@aidalistI $*"
    fi
else
    if [ x$2 != x"" ]; then
        export cmd="@aidalistIA_meta $1 $2"
    else
        export cmd="@aidalistI_meta $1"
    fi
fi

# Execute the aidalist*.sql command in $cmd. This is done using
# redirection, rather than as a command line arg, so that the sql quit
# can be appended, rather than putting the quit in the .sql command
# file, which would make it less useful as a utility.
#

#sqlplus -S $DB <<EOF | tr '\t' ' ' | sort
sqlplus -S /@$TWO_TASK <<EOF | tr '\t' ' ' | sort
$cmd
quit;
EOF

exit $?
