#!/usr/bin/perl
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";

#### initialise parameters ####
$start_time = `date '+%d/%m/%y %H:%M:%S %Z'`;

$alpha_date = &AlphaDate;

&ReadArguments;

chdir $ENV{RSAT};
$out_dir = "$ENV{RSAT}/archives";

for $folder ("perl-scripts","public_html") {
    warn "; Archiving folder $folder\n" if $verbose;
    $tar_file = "${out_dir}/${folder}_${config_site}_${alpha_date}.tar";
    if ($folder eq "public_html") {
	$excluded = " --exclude tmp --exclude logs --exclude data --exclude papers";
    }
    $command = "tar $excluded -cpf $tar_file $folder; gzip $tar_file";

    warn "$command\n" if ($verbose);
    system $command;
}

$done_time = `date '+%d/%m/%y %H:%M:%S %Z'`;
if ($verbose >= 1) {
    print ";Job started $start_time";
    print ";Job done    $done_time";
}

exit(0);

########################## subroutine definition ############################

sub PrintHelp {
#### display full help message #####
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	RSA-backup

USAGE
        RSA-backup

DESCRIPTION
	Creates a backup of rsa-tools programs, in the for of two tar
	files: one containing perl-script and the other public_html.

CATEGORY
	administration
	
OPTIONS
	-h	(must be first argument) display full help message
	-help	(must be first argument) display options
	-v	verbose
End_of_help
  close HELP;
  exit;
}

sub PrintOptions {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
template options
----------------
-h	(must be first argument) display full help message
-help	(must be first argument) display options
-v	verbose
End_short_help
  close HELP;
  exit;
}

sub ReadArguments {
#### read arguments ####
    foreach my $a (0..$#ARGV) {
	### verbose ###
	if ($ARGV[$a] eq "-v") {
	    if (&IsNatural($ARGV[$a+1])) {
		$verbose = $ARGV[$a+1];
	    } else {
		$verbose = 1;
	    }
	    
	    ### detailed help
	} elsif ($ARGV[$a] eq "-h") {
	    &PrintHelp;
	    
	    ### list of options
	} elsif ($ARGV[$a] eq "-help") {
	    &PrintOptions;
	    
	    
	}
    }
}
