#!/usr/bin/sh
#  @(#) Program name: GetSwlistings
#  @(#) Created:      04/11/02
#  @(#) Revision:     1.1, Modified: 02/12/03 , added front end
#  @(#) Revision:     1.2, Modified: 03/11/03 , added Datastore
#  @(#) Revision:     1.3, Modified: 06/28/06 , parse HP-UX and Linux Datastore
#  @(#) Revision:     1.4, Modified: 07/26/06 , split up HP-UX and Linux Datastore

Orig=orig_swlistings
Str=stripped
Conf=Configuration

PrintUsage() {
   print "\n\tusage: $0 [-c] [-s] [-w] [-p] [-d] [-ind proc [proc...]] [-docs]\n"
   print "\twhere\n\n\tdefault:  Get Swlistings on all processors in acceSS7 system"
   print "\t          (acceSS7 documentation not included)"
   print "\t     -c:  Get Swlistings on the Central Server"
   print "\t     -s:  Get Swlistings on all site processors"
   print "\t     -w:  Get Swlistings on all application servers and workstations"
   print "\t     -p:  Get Swlistings on all probe processors"
   print "\t     -d:  Get Swlistings on all datastore processors"
   print "\t  -indH:  Get Swlistings on individual processors (HP-UX only)"
   print "\t  -indL:  Get Swlistings on individual processors (Linux only)"
   print "\t  -docs:  Include acceSS7 documentation in Swlistings\n"
}

GetListings() {
   proc=$1
   print "Acquiring processors for \"list_processors -$proc\" ..."
   /opt/platform7/lbin/list_processors -$proc > $Conf/processors.$proc
 
   if [ $proc != "d" ]; then
      if [ -f $Conf/$Str.$proc ]; then rm $Conf/$Str.$proc ; fi
      for x in `cat $Conf/processors.$proc`
      do
        print "Getting swlist for $x ..."
        remsh $x "/usr/sbin/swlist -l fileset -a revision -a state" > swlist.$x
        print $Str/swlist.$x.$Str >> $Conf/$Str.$proc  # build `stripped` files list for Acquire_Patches
      done
   else
      if [ -f $Conf/$Str.$proc.hpux ]; then rm $Conf/$Str.$proc.hpux ; fi
      if [ -f $Conf/$Str.$proc.linux ]; then rm $Conf/$Str.$proc.linux ; fi
      if [ -f $Conf/$Str.$proc.other ]; then rm $Conf/$Str.$proc.other ; fi
      for x in `cat $Conf/processors.$proc`
      do
         OS=`ssh $x "uname"`
         if [ $? -eq 0 ]; then          # See if ssh command was successful
           if [ $OS = "HP-UX" ]; then
             print "Getting swlist for $x (HP-UX) ..."
             remsh $x "/usr/sbin/swlist -l fileset -a revision -a state" > swlist.$x
             print $Str/swlist.$x.$Str >> $Conf/$Str.$proc.hpux  # build `stripped` files list for Acquire_Patches
           elif [ $OS = "Linux" ]; then
             print "Getting rpm listing for $x (Linux) ..."
             ssh $x "rpm -qa" | grep -e Platform7 -e ^a7 -e ^A7 -e ^p7 -e ^P7 \
               -e ^Pcl -e ^App -e ^NMA -e ^RMS -e ^acq -e ^AMA \
                 -e ^cdr -e ^CDR -e ^bacq -e ^BOSS -e ^Config | sort > swlist.$x
             print "Converting RPM to Swlist for $x"
             ./ParseLinuxSwlist swlist.$x   # Parse and convert rpm to swlisting format
             mv swlist.$x $Orig/.
             print $Str/swlist.$x.$Str >> $Conf/$Str.$proc.linux  # build `stripped` files list for Acquire_Patches
           else
             print "$x is Other"; print $x >> $Conf/$Str.$proc.other
           fi
         else
           print "$x is Other"; print $x >> $Conf/$Str.$proc.other
         fi
      done
   fi
}

GetCentralListings() {
   proc=$1
   print "Acquiring hostname for Central Server ..."
   if [ -f $Conf/processors.a ]; then print "Acquiring hostname for Backup Central Server ..."; fi

   x=`cat $Conf/processors.$proc`
   print "Getting swlist for $x ..."
   /usr/sbin/swlist -l fileset -a revision -a state > swlist.$x
   if [ -f $Conf/processors.a ]; then
      x=`cat $Conf/processors.a`
      print "Getting swlist for $x ..."
      remsh $x "/usr/sbin/swlist -l fileset -a revision -a state" > swlist.$x
   fi

   # build `stripped` files list to be used by Acquire_Patches
   if [ -f $Conf/$Str.$proc ]; then rm $Conf/$Str.$proc ; fi
   x=`cat $Conf/processors.$proc`
   print $Str/swlist.$x.$Str > $Conf/$Str.$proc
   if [ -f $Conf/processors.a ]; then
      if [ -f $Conf/$Str.a ]; then rm $Conf/$Str.a ; fi
      x=`cat $Conf/processors.a`
      print $Str/swlist.$x.$Str > $Conf/$Str.a
   fi
}

