#!/usr/bin/perl -w
use strict;
use diagnostics;

my $input_ok = 0;
my ( $dumpcycle, $number_backups, $per_time_period, $time, $time_period, $yn );
my ( $changer, $runtapes );

print( "First, tell me how often you will be doing a backup\n" );
print( "I will ask for some number per time period\n" );
print( "For example, '2 backups every 2 weeks'\n" );

$number_backups = '';
while( $number_backups !~ /^\d{1,5}$/ ){
	print( "Enter number of backups: " );
	$number_backups = <>;
	chomp( $number_backups );
}

$time_period = 'false';
$per_time_period = '';
while( $time_period eq 'false' ){
	print( "Time period can be d for day, w for week, m for month, y for year\n" );
	print( "For multiple days/weeks/months/years type '2m', '5w', etc\n" );
	print( "Enter time period: " );
	$time_period = <>;
	chomp( $time_period );

	if( $time_period =~ /^(\d{0,6})([dwmy])$/ ){
		$per_time_period = $1;
		$time_period = $2;
		$time_period = lc( $time_period );
		
		if( $per_time_period ne '' ){
			$per_time_period = int( $per_time_period );
			if( $per_time_period == 0 ){
				print( "Number cannot be 0\n" );
				$time_period = 'false';
			}
		} else {
			$per_time_period = 1;
		}
	} else {
		$time_period = 'false';
	}
}

if( $time_period eq 'd' ){
	$dumpcycle = $per_time_period;
	$time = 'day';
} elsif( $time_period eq 'w' ){
	$dumpcycle = $per_time_period * 7;
	$time = 'week';
} elsif( $time_period eq 'm' ){
	$dumpcycle = $per_time_period * 31;
	$time = 'month';
} elsif( $time_period eq 'y' ){
	$dumpcycle = $per_time_period * 365;
	$time = 'year';
}
my $per = '';
if( $per_time_period > 1 ){
	$time .= 's';
	$per = $per_time_period . ' ';
}
my $backups = 'backup';
if( $number_backups > 1 ){
	$backups .= 's';
}

my $backups_per_time =  "$number_backups $backups every $per$time";
print( "You will do $backups_per_time\n" );

print( "Now we will determine the maximum number of tapes you will use per backup\n" );

$yn = '';
while( $yn !~ /^[yn]$/ ){
	print( "Do you have any kind of automatic/manual tape changer? (y/n): " );
	$yn = <>;
	chomp( $yn );
	$yn = lc( $yn );
}

my $tapes_per_backup;
if( $yn eq 'n' ){
	$tapes_per_backup = "1 tape maximum per backup";
	print( "You will use $tapes_per_backup\n" );
	$runtapes = 1;
} else {

	print( "Going to use a tape changer\n" );
	print( "What is the maximum number of tapes you want your changer to use every backup\n" );
	$runtapes = '';
	while( $runtapes !~ /^\d{1,3}$/ ){
		print( "Enter the maximum: " );
		$runtapes = <>;
		chomp( $runtapes );
	}

	my $tapes = 'tape';
	if( $runtapes > 1 ){
		$tapes .= 's';
	}
	$tapes_per_backup = "$runtapes $tapes maximum per backup";
	print( "You will use $tapes_per_backup\n" );
}
my $tapecycle = ( $number_backups + 1 ) * $runtapes;

print( "\n\nTo do $backups_per_time using $tapes_per_backup\n" );
print( "You should use minimum $tapecycle tapes and set the following Amanda configuration:\n\n" );
print( "dumpcycle $dumpcycle days\n" );
print( "runspercycle $number_backups\n" );
print( "runtapes $runtapes\n" );
print( "tapecycle $tapecycle tapes\n\n" );
