# This program will obtain system information by trying to determine the type
# of system being run on.  If the execution of the following command fails, 
# then it will be assumed that you are running on a non unix such as an NT. 

$tabstop = 20;

sub expand
{
        my @l = @_;
        for $_ (@l) {
                1 while s/^([^\t]*)(\t+)/
                        $1 . (" " x
                                ($tabstop * length($2)
                                - (length($1) % $tabstop)))
                        /e;
        }
        return @l if wantarray;
        return @l[0];
}

$PLAT="";

`uname -a 2>&1`;

if ($? == 0) {
   $_=`uname -a`;
   SWITCH : {
      if (/IRIX/)                   {$PLAT=sgi;     last SWITCH; }
      if (/UXP/)                    {$PLAT=vpp;     last SWITCH; }
      if ((/HP-UX/) && (/B\.10/))   {$PLAT=hp700;   last SWITCH; }
      if ((/HP-UX/) && (/B\.11/))   {$PLAT=hp;      last SWITCH; }
      if (/SunOS/)                  {$PLAT=solaris; last SWITCH; }
      if (/alpha/)                  {$PLAT=u_alpha; last SWITCH; }
      if (/CRAY/)                   {$PLAT=ymp;     last SWITCH; }
      if (/AIX/)                    {$PLAT=rs;      last SWITCH; }
      if (/SX-4/)                   {$PLAT=nec;     last SWITCH; }
      $nothing=1;
      }
   }
else
  {$PLAT=NTI;}

if ($PLAT eq "") {
   print "A system type could not be determined...\n";
   exit 1;
   }
else {
   print "Please wait until all needed information has been gathered...\n";
   }


# Computer Model table

%Sys_MD     = ('solaris',    "echo '           SUN'       ",
	       'sgi',        "echo '     Silicon Graphics'",
	       'hp700',      "echo '      Hewlett Packard'",
	       'hp',         "echo '      Hewlett Packard'",
	       'u_alpha',    "echo '         Dec Alpha'   ",
	       'ymp',        "echo '           Cray'      ",
	       'rs',         "echo '           IBM'       ",
               'NTI',        "echo              NT        ",
               'vpp',        "echo '          Fujitsu     ",
               'nec',        "echo '         NEC SX-4     "
               );

# Processor type table

%Sys_PR     = ('solaris',    "prtconf -vp | grep banner-name           ",
	       'sgi',        "hinv | grep -i cpu                       ",
	       'hp700',      "model                                    ",
	       'hp',         "model                                    ",
	       'u_alpha',    "uname -p                                 ",
	       'ymp',        "sysconf | grep MFTYPE                    ",
	       'rs',         "/usr/sbin/lsattr -E -l proc0 | grep type ",
               'NTI',        "echo %PROCESSOR_IDENTIFIER%",
               'vpp',        "uname -mp                                ",
               'nec',        "uname -m                                 "
               );

# SYSTEM ID table

%Sys_ID     = ('solaris',    "hostid                        ",
	       'sgi',        "/etc/sysinfo -s               ",
	       'hp700',      "uname -i                      ",
	       'hp',         "uname -i                      ",
	       'u_alpha',    "netstat -i | grep :           ",
	       'ymp',        "sysconf | grep -i serial      ",
	       'rs',         "uname -m                      ",
               'NTI',        "ipconfig /all                 ",
               'vpp',        "hostid                        ",
               'nec',        "hostid                        "
               );

# Operating system table

%Sys_OS     = ('solaris',    "uname -rv                       ",
	       'sgi',        "uname -a                        ",
	       'hp700',      "uname -r                        ",
	       'hp',         "uname -r                        ",
	       'u_alpha',    "uname -a                        ",
	       'ymp',        "uname -a                        ",
	       'rs',         "lslpp -L bos.rte | grep bos.rte ",
               'NTI',        "ver                             ",
               'vpp',        "pkginfo -i -l `pkginfo -i | grep \"uxpV\\.\" | awk '{print \$2}'` | grep VERSION | grep PUF | sort",
               'nec',        "pkginfo -l knl | grep VERSION:                       "
               );

