#!/opt/gnu/bin/perl
################################################################################
# Name.........: drm_status.pl                                                 #
# Author.......: Yves Morin                                                    #
# Date.........: Juillet, 26 1998                                              #
################################################################################



#########################################
# Which packages do we use              #
#########################################
use English;



#########################################
# Initialize the report variables       #
#########################################
$FORMAT_LINES_PER_PAGE = 40;
$FORMAT_NAME           = "REPORT";
$FORMAT_TOP_NAME       = "REPORT_TOP";



#########################################
# Initialize the local variables        #
#########################################
my $today   = `date`;
my $datetmp = `date "+%C%y%m%d%H%M"`;
my $Date    = substr($datetmp,0,5);
#my $File    = "./DRM_Status_$datetmp";
my $File    = "/var/adm/backup/drm/DRM_Status_$datetmp";

my %mountable = ();
my %notmountable = ();
my %vault = ();
my %vaultretrieve = ();
my %courierretrieve = ();
my %adsm         = ();
my %adsm_scratch = ();
my %adsm_private = ();
my %l3494              = ();
my %l3494_scratch_3490 = ();
my %l3494_scratch_3590 = ();
my %l3494_private = ();
my %l3494_other   = ();

my $nb_mountable = 0;
my $nb_notmountable = 0;
my $nb_vault = 0;
my $nb_vaultretrieve = 0;
my $nb_courierretrieve = 0;
my $nb_other = 0;
my $nb_adsm_scratch = 0;
my $nb_adsm_private = 0;
my $nb_3494_private = 0;
my $nb_3494_mksysb  = 0;
my $nb_3494_scratch_3590 = 0;
my $nb_3494_scratch_3490 = 0;
my $nb_3494_other = 0;
my $to_courierretrieve = 0;

#########################################
# Initialize the local variables        #
#########################################
$ADSM = "/usr/lpp/adsm/bin/dsmadmc -id=master -password=admin -noconfirm";

$CMD_DRMEDIA      = "query drmedia \\\*";
$CMD_MAGSTAR3494  = "query libv magstar3494";

$GREP_DRMEDIA     = "| egrep \"^C5\" | sort";
$GREP_MAGSTAR3494 = "| grep \"^MAGSTAR3494\" | sort +1";

$CMD_MTLIB        = "/usr/bin/mtlib -l /dev/lmcp0 -qI";


################################################################################
# Open the <INPUT> FILEHANDLE as the results of the dsmadmc command.           #
# Build associative arrays for Mountable, NotMountable, Vault, VaultRetrieve   #
# and CourrierRetrieve DRM states.                                             #
################################################################################
$ADSM_CMD = sprintf ("%s %s %s\n", $ADSM, $CMD_DRMEDIA, $GREP_DRMEDIA);

open (INPUT, "$ADSM_CMD |");
while (defined ($x = <INPUT>))
{
   ($cart, $status, $status2, $status3, $status4) = split(' ',$x);

   if ($status eq "Mountable")
   {
      $mountable{$cart} = "$status2 $status3";
      $nb_mountable++;
   }
   elsif ($status eq "Not" && $status2 eq "Mountable")
   {
      $notmountable{$cart} = "$status3 $status4";
      $nb_notmountable++;
   }
   elsif ($status eq "Vault" && $status2 eq "retrieve")
   {
      $vaultretrieve{$cart} = "$status3 $status4";
      $nb_vaultretrieve++;
   }
   elsif ($status eq "Vault")
   {
      $vault{$cart} = "$status2 $status3";
      $nb_vault++;
   }
   elsif ($status eq "Courier" && $status2 eq "retrieve")
   {
      $courierretrieve{$cart} = "$status3 $status4";
      $nb_courierretrieve++;
   }
   else
   {
     $nb_other++;
   }

}
close (INPUT);



