#!/s/std/bin/perl
use strict;
use warnings;

use File::Copy qw(copy);

# This script is a perl-ized version of the linux-save shell script for launch control
# To make this process a little easier, this script will be run on the remote computer
# and write files out to /common/tmp
# the main script will then deal with the data, and delete the uneeded files.

my $backupdir = "/common/tmp/launch_control";
my $hostname = `hostname`;
my $logfile = "$backupdir/log.txt";
my $me = `whoami`;
my $sys = `/usr/afsws/bin/sys`; 
my $timestamp;

chomp $hostname;
chomp $me;
chomp $sys;

my $hostdir = "$backupdir/$hostname";

# open log file, die if you can't read it
open(LOG, ">>", $logfile) or die "Could not open file $logfile for writing!\n";

# make the subdir for each host in /common/tmp/[hostname]

mkdir $hostdir;

#check to make sure that I am run as root, since we are copying
#fstab/crontab and other things that require it

$timestamp = getLoggingTime();
print LOG "$timestamp Started processing local data on $hostname\n";

if ($me eq 'root') {

	# Now that we know we are root, lets do some stuff
	#
	# lets start with the fstab
	print "Saving /etc/fstab..\n";
	print LOG "$timestamp Saving /etc/fstab for $hostname\n";
	copy "/etc/fstab", "$hostdir";
	
	# save df -h just for reference
	my @df = `df -h`;

	open(DF, ">>", "$hostdir/df.txt") or die "Could not open df.txt for writing\n";
	print DF @df;
	close DF;

	# lets move onto cran jabs
	mkdir "$hostdir/crontabs";

	copy "/usr/spool/cron/*", "$hostdir/crontabs";
	unlink "$hostdir/crontabs/root";
	unlink "$hostdir/crontabs/lstrivia";

	# local mail...
	
	print LOG "$timestamp Stopping sendmail service, and saving mqueue\n";
	system "service sendmail stop";

	# put a sleep here to wait for everything to stop correctly
	sleep(4);

	# service status:  sendmail (pid  6932) is running...
	# service status: sendmail is stopped
	# systemctl thingers: Active: inactive Active: active (running)
	
	# this is probably overkill, but I am going to make checks to see that sendmail
	# is really stopped, and log it
	
	if ($sys eq "amd64_rhel6") {

		my $status = `service sendmail status`;

		if ($status =~ /.+stopped/g) {
			print LOG "$timestamp Stopped..\n";
		} else {
			print LOG "$timestamp Sendmail failed to stop..\n";
			print LOG "$status\n";
		}
	}


	# check the mailqueue and back it up if there is anything in there
	# this may be a super hackish way to determine if there is anything in there
	# but it seems to work.
	
	$timestamp = getLoggingTime();
	print "Checking /var/spool/mqueue for files...\n";
	print LOG "$timestamp Checking /var/spool/mqueue\n";

	my $mqueuefiles = `ls /var/spool/mqueue | wc -l`;

	if ($mqueuefiles >= 1) {
		print LOG "$timestamp Saving /var/spool/mqueue\n";
		print `tar cvzf $hostdir/mqueue.tar.gz -C /var/spool/mqueue --exclude syslog --exclude lstrivia .`;
	} else {
		print LOG "$timestamp Nothing in /var/spool/mqueue\n";
	}

	# check the mail spool for files and back it up if required.
	# as with the above example, this may be a super hackish way to check to see if there
	# are files, but it does appear to work.
	
	$timestamp = getLoggingTime();
	print "Checking /var/spool/mail for files...\n";
	print LOG "$timestamp Checking /var/spool/mail\n";

	my $mailfiles = `find /var/spool/mail -type f -size +0 | wc -l`;

	if ($mailfiles >= 1) {
		print LOG "$timestamp Saving /var/spool/mail\n";
		print `tar cvzf $hostdir/mail.tar.gz -C /var/spool/mail --exclude adm --exclude bin --exclude games --exclude gdm --exclude gopher --exclude lp --exclude mail --exclude sync --exclude db2 --exclude root --exclude nfsnobody --exclude lstrivia .`;
	} else {
		print LOG "$timestamp Nothing in /var/spool/mail\n";
	}

	# restart sendmail
	if ($sys eq "amd64_rhel6") {

		system "service sendmail start";

		#sleep one more time to let things settle
		sleep(2);

		my $status = `service sendmail status`;

		if ($status =~ /sendmail.+is running/g) {
			print LOG "$timestamp Sendmail restarted...\n";
		} else {
			print LOG "$timestamp Sendmail failed to restart..\n";
			print LOG "$status\n";	
		}
	}

	print "Done with local mail..\n";
	# the final step is to copy the ssh keys over
	
	$timestamp = getLoggingTime();
	print LOG "$timestamp Copying SSH host keys\n";

	mkdir "$hostdir/openssh";

	print `cp /etc/ssh/ssh_host_* $hostdir/openssh`;

} else {
	#I'm not root
	print LOG "$timestamp LinuxSave script run as non root user: $me\n";
}

print LOG "$timestamp Finished processing $hostname\n";
close LOG;

print "\nSleeping for 10 seconds so you can see error messages if there are any\n";
sleep(10);


sub getLoggingTime {

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
                                   $year+1900,$mon+1,$mday,$hour,$min,$sec);
    return $nice_timestamp;
}