# FORTRAN compiler table

%Sys_FC     = ('solaris',    "what `which f77` | grep RELEASE          ",
	       'sgi',        "versions -b ftn77_dev_gcs ftn77_dev | grep Fortran ",
	       'hp700',      "what `which f77`                         ",
	       'hp',         "what `which /opt/fortran90/bin/f90`      ",
	       'u_alpha',    "f77 -what | grep -i fortran              ",
	       'ymp',        "f90 -V 2>&1 | grep -i version            ",
	       'rs',         "lslpp -L xlfcmp | grep xlfcmp            ",
               'NTI',        "df /help                                 ",
               'vpp',        "pkginfo -i -l `pkginfo -i | grep uxpF90VP | awk '{print \$2}'` | grep VERSION | sort",
               'nec',        "pkginfo -l f90 | grep VERSION:           "
               );

# C compiler table

%Sys_CC     = ('solaris',    "what `which cc` | grep RELEASE ",
	       'sgi',        "versions -b c_dev | grep C    ",
	       'hp700',      "what `which cc`                ",
	       'hp',         "what `which cc`                ",
	       'u_alpha',    "cc -V | grep ' C '             ",
	       'ymp',        "cc -V 2>&1 | grep -i version   ",
	       'rs',         "lslpp -L xlC.C | grep xlC      ",
               'NTI',        "cl /help 2>&1                  ",
               'vpp',        "pkginfo -i -l `pkginfo -i | grep uxpV-C | awk '{print \$2}'` | grep VERSION ",
               'nec',        "pkginfo -l csx | grep VERSION:                        "
               );

# C++ compiler table

%Sys_CP     = ('solaris',    "what `which CC` | grep RELEASE  ",
	       'sgi',        "versions -b c++_dev | grep C++  ",
	       'hp700',      "what `which CC` | grep HP       ",
	       'hp',         "what `which CC` | grep HP       ",
	       'u_alpha',    "cxx -V | grep ' C++ '           ",
	       'ymp',        "CC -V  2>&1 | grep -i version   ",
	       'rs',         "lslpp -L xlC.C++.cmp | grep xlC ",
               'NTI',        "cl /help 2>&1                   ",
               'vpp',        "pkginfo -i -l `pkginfo -i | grep uxpC++ | awk '{print \$2}'` | grep VERSION | sort",
               'nec',        "pkginfo -l C++ | grep VERSION:                 "
               );

# X graphics level table

%Sys_GX     = ('solaris',    "echo 'No available command !!!'                                                    ",
	       'sgi',        "versions -b x_dev | grep x_dev                                                     ",
	       'hp700',      "what /usr/lib/X11R*/libX11.sl | grep Window                                        ",
	       'hp',         "what /usr/lib/X11R*/libX11.sl | grep Window                                        ",
	       'u_alpha',    "setld -i | grep installed | grep X11 | grep Basic                                  ",
	       'ymp',        "echo 'No available command !!!'                                                    ",
	       'rs',         "lslpp -L X11.Dt.rte 2>&1| grep X11                                                 ",
               'NTI',        "echo No available command!!!",
               'vpp',        "pkginfo -i -l `pkginfo -i | grep uxpXWIN | awk '{print \$2}'` | grep VERSION | sort",
               'nec',        "pkginfo -l xwin | grep VERSION:                                                    "
               );

# GL graphics level table

%Sys_GL     = ('solaris',    "strings /opt/SUNWits/Graphics-sw/xgl/lib/libxgl.so | grep 'XGL '",
	       'sgi',        "versions -b gl_dev | grep gl_dev                                ",
	       'hp700',      "what /opt/graphics/OpenGL/lib/libGLw.a 2>&1 | grep HP-UX             ",
	       'hp',         "what /opt/graphics/OpenGL/lib/libGLw.a 2>&1 | grep HP-UX             ",
	       'u_alpha',    "setld -i | grep installed | grep OpenGL | grep Base             ",
	       'ymp',        "echo 'No available command !!!'                                 ",
	       'rs',         "lslpp -L OpenGL.GL32.rte.base 2>&1 | grep GL                    ",
               'NTI',        "echo Goto Start/Settings/Control Panel/",
               'vpp',        "echo 'No available command !!!'                                 ",
               'nec',        "pkginfo -l OpenGL 2>&1 | grep VERSION:                               "
               );