################################################################################
# Open the <INPUT> FILEHANDLE as the results of the $CMD_MAGSTAR3494.          #
# Build associative arrays for ADSM_Private and ADSM_Scratch Volumes.          #
################################################################################
$ADSM_CMD = sprintf ("%s %s %s\n", $ADSM, $CMD_MAGSTAR3494, $GREP_MAGSTAR3494);
open (INPUT, "$ADSM_CMD |");
while (defined ($x = <INPUT>))
{
   ($library, $cart, $status, $status2) = split(' ',$x);

   $adsm{$cart} = "OK";

   if ($status eq "Private")
   {
      $adsm_private{$cart} = "OK";
      $nb_adsm_private++;
   }
   elsif ($status eq "Scratch")
   {
      $adsm_scratch{$cart} = "OK";
      $nb_adsm_scratch++;
   }
}
close (INPUT);



################################################################################
# Open the <INPUT> FILEHANDLE as the results of the $CMD_MTLIB.                #
# Build associative arrays for 3494_private,3494_Scratch_3490,ADSM_Scratch_3590#
################################################################################
open (INPUT, "$CMD_MTLIB |");
while (defined ($x = <INPUT>))
{
   ($cart, $category, $status, $status2, $status3) = split(' ',$x);

   $l3494{$cart} = "OK";

   if ($category eq "012E")
   {
      $l3494_scratch_3590{'$cart'} = "OK";
      $nb_3494_scratch_3590++;
   }
   elsif ($category eq "012D")
   {
      $l3494_scratch_3490{$cart} = "OK";
      $nb_3494_scratch_3490++;
   }
   elsif ($category eq "012C")
   {
      $l3494_private{$cart} = "OK";
      $nb_3494_private++;
   }
   elsif ($category eq "0107")
   {
      $l3494_mksysb{$cart} = "OK";
      $nb_3494_mksysb++;
   }
   else
   {
      $l3494_other{$cart} = $category;
      $nb_3494_other++;
   }
}
close (INPUT);



################################################################################
# OPEN A PIPE TO ADSM AND MOVE from VaulRetrieve to CourierRetrieve.           #
################################################################################
foreach $cart ( keys %vaultretrieve)
{
   $to_courierretrieve++;
}

if ($to_courierretrieve > 0)
{
   open (OUTPUT, "| $ADSM");
#   print OUTPUT "move drmedia * wherestate=vaultretrieve\n";
   print OUTPUT "quit\n";
   close (OUTPUT);
}


################################################################################
# OPEN THE REPORT FILE FOR WRITING.                                            #
################################################################################
open (REPORT, ">$File") || die "Can't start ADSM!\n";
select(REPORT);



################################################################################
# GENERATES THE REPORT                                                         #
################################################################################
$header_desc = "MOUNTABLE DRMEDIA VOLUMES:";
$header1     = "Cart";
$header2     = "In this State since";
foreach $cart ( sort keys %mountable )
{
   $description = $mountable{$cart};
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_mountable == 0)
{
   $cart        = "NO";
   $description = "VOLUMES are in the MOUNTABLE STATE";
   write;
}



$FORMAT_LINES_LEFT=0;
$header_desc = "NOT MOUNTABLE DRMEDIA VOLUMES:";
foreach $cart ( sort keys %notmountable )
{
   $description = $notmountable{$cart};
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_notmountable == 0)
{
   $cart        = "NO";
   $description = "VOLUMES are in the NOT MOUNTABLE STATE";
   write;
}



$FORMAT_LINES_LEFT=0;
$header_desc = "VAULT DRMEDIA VOLUMES:";
foreach $cart ( sort keys %vault )
{
   $description = $vault{$cart};
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_vault == 0)
{
   $cart        = "NO";
   $description = "VOLUMES are in the VAULT STATE";
   write;
}



$FORMAT_LINES_LEFT=0;
$header_desc = "VAULT RETRIEVE DRMEDIA VOLUMES:";
foreach $cart ( sort keys %vaultretrieve )
{
   $description = $vaultretrieve{$cart};
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_vaultretrieve == 0)
{
   $cart        = "NO";
   $description = "VOLUMES are in the VAULT RETRIEVE STATE";
   write;
}



$FORMAT_LINES_LEFT=0;
$header_desc = "COURIER RETRIEVE DRMEDIA VOLUMES:";
foreach $cart ( sort keys %courierretrieve )
{
   $description = $courierretrieve{$cart};
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_courierretrieve == 0)
{
   $cart        = "NO";
   $description = "VOLUMES are in the COURIER RETRIEVE STATE";
   write;
   print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
   print "DRM Status Report as of $today\n";
}



