#! /bin/ksh 

#WORKDIR=/net/m7server/h/hpusbime/sprint/tools/find_disabled
WORKDIR=/home/a7users/ss7gjobi/src/find_disabled
TMPDIR=/$WORKDIR/tmp                      

OUTFILE=$WORKDIR/alllinks.results
DISABLED=$WORKDIR/disabled.results
DOMESTIC=$WORKDIR/domestic.only
PROVISIONING=$WORKDIR/provisioning.results
NOPRV=$WORKDIR/disabled_not_provisioning.results
DOMNOPRV=$WORKDIR/domestic_not_prv.results
ENABLED_PRV=$WORKDIR/enabled_prv.results
SWITCH_RESULTS=$WORKDIR/switch_results # slimmed down output listing only switch and disabled fields
ERRORFILE=$WORKDIR/error.results
TMP1=$WORKDIR/tmp1
TMP2=$WORKDIR/tmp2

#ETR_FILE=/tmp/wm.etr
ETR_FILE=$WORKDIR/jon.etr

LINKSTUFF=$WORKDIR/linkstuff_only      # link info only from ETR file

. /opt/platform7/lbin/profile       # setup environment for p7config

# Create dump of current ETR file
if [ -f $ETR_FILE ]; then rm $ETR_FILE ; fi
/opt/platform7/bin/p7config -db $ETR_FILE

# stop script if an ETR can't be created
# -s tests for zero length
if [ -s $ETR_FILE ]; then break ; fi

# Get only linkinfo from ETR file
awk '/^ \# Linksets/,/^\[sites\]/ {print}' $ETR_FILE | grep -v '#' > $LINKSTUFF

# put all data for a line  on single line
# (to have all data for individual group on diffent lines, add \n to each print statement)
awk 'BEGIN { RS = "";ORS = "\n\n"; FS = "\n";}
      $1 ~ /\[linkset\]/ {printf("%s %s\n", $1, $2)}
      $1 ~ /\[link\]/    {printf("%s %s %s\n", $1, $2, $3)}
#      $1 ~ /\[v35culinkinterface\]/ {printf("%s %s \n ", $1, $2)}
#      $1 ~ /\[linkapplications\]/ {printf("%s %s %s %s %s %s %s %s\n\n", $1, $2, $3, $4, $5, $6, $7, $8)}
    ' $LINKSTUFF > $TMP1

#remove quotes
sed -e 's/\"//g' $TMP1 > $TMP2

# header for output file
print "Monitored      Remote       .     Link      SLC     Link   Alt    .    Link" > $OUTFILE
print "Signal_Point  Signal_Point Type   Name     Number   Name   Name  Status No." >> $OUTFILE
print " " >> $OUTFILE

# copy header to other files
cp $OUTFILE $DISABLED
cp $OUTFILE $PROVISIONING
cp $OUTFILE $NOPRV
cp $OUTFILE $ENABLED_PRV

# Need this to add linkset info to non-0 SLR's
awk ' 
#     BEGIN { RS = "";FS = "\n";}

     function get_link_numb(some_var){
               size_of_array = split(some_var,array,"-")
               return array[3]
     }

{
      if ($1 ~ /^\[linkset\]/)
              { linksetinfo=""$2" "$3" "$4" "$6 "" };
     
      if ($1 ~ /^\[link\]/)
              { linkinfo="" " "$2" "$5" "$6" "$NF "" ;
                 link_number=get_link_numb($6);
                 print linksetinfo  linkinfo "  " link_number
              };

}' $TMP2 >> $OUTFILE

#   Extract lines that are disabled for provisioning
awk '
	($7 ~ /^PRV-/ || $7 ~ /^PR-/) {print}
' $OUTFILE >> $PROVISIONING

#   Extract lines that are disabled but not for provisioning

awk '
	$8 ~ /DISABLED/ && ($7 !~ /^PRV-/ && $7 !~ /^PR-/) {print}
' $OUTFILE >> $NOPRV

# This part is for the 2nd and 3rd file requested by Dan.
# It's purpose is to get just the switch info from 1st part and 3rd fields
# of variable 5 and field saying it's disabled.

awk '
     function get_switch_name(some_var){
               size_of_array = split(some_var,array,"-")
               return array[1]
     }

     function get_link_numb(some_var){
               size_of_array = split(some_var,array,"-")
               return array[3]
     }

{
      if ($1 ~ /^\[link\]/)
              { status=$NF ;
                 switch_info=get_switch_name($6);
                 link_number=get_link_numb($6);
                 print switch_info "  " link_number " "status
              };

}' $TMP2 > $SWITCH_RESULTS