# SYSTEM graphics board table

%Sys_GB     = ('solaris',    "echo 'No available command !!!'",
               'sgi',        "hinv -c graphics               ", 
               'hp700',      "echo 'No available command !!!'",
               'hp',         "echo 'No available command !!!'",
               'u_alpha',    "sizer -gt                      ",
               'ymp',        "echo 'No available command !!!'",
               'rs',         "lsdisp 2>&1 | grep Graphics    ",
               'NTI',        "echo and choose (Display) (Settings) (Display Type))",
               'vpp',        "echo 'No available command !!!'",
               'nec',        "echo 'No available command !!!'",
               );

# Main Memory table

%Sys_MM     = ('solaris',    "/usr/bin/dmesg | grep mem | head -1     ",
	       'sgi',        "hinv -t memory                          ",
	       'hp700',      "echo 'No available command !!!'         ",
	       'hp',         "echo 'No available command !!!'         ",
	       'u_alpha',    "vmstat -P | grep Memory | grep =        ",
	       'ymp',        "sysconf | grep SYSMEM                   ",
	       'rs',         "/usr/sbin/lsattr -E -l mem0 | grep size ",
               'NTI',        "echo Goto Start/Settings/Control Panel/ and choose (System) (General))",
               'vpp',        "echo 'No available command !!!'         ",
               'nec',        "echo 'No available command !!!'         "
               );

# Disk space table

%Sys_DS     = ('solaris',    "df -k | grep -v :    ",
	       'sgi',        "df -k | grep -v :    ",
	       'hp700',      "bdf | grep -v :      ",
	       'hp',         "bdf | grep -v :      ",
	       'u_alpha',    "df -k | grep -v :    ",
	       'ymp',        "df -k -P | grep -v : ",
	       'rs',         "df -k | grep -v :    ",
               'NTI',        "cd 2>&1              ",
	       'vpp',        "df -k | grep -v :    ",
	       'nec',        "df -c | grep -v :    "
               ); 

$rec       = {OS => \%Sys_OS,
              ID => \%Sys_ID,
              FC => \%Sys_FC,
              CC => \%Sys_CC,
              CP => \%Sys_CP,
              PR => \%Sys_PR,
              MM => \%Sys_MM,
              GX => \%Sys_GX,
              GL => \%Sys_GL,
              DS => \%Sys_DS,
              GB => \%Sys_GB,
              MD => \%Sys_MD,
              };

# This section checks that the command being executed is valid.
# If valid, then it is assigned to the appropriate variable and 
# is placed into an array.
# If not valid, a message of "Command not valid !" will be printed
# in place of the information.