Central_FLAG=0
Sites_FLAG=0
Apps_FLAG=0
Probes_FLAG=0
DataStore_FLAG=0
Ind_FLAG=0
GetDocs=0

if [ $# -eq 0 ]   # default: Get Swlistings on all processors in acceSS7 system; no documentation
then
   Central_FLAG=1; Sites_FLAG=1; Apps_FLAG=1; Probes_FLAG=1; DataStore_FLAG=1
fi

if [ $# -eq 1 ] && [ $1 = "-docs" ]     # default settings plus documentation
then
   Central_FLAG=1; Sites_FLAG=1; Apps_FLAG=1; Probes_FLAG=1; DataStore_FLAG=1
   GetDocs=1; shift;
fi

if [ $# -gt 0 ] && [ $1 = "-indH" ]     # See if individual (HP-UX) listings are requested
then
   Ind_FLAG=1
   while [ $# -gt 1 ]                   # Start looking for optional arguments
   do
      if [ $2 = "-docs" ]; then
         GetDocs=1
      else
         print "Getting swlist for $2 ..."
         remsh $2 "/usr/sbin/swlist -l fileset -a revision -a state" > swlist.$2
      fi
      shift
   done
fi

if [ $# -gt 0 ] && [ $1 = "-indL" ]     # See if individual (Linux) listings are requested
then
   Ind_FLAG=1
   while [ $# -gt 1 ]                   # Start looking for optional arguments
   do
      if [ $2 = "-docs" ]; then
         GetDocs=1
      else
         filename=$2
         print "Getting rpm listing for $filename (Linux) ..."
         ssh $filename "rpm -qa" | grep -e Platform7 -e ^a7 -e ^A7 -e ^p7 -e ^P7 \
           -e ^Pcl -e ^App -e ^NMA -e ^RMS -e ^acq -e ^AMA \
             -e ^cdr -e ^CDR -e ^bacq -e ^BOSS -e ^Config | sort > swlist.$filename
         print "Converting RPM to Swlist for $filename"
         ./ParseLinuxSwlist swlist.$filename   # Parse and convert rpm to swlisting format
         mv swlist.$filename $Orig/.
      fi
      shift
   done
fi

if [ Ind_FLAG -eq 0 ]; then
   while [ $# -gt 0 ]                  # Start looking for optional arguments
   do
      case $1 in
        -c) Central_FLAG=1
            shift ;;
        -s) Sites_FLAG=1
            shift ;;
        -w) Apps_FLAG=1
            shift ;;
        -p) Probes_FLAG=1
            shift ;;
        -d) DataStore_FLAG=1
            shift ;;
        -docs) GetDocs=1
            shift ;;
       -* ) PrintUsage
            exit ;;
        * ) PrintUsage 
            exit ;;
       -- ) shift
            break ;;
      esac
   done
fi

# if [ Central_FLAG -eq 1 ]; then print "the value of Central_FLAG = \"$Central_FLAG\""; fi
# if [ Sites_FLAG -eq 1 ]; then print "the value of Sites_FLAG = \"$Sites_FLAG\""; fi
# if [ Apps_FLAG -eq 1 ]; then print "the value of Apps_FLAG = \"$Apps_FLAG\""; fi
# if [ Probes_FLAG -eq 1 ]; then print "the value of Probes_FLAG = \"$Probes_FLAG\""; fi
# if [ DataStore_FLAG -eq 1 ]; then print "the value of DataStore_FLAG = \"$DataStore_FLAG\""; fi
# if [ GetDocs -eq 1 ]; then print "the value of GetDocs = \"$GetDocs\""; fi

if [ Ind_FLAG -eq 0 ]; then
   for n in c s w p d
   do
      if [ Central_FLAG -eq 1 ] && [ $n = "c" ]; then GetCentralListings $n ; fi
      if [ Sites_FLAG -eq 1 ] && [ $n = "s" ]; then GetListings $n ; fi
      if [ Apps_FLAG -eq 1 ] && [ $n = "w" ]; then GetListings $n ; fi
      if [ Probes_FLAG -eq 1 ] && [ $n = "p" ]; then GetListings $n ; fi
      if [ DataStore_FLAG -eq 1 ] && [ $n = "d" ]; then GetListings $n ; fi
   done
fi
print "done\n"

# Now Parse all swlist files
if [ -f swlist.* ]; then
   if [ GetDocs -eq 1 ]; then
     ./ParseFiles -docs
   else
     ./ParseFiles
   fi
fi
