#!/bin/bash
#=======================================================================
# 
# filename: ioc_check
# 
# Desc: this script has one input argument which is the name of a file
# with a list of iocs. This script then compars this list against what
# is in the screeniocs file, and counts those found,not found, and the
# total of iocs in the input file
#
# Usage:   ioc_check <filename>
#
# Author:  20-Apr-2018, K. Luchini     (luchini)
#-----------------------------------------------------------------------
# Mods: (newest to oldest)
#          dd-mmm-yyyy, First Lastname (username):
#
#=======================================================================
#
set -f                  # disable globbing
IFS=$'\n'               # make newlines the only separator
master_search_file="$EPICS_IOCS/screeniocs"
search_file="screeniocs_without_comments"
good_ioc_cnt=0
bad_ioc_cnt=0
nodir_cnt=0
ioc_cnt=0

# Remove file generated by this script from the last run
rm -f "iocs_found"
rm -f "iocs_not_found"
rm -f "nodir_found"
rm -f $search_file

# Check number of iput arguments
if [ $# -ne 1 ]; then
  echo "Usage: $0 <filename>"
  echo "Example: $0 ioc_list"
  exit 1
fi
filename=$1             # file containes list of ioc names in $EPICS_IOCS

# create a temporary screeniocs file without commented out lines
egrep -o '^[^#]*' $master_search_file > $search_file 

# Check of the iocs from the input file are the screeniocs file.
for i in $(cat < "$filename"); do
  if [ ! -d $EPICS_IOCS/$i ]; then
    echo "Warning: Directory for $EPICS_IOCS/$1 does not exist!"
    echo "$i" >> nodir_found
    nodir_cnt=$((nodir_cnt+1))
  elif [ -n "$(egrep -i "$i" $search_file)" ]; then
    echo "$i" >> iocs_found
    good_ioc_cnt=$((good_ioc_cnt+1))
    echo "Found $i in screeniocs"
  else
    echo "$i" >> iocs_not_found
    echo "Warning: $i not found in $EPICS_IOCS/screeniocs!"
    bad_ioc_cnt=$((bad_ioc_cnt+1))
  fi
  ioc_cnt=$((ioc_cnt+1))
done
echo "Number iocs in $filename: "$ioc_cnt " (from $EPICS_IOCS)"
echo "Number of iocs in screeniocs: "$good_ioc_cnt
echo "Number of iocs not in screeniocs: "$bad_ioc_cnt
echo "Number of iocs where directory does not exists: "$nodir_cnt


# End of file