foreach $command (OS,ID,FC,CC,CP,PR,MM,GX,GL,DS,GB,MD) {

$ccom=$command . "_CMD";
$ecom=$command . "_E";

#Command being used.
#print "$rec->{$command}{$PLAT} ","\n";

`$rec->{$command}{$PLAT} 2>&1`;
if ($? == 0) {
   chop($$ccom = `$rec->{$command}{$PLAT}`);
   if (($PLAT eq "hp700" || $PLAT eq "hp") && $command eq "GX") {
      @tmp = split '\n',$$ccom;
      $$ccom =  @tmp[@tmp-1];
      @$command = split ' ',$$ccom;
      }
   elsif (($PLAT eq "vpp"|| $PLAT eq "nec") && ($command eq "OS" || $command eq "FC" || $command eq "CC" || $command eq "CP" || $command eq "GX" || $command eq "GL")) {
      @tmp = split 'VERSION:  ',$$ccom;
      $$ccom =  @tmp[@tmp-1];
      @$command = split ' ',$$ccom;
      }
   elsif ($PLAT eq "NTI" && ($command eq "DS" || $command eq "ID")){
      if ($command eq "DS") {
         foreach $dir ("c:","d:","e:","f:","g:","h:","i:","j:","k:","l:","m:","n:","o:","p:","q:","r:","s:") {
            `$dir 2>&1`;
            if ($? == 0) {
               $tmpd = "    $dir ";
               @tmps = `dir $dir`;
               @tmpt = split ' ',@tmps[@tmps-1];
               $tmp = @tmpt[0] . "\n";
               @DS[@DS+0] = $tmpd . "\t\t" . $tmp;
               }
            }
         }
      elsif ($command eq "ID") {
         @tmp = split '\n',$$ccom;
         for ( $i = 0; $i <= @tmp-1; $i++ ) {
             $_ = @tmp[$i];
             if (/Physical Address/ && !/00-00-00-00-00-00/) {
                $$ccom =  $_;
	        }
             @$command = split ': ',$$ccom;
	     }
         } 
      }
   else {
      @$command = split ' ',$$ccom;
      }
   $$ecom = 0;
   }
else {
   $$ccom = "Information not currently available !";
   $$ecom = 1;
   }
}


# This section will format the data obtained in the command and the 
# apropriate information is placed into a variable so it can be
# printed later on.

if ($PLAT eq "solaris"){

   if (!$MM_E) {$MM_CMD = join ' ', @MM[2];}
   if (!$CC_E) {$CC_CMD = join ' ', @CC;}
   if (!$CP_E) {$CP_CMD = join ' ', @CP;}
   if (!$FC_E) {$FC_CMD = join ' ', @FC;}
   if (!$PR_E) {$PR_CMD = join ' ', @PR[1],@PR[2],@PR[3],@PR[4],@PR[5],@PR[6],@PR[7],@PR[8],@PR[9],@PR[10];}
   if (!$GL_E) {$GL_CMD = join ' ', @GL[0],@GL[1];}
   if (!$DS_E) {   
      $dslength=@DS/6;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+7;
            }
         else{
            $DSA_CMD[$i] = join "\t", "\t",@DS[$j+1],@DS[$j+3],@DS[$j+5];
            $j=$j+6;
            }
         }
      }

}

elsif ($PLAT eq "sgi") {
   
   if (!$OS_E) {$OS_CMD = join ' ', @OS[0],@OS[2];}
   if (!$FC_E) {$FC_CMD = join ' ', @FC[2],@FC[3],@FC[4],@FC[5],@FC[6],@FC[7],@FC[8],@FC[9],@FC[10];}
   if (!$CC_E) {$CC_CMD = join ' ', @CC[2],@CC[3],@CC[4],@CC[5],@CC[6],@CC[7],@CC[8],@CC[9],@CC[10];}
   if (!$CP_E) {$CP_CMD = join ' ', @CP[2],@CP[3],@CP[4],@CP[5],@CP[6],@CP[7],@CP[8],@CP[9],@CP[10];}
   if (!$PR_E) {$PR_CMD = join ' ', @PR[1],@PR[2],@PR[3];}
   if (!$GX_E) {$GX_CMD = join ' ', @GX[3],@GX[6],@GX[7],@GX[8],@GX[9];}
   if (!$GL_E) {$GL_CMD = join ' ', @GL[3],@GL[4],@GL[5],@GL[6],@GL[7];}
   if (!$GB_E) {$GB_CMD = join ' ', @GB;}
   if (!$MM_E) {$MM_CMD = join ' ', @MM[3],@MM[4];}
   if (!$DS_E) {   
      $dslength=@DS/7;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+8;
            }
         else{
            $DSA_CMD[$i] = join "\t","\t",@DS[$j+2],@DS[$j+4],@DS[$j+6];
            $j=$j+7;
            }
         }
      }
}