###############################################################################
# Checkin the orphans if they are in the range of C50000 to C59999.
###############################################################################
open (ADSM, "| $ADSM");
$FORMAT_LINES_LEFT=0;
$header_desc = "3494 LIBRARY VOLUMES OF UNKNOWN TYPES:";
$header2     = "State";

foreach $cart ( sort keys %l3494_other )
{
   if (($cart =~ /^C5/) && ($l3494_other{$cart} eq "FF00"))
   {
#      system("mtlib -l /dev/lmcp0 -C -t 012E -V $cart");
#      print ADSM "move drm $cart wherestate=courierretrieve\n";
#      print ADSM "checkin libv magstar3494 $cart status=scratch devtype=3590\n";
      $description = "Will be Checkin";
   }
   else
   {
      $description = $l3494_other{$cart};
   }
   write;
   if ($FORMAT_LINES_LEFT <= 5)
   {
      print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
      print "DRM Status Report as of $today\n";
      $FORMAT_LINES_LEFT=0;
   }
}
if ($nb_3494_other == 0)
{
   $cart        = "NO";
   $description = "VOLUMES of UNKNOWN TYPE IN THE 3494 LIBRARY";
   write;
}
print ADSM "quit\n";
close(ADSM);



$FORMAT_LINES_LEFT=0;
$header_desc = "ORPHAN VOLUMES:";
$header2     = "State";

foreach $cart ( sort keys %l3494 )
{
   if (!defined($adsm{$cart}))
   {
      $description = "In 3494 but not defined in ADSM";
      write;
      if ($FORMAT_LINES_LEFT <= 5)
      {
         print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
         print "DRM Status Report as of $today\n";
         $FORMAT_LINES_LEFT=0;
      }
   }
}
foreach $cart ( sort keys %adsm )
{
   if (!defined($l3494{$cart}))
   {
      $description = "In ADSM but not defined in 3494";
      write;
      if ($FORMAT_LINES_LEFT <= 5)
      {
         print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
         print "DRM Status Report as of $today\n";
         $FORMAT_LINES_LEFT=0;
      }
   }
}



$FORMAT_LINES_LEFT=0;
$header_desc = "DRMEDIA VOLUMES STATUS:";
$header1     = "Qte";
$header2     = "State";

$cart = $nb_mountable;
$description = "Mountable Volumes";
write;

$cart = $nb_notmountable;
$description = "Not Mountable Volumes";
write;

$cart = $nb_vault;
$description = "Vault Volumes";
write;

$cart = $nb_vaultretrieve;
$description = "Vault Retrieve Volumes";
write;

$cart = $nb_courierretrieve;
$description = "Courier Retrieve Volumes";
write;

$cart = $nb_other;
$description = "Other Volumes";
write;

$cart = "";
$description = "";
write;

$cart = $to_courierretrieve;
$description = "Volumes Moved From Vault Retrieve To Courier Retrieve";
write;



$FORMAT_LINES_LEFT=0;
$header_desc = "ADSM vs 3494 VOLUMES INVENTORY:";

$cart = $nb_adsm_private;
$description = "ADSM Private Volumes";
write;

$cart = $nb_adsm_scratch;
$description = "ADSM Scratch Volumes";
write;

$cart = $nb_3494_private;
$description = "3494 Private Volumes";
write;

$cart = $nb_3494_scratch_3490;
$description = "3494 Private 3490";
write;

$cart = $nb_3494_scratch_3590;
$description = "3494 Scratch 3590";
write;

$cart = $nb_3494_other;
$description = "3494 Other Volumes";
write;

$cart = $nb_3494_mksysb;
$description = "3494 Mksysb spet1000";
write;



close (REPORT);

format REPORT_TOP=
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                   Page @>>>>>>
$header_desc,$FORMAT_PAGE_NUMBER
       @<<<<<<          @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$header1,$header2
 ==============================================================================
.

format REPORT=
       @<<<<<<          @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$cart,$description
.



