#!/usr/bin/ksh

# Original written by billw
# Customized by billm

# Purpose:  BPPs can not be added to a datastore session unless they have a link assigned.
#           Need to determine how many links are on a BPP.


. /opt/platform7/lbin/p7profile

WORKDIR=/h/bmetzger/s/link_stuff

ETR=${WORKDIR}/wm.etr
COMMS=${WORKDIR}/wm.comms
LINKS=${WORKDIR}/links.txt
HW=${WORKDIR}/hw.txt
JUNK=${WORKDIR}/linkstuff_junk
JUNK2=${WORKDIR}/linkstuff_junk2
RESULTS=${WORKDIR}/number_of_links_per_bpp.out
LINKS_PER_DSP=${WORKDIR}/number_of_links_per_dsp.out

if [ -f ${JUNK2} ]; then rm ${JUNK2}; fi
if [ -f ${RESULTS} ]; then rm ${RESULTS}; fi
if [ -f ${LINKS_PER_DSP} ]; then rm ${LINKS_PER_DSP}; fi


# link format from ParseErtLinks
#Site;Int Type;CC;HSP;Slot;Bearer;Timeslot;Proc;DataCap;Mon_PC     ;Rem_PC     ;STP_IP;SLC;CLLI       ;LSN        ;Disabled;Rem_IP
#7   ;J6736A/1;3 ;-  ;2   ;0,0   ;15,16   ;2   ;-      ;001-055-000;001-006-002;-     ;0  ;001-006-002;NYCTNYLJDS0;;
#2   ;J6738A/1;23;-  ;3   ;0,1   ;1       ;3   ;-      ;238-001-000;001-016-040;-     ;0  ;001-016-040;JRCYNJLCI01;;
#2   ;J6739A  ;15;-  ;4   ;7,2,0 ;-       ;1   ;-      ;238-001-000;AK_IP      ;172.19.114.74 AND ipv4.addr.dest == 172.25.20.13 ;0:1;AK_IP;;;


# create current copy of files required
  count=2;   # uncomment if you don't want to dump ETR
# count=`ls /var/opt/platform7/tmp/*.tlf 2>/dev/null | wc -l `

 if ((${count} > 0))
   then
         print -n "ETR is locked.  Will continue using existing files. "
         ${WORKDIR}/ParseEtrLinks  ${ETR}  ${LINKS}  ${HW}
    else
         print  "ETR is available.  Dumping ETR "
         time p7config -db ${ETR}    2> /dev/null

#         print  "Dumping comms config "
          p7updatecomms -d ${COMMS}   > /dev/null 2>&1

#         print  "Running ParsEtrLinks"
         ${WORKDIR}/ParseEtrLinks  ${ETR}  ${LINKS}  ${HW}
    fi

# construct numbers needed from comms config

#from: comms config
#    name     :13.site.cc.slot:IP address
#  14BPP1C15S1:13.1   .15.1   :192.168.46.232
#
#from: comms config
#  name  :5.site.0.proc#:IP address
#  DSP1S1:5.1   .0.1    :192.168.46.200

# Create list of sites and DSP proc#
# Result is: site  DSP_proc#  IP
#
grep -e ":5." ${COMMS} | awk 'BEGIN {FS=":"; } $NF = 3  {print $2} ' |  awk 'BEGIN {FS="."; } {print $2 " "$4}' > ${WORKDIR}/DSP_LIST

#from:  /etc/opt/platform7/resources/datastore/scalar/DS_Datastore_dist
#<site>:<lyds processor number>-<lightyear cardcage>:<BPP slot>

# Get matching BPP_slot and CC#  
# Result is:  site lyds_processor_number lightyear_cardcage BPP_slot
#
cat /etc/opt/platform7/resources/datastore/scalar/DS_Datastore_dist | sed -e 's/[:|-]/ /g' | awk '/^[0-9]/ {print} ' > ${WORKDIR}/parsed_mapping