elsif ($PLAT eq "ymp") {
   if (!$ID_E) {$ID_CMD = join ' ', @ID[2];}
   if (!$OS_E) {$OS_CMD = join ' ', "UNICOS",@OS[2];}
   if (!$PR_E) {$PR_CMD = join ' ', @PR[4];}
   if (!$FC_E) {$FC_CMD = join ' ', @FC[0],@FC[1],@FC[3];}
   if (!$CC_E) {$CC_CMD = join ' ', @CC[0],@CC[1],@CC[2],@CC[4];}
   if (!$CP_E) {$CP_CMD = join ' ', @CP[0],@CP[1],@CP[3];}
   if (!$MM_E) {$MM_CMD = join ' ', @MM[0],@MM[1],@MM[2],@MM[3],@MM[4],@MM[5];}
   if (!$DS_E) {   
      $dslength=@DS/6;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+7;
            }
         else{
            $DSA_CMD[$i] = join "\t", "\t",@DS[$j+1],@DS[$j+3],@DS[$j+5];
            $j=$j+6;
            }
         }
      }
}

elsif ($PLAT eq "NTI") {
   if (!$ID_E) {$ID_CMD = join ' ', @ID[1];}
   if (!$OS_E) {$OS_CMD = join ' ', @OS[0],@OS[1],@OS[2],@OS[3];}
   if (!$FC_E) {$FC_CMD = join ' ', @FC[0],@FC[1],@FC[2],@FC[6];}
   if (!$CC_E) {$CC_CMD = join ' ', @CC[0],@CC[2],@CC[3],@CC[7];}
   if (!$CP_E) {$CP_CMD = join ' ', @CP[0],@CP[2],@CP[3],@CC[7];}
   $dslength=@DS;
   $j=0;
   for ( $i = 0; $i <= $dslength; $i++ ) {
      if ($i == 0) {
         $DSA_CMD[$i] = join "\t", "  Drive","     Disk Space (Free bytes)\n";
         }
      else{
         $DSA_CMD[$i] = join "\t\t\t\t\t","   ",@DS[$j],@DS[$j+1];
         $j=$j+2;
         }
   }
}

elsif ($PLAT eq "hp700" || $PLAT eq "hp") {

   if (!$FC_E) {$FC_CMD = join ' ', @FC[1],@FC[2],@FC[3],@FC[4],@FC[5],@FC[6],@FC[7];}
   if (!$CC_E) {$CC_CMD = join ' ', @CC[5],@CC[6],@CC[7],@CC[8],@CC[9];}
   if (!$CP_E) {$CP_CMD = join ' ', @CP;}
   if (!$GX_E) {$GX_CMD = join ' ', @GX[0],@GX[1],@GX[2],@GX[3],@GX[4],@GX[5],@GX[6],@GX[7],@GX[8],@GX[9],@GX[10];}
   if (!$GL_E) {$GL_CMD = join ' ', @GL[0],@GL[1],@GL[2],@GL[3],@GL[4],@GL[5],@GL[6],@GL[11];}
   if (!$GB_E) {$GB_CMD = join ' ', @GB;}
   if (!$DS_E) {   
      $dslength=@DS/6;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
               $j=$j+7;
            }
         else{
            $DSA_CMD[$i] = join "\t","\t",$DS[$j+1],$DS[$j+3],$DS[$j+5];
            $j=$j+6;
            }
      }
   }
}

elsif ($PLAT eq "u_alpha") {

   if (!$OS_E) {$OS_CMD = join ' ',"Digital Unix", @OS[2], @OS[3];}
   if (!$ID_E) {$ID_CMD = @ID[3];}
   if (!$FC_E) {$FC_CMD = join ' ', @FC;}
   if (!$CC_E) {$CC_CMD = join ' ', @CC;}
   if (!$GB_E) {$GB_CMD = join ' ', @GB;}
   if (!$GL_E) {$GL_CMD = join ' ', @GL;}
   if (!$GX_E) {$GX_CMD = join ' ', @GX;}
   if (!$CP_E) {$CP_CMD = join ' ', @CP;}
   if (!$MM_E) {$MM_CMD = join ' ', @MM[4],@MM[5];}
   if (!$DS_E) {
      $dslength=@DS/6;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+7;
            }
         else{
            $DSA_CMD[$i] = join "\t", "\t",@DS[$j+2],@DS[$j+3],@DS[$j+5];
            $j=$j+6;
            }
         }
      }
}

