#!/usr/local/bin/perl -w
######################################################################################
# Filename: tape_rotation_vaulting.pl
# Usage: This program will perform two primary processes while using Perllanguage
# and EDM application calls. 1) will gather data for vaulting 2) will gather data for
# reimporting scratch tapes.
# Written by: Derek Smith
# Date: 03/2004
# OhioHealth Backup/Restore team
# Celeste Grone, Derek Smith
######################################################################################

## Set pragmas

	use strict;
	use diagnostics;
	use MIME::Lite;	
	use Net::FTP;
		

## Define ENV and globals

	$ENV{"PATH"} = qq(/usr/epoch/bin:/usr/epoch/EB/bin:/usr/bin:/usr/sbin:/bin:/sbin);
	delete $ENV{"IFS"}; 
	#print $ENV{"PATH"},"\n";
	my $time = $^T;

## Declare scalers for client templates and client tape logs

	my $Lsched="lvimage-L-Drive_sched";
	my $Ksched="lvimage-K-Drive_sched";
	my $JIsched="lvimage-JI-Drives_sched";
	my $GHsched="lvimage-GH-Drives_sched";
	my $FEsched="lvimage-FE-Drives_sched";
	my $DCsched="lvimage-DC-Drives_sched";
	my $Lnotesched="21dayLnotesFS_sched";
	my $Edmsched="default";

	my $logf="/usr/epoch/EB/log/ebcat.log";
	my $stps="/tmp/scratch_tapes.log";
	my $scratchtps="/usr/local/log/ejects_and_injects/scratch_tapes.log";
	my $irmt="/tmp/IRMTscratch_tapes.log";
	my $IRMTscratchtps="/usr/local/log/ejects_and_injects/IRMTscratch_tapes.log";
	my $defaultapes="/usr/local/log/edm_tapes.log";
	my $lvimgLtapes="/usr/local/log/lvimg_Ldrive_tapes.log";
	my $lvimgKtapes="/usr/local/log/lvimg_Kdrive_tapes.log";
	my $lvimgJItapes="/usr/local/log/lvimg_JIdrives_tapes.log"; 
	my $lvimgGHtapes="/usr/local/log/lvimg_GHdrives_tapes.log";
	my $lvimgFEtapes="/usr/local/log/lvimg_FEdrives_tapes.log";
	my $lvimgDCtapes="/usr/local/log/lvimg_DCdrives_tapes.log";
	my $lnotestapes="/usr/local/log/lnotes_tapes.log";

	my $ejectsforedm="/usr/local/log/ejects_and_injects/edmejects.log";
	my $ejectsforlnotes="/usr/local/log/ejects_and_injects/lnotesejects.log";
	my $ejectsforlvimgL="/usr/local/log/ejects_and_injects/lvimgLejects.log";
	my $ejectsforlvimgK="/usr/local/log/ejects_and_injects/lvimgKejects.log";
	my $ejectsforlvimgJI="/usr/local/log/ejects_and_injects/lvimgJIejects.log";
	my $ejectsforlvimgGH="/usr/local/log/ejects_and_injects/lvimgGHejects.log";
	my $ejectsforlvimgFE="/usr/local/log/ejects_and_injects/lvimgFEejects.log";
	my $ejectsforlvimgDC="/usr/local/log/ejects_and_injects/lvimgDCejects.log";
	


####################################################################
## Create date strings so program can look in scratch tape list   ##
## for today and today -1. Refer to localtime for details.	  ##	
####################################################################

	sub date_manip {

		my ($month, $day, $year) = (localtime)[4,3,5];
		sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % 100));
	}

my $dm = &date_manip;

	sub date_manip1 {

		my $days = (shift);
		my ($d, $m, $y) = (localtime($time - $days * 86400)) [3..5];
		sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
	}

my $dmm = &date_manip1(1);

chomp $dm;
chomp $dmm;


###############################################################################
## Establish communications with offsite storage vendor Iron Mountain 
## for return scratch tapes and their necessary documents.
###############################################################################

#	sub mailme {
#		
#		my $msg = MIME::Lite->new(
#			From    => 'EDM01 <root@edm01.ohnet>',
#                        To	=> 'OhioHealth Operations <oper1@ohiohealth.com>',
#			Cc	=> 'Derek Smith <dbsmith@ohiohealth.com>',
#			Cc	=> 'Celeste Grone <cgrone@ohiohealth.com>',
#			Subject => "EDM Return Tapes",
#                        Data    => "$IRMTscratchtps" );
#                        $msg->send;
#
#	} 
#
#
	sub ftpme {

		my $remotehost="ftp.digitalarchives.com";
		my $remotedir="archive";
		my $user="cb100524";
		my $pass="532pa599";
		my $data=$scratchtps;
		my $ftplog="/usr/local/log/ftp_IrMt_scratchtapes.log";
		
		my $ftp = Net::FTP->new($remotehost, Debug => 10)
		|| die "cannot connect to $remotehost: IronMt";
		$ftp->login($user, $pass) or die "cannot login ";
		$ftp->ascii();
		$ftp->cwd($remotedir);
		$ftp->put($data) or die "FTP put to IrMt failed";
		$ftp->quit;

	}
 

	
