#!/usr/bin/perl

### Copyright 2003 AcornSoft ###

use Config;
$osx_version = qx/sw_vers -productVersion | cut -d . -f 2/;
chomp $osx_version;
if ($osx_version > 4) {
# run 32-bit because MacOSX::File uses Carbon
$arch = "/usr/bin/arch -" . (substr($Config{byteorder}, 0, 1) == "1" ? "i386" : "ppc") . " ";
} else {
# arch on 10.4 and earlier doesn't have this capability
$arch = "";
}

$psync = "$arch/usr/local/bin/psync";

# Turn off buffering
$|=1;

$d = '';   # option variable with default value (false)
$r = '';       # option variable with default value (false)
$f = '';       # option variable with default value (false)

$ARGV_length=scalar @ARGV;
if(@ARGV) {
$f=$ARGV[0];
$d=$ARGV[1];
$r=$ARGV[2];
$mainHD=$ARGV[3];
$secondHD=$ARGV[4];
}

if($d) {
$d="$d";
}
else {
$d="";
}
if($f) {
$f="$f";
}
else {
$f="";
}
if($r) {
$r="$r";
}
else {
$r="";
}

if((substr($mainHD,0,1)) eq " ") {
$mainHD=~s/ //;
}

if(-d $mainHD && -d $secondHD) {

print `touch /var/log/psync.log`;
open(STDOUT, "| tee /var/log/psync.log");
$now=localtime();
print "Backup begun $now\n";
open(INPUT, "$psync $d $f $r '$mainHD' '$secondHD'|");

while(<INPUT>) {
print $_;
}
$now=localtime();
print "Backup completed: $now\n";
close INPUT;
close STDOUT;
} # end if
else {
open(FILE, ">/var/log/psync.log");
$now=localtime;
print FILE "$now\n";
print "$now\n";
print FILE "Backup drive wasn't connected\n\n";
print "One of the folders specified wasn't there (was your backup drive connected?)\n";
close FILE;
}
