#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 12/07/10
#
#    Usage : . /symdev_star_list.ksh -s <Site>
#       or : . /symdev_star_list.ksh -s <Site> -V
#       or : . /symdev_star_list.ksh -V -s <Site>
#       or : . /symdev_star_list.ksh -h
#
#    Description : A script to report on the DMX or VMAX SRDF STAR devices
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 08.06.2010   1.1      Adam Radage      Initial version
# 09.07.2010   1.2      Adam Radage      Added Powerbroker check
# 09.07.2010   1.3      Adam Radage      Changed to function style
# 12.07.2010   1.4      Adam Radage      Amended Powerbroker check
# 12.07.2010   1.5      Adam Radage      Added VMAX -V option
# 11.05.2011   1.5      Adam Radage      Added -star_mode flag
#####################################################################
#
#####################################################################
# Variables
#####################################################################

# Variables for date & time to append to filenames to make them unique
 DATENOW=`date +"%d%m%y"`
 MONTH=`date +"%b%y"`

# General variables
 SYNTAX_NO=0
 PARAM_NO=$#
 SCRIPT=$(basename $0)
 LOGFILE="./logs/${SCRIPT}_${MONTH}.log"
 VERB=0

 EMCENV="/usr/storapi/bin/"
 PBENV="/opt/pb/bin/pbrun1"
 PBRUN="${PBENV} "

 # Check if Powerbroker is present, otherwise use full storapi path
 if [[ ! -e $PBENV ]]
  then
    PREFIX=$EMCENV
    echo "Powerbroker is not present, using path: ${PREFIX}"
  else
    PREFIX=$PBRUN
    echo "Powerbroker is present, using path: ${PREFIX}"
 fi

#####################################################################
# Functions
#####################################################################
function exit_error
{
  EXIT_CODE=$1
  EXIT_MESSAGE=$(echo $@ | cut -f 2- -d " ")

  echo "$(date):$SCRIPT:$EXIT_MESSAGE" 2>&1 >>./logs/${VMAX}log.${DATENOW}.log
  exit $EXIT_CODE
}

#####################################################################
function check_parameters
{

# Check to see whether the number of parameters is correct.

case $PARAM_NO in
  1 ) if [ $1 = "-h" ]				# Help
      then
        help_info
        VERB=0
      else
        echo "Incorrect parameters have been issued. For help type       ./symdev_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  2) if [ $1 = "-s" ]				# Site flag
      then
         SITE=$2
         VERB=1
         VMAX=""
         echo "Site is: $SITE"
         echo "Investigating DMX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symdev_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  3 ) if [ $1 = "-s" ] && [ $3 = "-V" ]	# Site flag and VMAX
      then
         SITE=$2
         VERB=2
         VMAX="VMAX_"
         echo "Site is: $SITE"
         echo "Investigating VMAX arrays..."
      elif [ $1 = "-V" ] && [ $2 = "-s" ]
      then
         SITE=$3
         VERB=2
         VMAX="VMAX_"
         echo "Site is: $SITE"
         echo "Investigating VMAX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symdev_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./symdev_list.ksh -h"
      exit_error 1 " Incorrect parameters have been issued";;
esac
}

#####################################################################
function create_file
{

# Create empty headed results file for output file
  echo "Site SID Device Type Size_(MB)">${SITE}_${VMAX}symdev_star_list_${DATENOW}.txt

}

#####################################################################
function get_info
{
 # Array variable to hold local array list 
 set CFG_LIST ""

 # Get list of local arrays and place in the array variable
 if [[ $VERB -eq 2 ]]						# If VERB=2, list VMAX arrays
  then
    set -A CFG_LIST `${PREFIX}symcfg list|grep -i Local|grep -i VMAX|awk '{print $1}'`
 else
    set -A CFG_LIST `${PREFIX}symcfg list|grep -i Local|grep -iv VMAX|awk '{print $1}'`
 fi

# Run through each array and issue symaccess commands
for SYMNAME in ${CFG_LIST[@]}
 do
   echo "`date`. Processing $SYMNAME ..." | tee -a $LOGFILE

   ${PREFIX}symdev -sid $SYMNAME list -star_mode|grep -i Grp\'d|sed 's/Not V/Not_V/'|sed 's/2-Way Mir/2-Way-Mir/'|sed 's/+/-/'|awk '{print $1, $5, $NF}'>${VMAX}symdev_star_list_${SYMNAME}.temp

   while read LINE
    do
      echo $SITE $SYMNAME $LINE|sed 's/ -/ 0/'>>${SITE}_${VMAX}symdev_star_list_${DATENOW}.txt
   done<${VMAX}symdev_star_list_${SYMNAME}.temp
   rm ${VMAX}symdev_star_list_${SYMNAME}.temp

   echo "`date`. Completed $SYMNAME ..." | tee -a $LOGFILE

done

}

#####################################################################
function help_info
{

# Help data
 echo "Help information"
 echo " Specify the site name by typing 
 	. /symdev_star_list.ksh -s <Site>"
 echo " Specify for VMAX arrays by typing 
      . /symdev_star_list.ksh -s <Site> -V"
 echo " or 
      . /symdev_star_list -V -s <Site>"
 echo " for help type 
      . /symdev_star_list.ksh -h"

}

#####################################################################
#####################################################################    
# Start of Main Script
#####################################################################

check_parameters $@

case $VERB in					# VERB not set to 0 so not -h option
1)   create_file					# -V not set (non-VMAX arrays)
     get_info;;
2)   create_file					# -V set (VMAX arrays)
     get_info;;
esac

exit 0

#####################################################################
#####################################################################