###########################################################################
## Gather data for scratch tapes; open file, create list, then print it  ##
## based off of dates from above. 					 ##
###########################################################################

## BEGIN MAIN;
		
	open (TP, ">$stps") || die "could not open file:$!";
	open (TP2, ">$irmt ") || die "could not open file:$!";
	open (D, "$logf") || die "could not open file:$!";
	($^I) = ('.bak', $scratchtps); 
	
	 print TP "\n";
	 print TP "Print Date: $dm\n";
	 print TP "EDM Scratch Tapes. Please have these returned from IronMt.\n";
	 print TP "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
 
	 while (<D>) {

                ## look for 9840S and ebexpire
                ## declare OFS = tab
                ## tell split to split on IRS 0,1&5. 
                
		if ( (($_ =~ /9840S/) && ($_ =~ /ebexpire, ebexpire/)) && (($_ =~ $dm)||($_ =~ $dmm)) ) {
                        local $, = "\t";
                        print TP +(split)[0,1,5], $/ ;
			print TP2 +(split)[5], $/ ;
			system ("sort -k3 $stps > $scratchtps");
			system ("sort -k3 $irmt > $IRMTscratchtps");
                }
        }
        close (TP);
	close (TP2);
        close (D);

		

########################################################################
## For all subroutines below, gather data for offsite tape vaulting;  ##
## open file, create list, flush buffer				      ## 
## and grab each line w/out *Orig or any active E-tape.		      ##	
########################################################################

## EDM's default template

sub EDM {

## Create backup file first.
	
	open (OUT, ">$defaultapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforedm") || die "could not open file:$!";
	my $EDM_nonactive_tapelist = `ebreport media -template $Edmsched`;
	($^I) = ('.bak', $defaultapes);
	select( (select(OUT), $|=1 ) [0] );
		

		foreach (split /\n/, $EDM_nonactive_tapelist )	{
			
			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {
				
				local $, = "\t";
				if ( substr($_, 29, 6) =~ "^E0") {
					my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr \n";
					if  (substr($_, 60, 13) =~ "^st*" ) {
						my $newstr2 = substr($_, 60, 13);
						print OUT "Tape: $newstr $, Silo: $newstr2\n";
					}
				}
			}
		}
		system ("grep E0 $defaultapes > /tmp/edmtapes");
		my $exval  = $? >> 8;
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for client edm01\n";
			print OUT "These specific tapes are active therefore will not be ejected\n";

		} else {
	
			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client edm01\n";
		}

	close (OUT);
	close (OUT2);
		
}

## Domino clients, Lnotes

#sub DOMINO {
#       
#	($^I, @ARGV) = ('.bak', $lnotestapes); 
#	open (OUT, ">$lnotestapes") || die "could not open file:$!";
#	open (OUT2, ">$ejectsforlnotes") || die "could not open file:$!";
#        my $Lnotes_nonactive_tapelist = `ebreport media -template $Lnotesched`;
#        select( (select(OUT), $|=1 ) [0] );
#
#
#                foreach (split /\n/, $Lnotes_nonactive_tapelist )  {
#			
#			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {
#
#                                local $, = "\t";
#                                if ( substr($_, 29, 6) =~ "^E0") {
#                                        my $newstr = substr($_, 29, 6);
#					print OUT2 "$newstr\n";
#                                        if  (substr($_, 60, 13) =~ "^st*" ) {
#                                                my $newstr2 = substr($_, 60, 13);
#                                                print OUT "Tape: $newstr $, Silo: $newstr2\n";
#                                        }
#                                }
#                        }
#
#                }
#		system ("grep E0 $lnotestapes > /tmp/dominotapes");
#		my $exval  = $? >> 8;
#		if ( $exval == 1 ) {
#
#			print OUT "Print Date: $dm \n";
#			print OUT "There are no vault tapes for domino 1-5\n";
#			print OUT "These specific tapes are active therefore will not be ejected\n";
#
#		} else {
#			
#			print OUT "\n";
#			print OUT "- - - - - - - - - - - - - - - - - - - - \n";
#			print OUT "Print Date: $dm\n";
#			print OUT "EDM Vault Tape List for clients domino[1-5]\n";
#		}
#
#	close (OUT);
#	close (OUT2);
#}


## Lvimage client C-L drives

sub LVIMAGE_L {
       
	open (OUT, ">$lvimgLtapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgL") || die "could not open file:$!";
        my $lvimgL_nonactive_tapelist = `ebreport media -template $Lsched`;
	($^I) = ('.bak', $lvimgLtapes);
        select( (select(OUT), $|=1 ) [0] );


                foreach (split /\n/, $lvimgL_nonactive_tapelist )  {
		
			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

				local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		system ("grep E0 $lvimgLtapes > /tmp/lvimgtapesL");
		my $exval  = $? >> 8;
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for lvimg L drives\n";
			print OUT "These specific tapes are active therefore will not be ejected\n";

		} else {

			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage L drive\n";
		}

	close (OUT);
	close (OUT2);
}


