#!/usr/bin/perl

# purpose:
# verify the format of the links are correct

# customize:
#  - location of ETR
#  - location of DB
#  - specify monitored end as FTW_ISTP or STK_ISTP 


use English;
use Carp;
use Cwd;
use DBI;
use Net::Ping;
use File::Basename;
use File::Path;
use File::Glob;
use File::Copy;


# db
# LS9231 0 166 D ARGENTINA_STPN1

# ETR
#
# LS9231 
# SS7 STK_ISTP ARGENTINA_STPN1 D LS9231 1
# Q703:NxDS0 0 CCITT-BLUE STI-ARGTN_STP1/0 STI-LS9231/0-166 acceSS7 ENABLED \

#  ARGENTINA_STPN1 
# SSP ARGENTINA_STPN1 274 ARGENTINA_STPN1 "" 0 REM SSP IN:7-44-4;ITU383 "" "" ""
# SS7 STK_ISTP ARGENTINA_STPN1 D LS9231 1

my $ETR = "/tmp/wm/wm.etr";

#my $DB = "/h/bmetzger/s/p/db2";
#my $PreName = "FI";
#my $PreAltName = "FTI";

my $DB = "/h/bmetzger/s/p/db.tmp";
my $PreName = "SI";
my $PreAltName = "STI";

  open DB, "<$DB" or croak "Can't open DB for read: $!\n";
  while (my $dbline = <DB>) {
#  @dblines = <DB>; 
#  foreach $dbline (@dblines) { 
        chomp $dbline;
        next if $dbline =~ /^$/;
        ($dblinkset, $dbslc, $dblinknumb, $dblinktype, $dbname) = split(/\s+/,$dbline);
        $dblinknumb = sprintf "%03d", $dblinknumb;   #zero fill

#print "$dblinkset\n";
          open (ETR, "<$ETR") or croak "Can't open etr for read: $!\n";
          @etrlines = <ETR>; 
          foreach $etrline (@etrlines){
              next if $etrline =~ /^$/;
              if ( ($etrline =~ /$dblinkset/) && ($etrline =~ /acceSS7/) ) {
                 ($junk1, $etrnumb, $personality, $Name, $altName, $monSystem) = split(/\s+/,$etrline);
              
                  if ($Name =~ /$PreName-$dbname\/$dbslc/){
#                       printf "etr: %-25s  db: %s-%s/%s --Name ok\n",$Name, $PreName, $dbname, $dbslc ;
                  }else {
                       printf "etr: %-25s  db: %s-%s/%s   %s  --Name mismatch\n",$Name, $PreName, $dbname, $dbslc, $dblinkset ;
                  }

                  if ($altName =~ /$PreAltName-$dblinkset\/$dbslc-$dblinknumb/){
#                       printf "etr: %-25s  db: %s-%s/%s-%s --AltName ok\n",$altName, $PreAltName, $dblinkset, $dbslc, $dblinknumb ;
                  }else {
                       printf "etr: %-25s  db: %s-%s/%s-%s   %s  --AltName mismatch\n",$altName, $PreAltName, $dblinkset, $dbslc, $dblinknumb, $dblinkset ;
                  }
              } #matches linkset
          }
          close (ETR);
  }
   close (DB);