elsif ($PLAT eq "vpp" || $PLAT eq "nec") {

   if (!$FC_E) {$FC_CMD = join ' ', @FC;}
   if (!$CC_E) {$CC_CMD = join ' ', @CC;}
   if (!$CP_E) {$CP_CMD = join ' ', @CP;}
   if (!$DS_E) {
      $dslength=@DS/6;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+7;
            }
         else{
            $DSA_CMD[$i] = join "\t", "\t",@DS[$j+1],@DS[$j+3],@DS[$j+5];
            $j=$j+6;
            }
         }
      }
}

elsif ($PLAT eq "rs") {

   if (!$OS_E) {$OS_CMD = join ' ', @OS[1],@OS[3],@OS[4],@OS[5],@OS[6];}
   if (!$FC_E) {$FC_CMD = join ' ', @FC[1],@FC[3],@FC[4],@FC[5]};
   if (!$CC_E) {$CC_CMD = join ' ', @CC[1],@CC[3],@CC[4],@CC[5],@CC[6];}
   if (!$PR_E) {$PR_CMD = join ' ', @PR[1];}
   if (!$GB_E) {$GB_CMD = join ' ', @GB[3];}
   if (!$GL_E) {$GL_CMD = join ' ', @GL;}
   if (!$GX_E) {$GX_CMD = join ' ', @GX;}
   if (!$CP_E) {$CP_CMD = join ' ', @CP[1],@CP[3],@CP[4],@CP[5],@CP[6],@CP[7],@CP[8];}
   if (!$MM_E) {$MM_CMD = join ' ', @MM[1];}
   if (!$DS_E) {
      $dslength=@DS/7;
      $j=0;
      for ( $i = 1; $i <= $dslength; $i++ ) {
         if ($i == 1) {
            $DSA_CMD[$i] = join "\t","Kbytes","Avail","Mounted";
            $j=$j+8;
            }
         else{
            $DSA_CMD[$i] = join "\t", "\t",@DS[$j+1],@DS[$j+2],@DS[$j+6];
            $j=$j+7;
            }
         }
      }
}

# Output of collected data.


print(STDOUT "\n\t\t\t\t","    SYSTEM INFORMATION");
print(STDOUT "\n\t\t\t\t",$MD_CMD,"\n\n");
print(STDOUT "\t\tProcessor:              ",$PR_CMD,"\n");
print(STDOUT "\t\tID:                     ",$ID_CMD,"\n");
print(STDOUT "\t\tOS:                     ",$OS_CMD,"\n");
print(STDOUT "\t\tFortran Level:          ",$FC_CMD,"\n");
print(STDOUT "\t\tC Level:                ",$CC_CMD,"\n");
print(STDOUT "\t\tC++ Level:              ",$CP_CMD,"\n");
print(STDOUT "\t\tX11 Level:              ",$GX_CMD,"\n");
print(STDOUT "\t\tGraphics Lib Level:     ",$GL_CMD,"\n");
print(STDOUT "\t\tGraphics Board:         ",$GB_CMD,"\n");
print(STDOUT "\t\tMain Memory:            ",$MM_CMD,"\n");
print(STDOUT "\t\tDisk Space:             ");
   if (!$DS_E && $PLAT ne "NTI") {
      for ( $k = 1; $k <= $dslength; $k++ ) {
         print expand($DSA_CMD[$k],"\n");
        }
      }
   elsif($PLAT eq "NTI") {
         print (STDOUT @DSA_CMD);
      }
   else {
      print(STDOUT "Information not currently available !\n");
      }