# Now rename and put this in Dans directory with correct permissions.
cp $OUTFILE /home/a7users/bsotjoki/excel_files/all_links.txt
cp $PROVISIONING /home/a7users/bsotjoki/excel_files/disabled_provisioning.txt
cp $NOPRV /home/a7users/bsotjoki/excel_files/disabled_not_provisioning.txt
chown bsotjoki:a7grp /home/a7users/bsotjoki/excel_files/*

grep ENABLED $PROVISIONING >> $ENABLED_PRV
grep DISABLED $OUTFILE >> $DISABLED
grep -v IN: $DISABLED >> $DOMESTIC
grep -v PRV $DOMESTIC >> $DOMNOPRV

cp $DISABLED /home/a7users/pcshafo/excel_files/disabled_links.txt
cp $PROVISIONING /home/a7users/pcshafo/excel_files/disabled_provisioning.txt
cp $NOPRV /home/a7users/pcshafo/excel_files/disabled_not_provisioning.txt
cp $DOMESTIC /home/a7users/pcshafo/excel_files/domestic_disabled_links.txt
cp $DOMNOPRV /home/a7users/pcshafo/excel_files/domestic_disabled_not_prv_links.txt
cp $ENABLED_PRV /home/a7users/pcshafo/excel_files/enabled_prv.txt
chown pcshafo:a7grp /home/a7users/pcshafo/excel_files/*

cp $DISABLED /home/a7users/ss7floor/disabled_links.txt
cp $PROVISIONING /home/a7users/ss7floor/disabled_provisioning.txt
cp $NOPRV /home/a7users/ss7floor/disabled_not_provisioning.txt
cp $DOMESTIC /home/a7users/ss7floor/domestic_disabled_links.txt
cp $DOMNOPRV /home/a7users/ss7floor/domestic_disabled_not_prv_links.txt
cp $ENABLED_PRV /home/a7users/ss7floor/enabled_prv.txt
chown ss7floor:a7grp /home/a7users/ss7floor/*

cp $DISABLED /home/a7users/ss7wall/disabled_links.txt
cp $PROVISIONING /home/a7users/ss7wall/disabled_provisioning.txt
cp $NOPRV /home/a7users/ss7wall/disabled_not_provisioning.txt
cp $DOMESTIC /home/a7users/ss7wall/domestic_disabled_links.txt
cp $DOMNOPRV /home/a7users/ss7wall/domestic_disabled_not_prv_links.txt
cp $ENABLED_PRV /home/a7users/ss7wall/enabled_prv.txt
chown ss7wall:a7grp /home/a7users/ss7wall/*

cp $DISABLED /home/a7users/cperry/disabled_links.txt
cp $PROVISIONING /home/a7users/cperry/disabled_provisioning.txt
cp $NOPRV /home/a7users/cperry/disabled_not_provisioning.txt
cp $DOMESTIC /home/a7users/cperry/domestic_disabled_links.txt
cp $DOMNOPRV /home/a7users/cperry/domestic_disabled_not_prv_links.txt
cp $ENABLED_PRV /home/a7users/cperry/enabled_prv.txt
chown cperry:a7grp /home/a7users/cperry/*txt

cp $DISABLED /home/a7users/ss7gjobi/var/disabled_links/disabled_links.txt
cp $PROVISIONING /home/a7users/ss7gjobi/var/disabled_links/disabled_provisioning.txt
cp $NOPRV /home/a7users/ss7gjobi/var/disabled_links/disabled_not_provisioning.txt
cp $DOMESTIC /home/a7users/ss7gjobi/var/disabled_links/domestic_disabled_links.txt
cp $DOMNOPRV /home/a7users/ss7gjobi/var/disabled_links/domestic_disabled_not_prv_links.txt
cp $ETR_FILE /home/a7users/ss7gjobi/src/etr/etrfile.etr
cp $ENABLED_PRV /home/a7users/ss7gjobi/var/disabled_links/enabled_prv.txt
chown ss7gjobi:a7grp /home/a7users/ss7gjobi/var/disabled_links/*.txt

cp $OUTFILE /home/filexfer/links/all_links.txt
cp $DISABLED /home/filexfer/links/disabled_links.txt
cp $PROVISIONING /home/filexfer/links/disabled_provisioning.txt
cp $NOPRV /home/filexfer/links/disabled_not_provisioning.txt
cp $DOMESTIC /home/filexfer/links/domestic_disabled_links.txt
cp $DOMNOPRV /home/filexfer/links/domestic_disabled_not_prv_links.txt
cp $ENABLED_PRV /home/filexfer/links/enabled_prv.txt
chown filexfer:users /home/filexfer/links/*
chmod 644 /home/filexfer/links/*
cp $ETR_FILE /home/filexfer/etr/etrfile.etr
chown filexfer:users /home/filexfer/etr/etrfile.etr
chmod 644 /home/filexfer/etr/etrfile.etr

rm $ETR_FILE
rm $OUTFILE
rm $PROVISIONING
rm $NOPRV
rm $DISABLED
rm $LINKSTUFF
rm $DOMESTIC
rm $DOMNOPRV
rm $TMP1
rm $TMP2
rm $ENABLED_PRV