sub LVIMAGE_K {

	open (OUT, ">$lvimgKtapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgK") || die "could not open file:$!";
        my $lvimgK_nonactive_tapelist = `ebreport media -template $Ksched`;
	($^I) = ('.bak', $lvimgKtapes);
        select( (select(OUT), $|=1 ) [0] );


                foreach (split /\n/, $lvimgK_nonactive_tapelist )  {
	
			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

                                local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		system ("grep E0 $lvimgKtapes > /tmp/lvimgtapesK");
                my $exval  = $? >> 8;
                if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for lvimg K drives\n";
			print OUT "These specific tapes are active therefore will not be e
jected\n";

		} else {
			
			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage K drive\n";
		}
	
	close (OUT);
	close (OUT2);
}


sub LVIMAGE_JI {

	open (OUT, ">$lvimgJItapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgJI") || die "could not open file:$!";
        my $lvimgJI_nonactive_tapelist = `ebreport media -template $JIsched`;
	($^I) = ('.bak', $lvimgJItapes);
        select( (select(OUT), $|=1 ) [0] );


                foreach (split /\n/, $lvimgJI_nonactive_tapelist )  {

			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

                                local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		system ("grep E0 $lvimgJItapes > /tmp/lvimgtapesJI");
		my $exval  = $? >> 8;
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for lvimg JI drives\n";
			print OUT "These specific tapes are active therefore will not be e
jected\n";

		} else {
	
			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage JI drives\n";
		}

	close (OUT);
	close (OUT2);
}


sub LVIMAGE_GH {

	open (OUT, ">$lvimgGHtapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgGH") || die "could not open file:$!";
        my $lvimgGH_nonactive_tapelist = `ebreport media -template $GHsched`;
	($^I) = ('.bak', $lvimgGHtapes);
        select( (select(OUT), $|=1 ) [0] );


                foreach (split /\n/, $lvimgGH_nonactive_tapelist )  {

			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

                                local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		system ("grep E0 $lvimgGHtapes > /tmp/lvimgtapesGH");
		my $exval  = $? >> 8;
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for lvimg GH drives\n";
			print OUT "These specific tapes are active therefore will not be e
jected\n";

		} else {

			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage GH drives\n";
		}

	close (OUT);
	close (OUT2);
}


sub LVIMAGE_FE {

	open (OUT, ">$lvimgFEtapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgFE") || die "could not open file:$!";
        my $lvimgFE_nonactive_tapelist = `ebreport media -template $FEsched`;
	($^I) = ('.bak', $lvimgFEtapes);
        select( (select(OUT), $|=1 ) [0] );


		foreach (split /\n/, $lvimgFE_nonactive_tapelist )  {

			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

                                local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		system ("grep E0 $lvimgFEtapes > /tmp/lvimgtapesFE");
		my $exval  = $? >> 8;	
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm \n";
			print OUT "There are no vault tapes for lvimg FE drives\n";
			print OUT "These specific tapes are active therefore will not be e
jected\n";

		} else {

			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - -\n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage FE drives\n";
		}

	close (OUT);
	close (OUT2);
}


sub LVIMAGE_DC {

	open (OUT, ">$lvimgDCtapes") || die "could not open file:$!";
	open (OUT2, ">$ejectsforlvimgDC") || die "could not open file:$!";
        my $lvimgDC_nonactive_tapelist = `ebreport media -template $DCsched`;
	($^I) = ('.bak', $lvimgDCtapes);
        select( (select(OUT), $|=1 ) [0] );


		foreach (split /\n/, $lvimgDC_nonactive_tapelist )  {

			if (( m/\((E\d+)/ ) && ( m/st_9840_acs_0/ ) && ( !m/\*Orig/ ) ) {

                                local $, = "\t";
                                if ( substr($_, 29, 6) =~ "^E0") {
                                        my $newstr = substr($_, 29, 6);
					print OUT2 "$newstr\n";
                                        if  (substr($_, 60, 13) =~ "^st*" ) {
                                                my $newstr2 = substr($_, 60, 13);
                                                print OUT "Tape: $newstr $, Silo: $newstr2
\n";
                                        }
                                }
                        }
                }
		#print OUT "$1\n" unless substr($_, 0, 5) eq '*Orig';
		system ("grep E0 $lvimgDCtapes > /tmp/lvimgtapesDC");
		my $exval  = $? >> 8;
		if ( $exval == 1 ) {

			print OUT "Print Date: $dm\n";
			print OUT "There are no vault tapes for lvimg DC drives\n";
			print OUT "These specific tapes are active therefore will not be ejected\n";
			
		} else {

			print OUT "\n";
			print OUT "- - - - - - - - - - - - - - - - - - - - - - - - \n";
			print OUT "Print Date: $dm\n";
			print OUT "EDM Vault Tape List for client lvimage DC drives\n";
		}

	close (OUT);
	close (OUT2);
}

## END MAIN;

#&ftpme;
&EDM;
#&DOMINO;
&LVIMAGE_L;
&LVIMAGE_K;
&LVIMAGE_JI;
&LVIMAGE_GH;
&LVIMAGE_FE;
&LVIMAGE_DC;