# The purpose of this whole exercise is to determine  whether the CC (and up to 8 BPPs) associated with a LYDS has any links configured.

print  "Creating links/bpp list "
while read site dspnumb
do
     cat ${WORKDIR}/parsed_mapping | 
        awk '{ if ( $1 == "'$site'" && $2 == "'$dspnumb'" ) 
                   printf ("all_parts %s %s %s %s\n", $1, $2, $3, $4);
#                   printf ("site:%s  dsp:%s match site:%s dsp:%s cc:%s slot:%s\n", "'$site'",  "'$dspnumb'", $1, $2, $3, $4);
                else 
                    print "\n"
            }' >> ${JUNK2}

done  < ${WORKDIR}/DSP_LIST

grep all ${JUNK2} > ${JUNK}  # get rid of extra line feeds

# Now, parse output from Ross script, and get number of links per BPP 
#
while read text site dsp cardcage BPP
do
   # add dsp hostname so I won't have to manually convert in summary
   #
   dsp_hostname=`grep ds0 ${ETR} | grep "^$site $dsp" | awk ' {print $NF}'`

let IPcount=$(egrep "^$site;J6739A;$cardcage;" links.txt | wc -l)
let IPdirect=$(egrep "^$site;J4198A;$cardcage;" links.txt | wc -l)
let LSHScount=$(egrep "^$site;J673.A..;$cardcage;" links.txt | wc -l)

if [[ $LSHScount -gt 0 ]] ; then
        let count=$(egrep "^$site;J673.A..;$cardcage;-;$BPP;" links.txt | wc -l)
elif [[ $IPdirect -gt 0 ]] || [[ $IPcount -gt 0 ]] ; then
        let count=$(egrep "^$site;J4198A;$cardcage;-;$BPP;" links.txt | wc -l)
        if [[ $IPcount -gt 0 ]] ; then
           let count=$count+$(egrep "^$site;J6739A;$cardcage;" links.txt | cut -d ";" -f 8 | sort | grep $BPP | wc -l)
        fi
else
        let count=0
fi
  
   echo $text | awk '{printf ("%3s links configured on site %2s cardcage %2s BPP %2s DSP %2s  %s\n", "'$count'", "'$site'", "'$cardcage'", "'$BPP'", "'$dsp'", "'$dsp_hostname'") }' >> ${RESULTS}

done < ${JUNK}

# Parse links/BPP output, and get what we really wanted, links/dsp
#
print  "Creating total links/dsp list "
while read site dsp 
do
        cardcage=`cat ${WORKDIR}/parsed_mapping | grep -E "^$site $dsp"  | cut -d" " -f3 | head -1'` 
        dsp_hostname=`grep ds0 ${ETR} | grep "^$site $dsp" | awk ' {print $NF}'`

        cat ${RESULTS} | 
        awk ' BEGIN { links_per_dsp = 0; }
              { if ( $6 == "'$site'" && $12 == "'$dsp'" ) 
                   links_per_dsp = links_per_dsp + $1; 
              }
             END { printf ("%3s total links on site %2s  cardcage %2s  datastore %s  %s\n", links_per_dsp, "'$site'",  "'$cardcage'", "'$dsp'", "'$dsp_hostname'") }
            '  >> ${LINKS_PER_DSP}
done < ${WORKDIR}/DSP_LIST

# stopped here
# add name of datastore to results

#cleanup
if [ -f ${JUNK} ]; then rm ${JUNK}; fi
if [ -f ${JUNK2} ]; then rm ${JUNK2}; fi

if [ -f  ${WORKDIR}/parsed_mapping ]; then rm ${WORKDIR}/parsed_mapping; fi 
if [ -f  ${WORKDIR}/DSP_LIST ]; then rm ${WORKDIR}/DSP_LIST; fi
if [ -f  ${LINKS} ]; then rm ${LINKS}; fi
if [ -f  ${HW} ]; then rm ${HW}; fi

