#!/usr/bin/perl
# sophoscdcreate Copyright Michael Howe 2005
# Released under the GNU GPL (a copy of which is available from
# http://www.gnu.org/copyleft/gpl.html )
#
# Purpose:
#   Creates a cd image for command-line sophos scanning with the latest IDE
#   files from ftp://micros.oucs.ox.ac.uk.  They are saved to the ides/ dir of
#   the CD.
# Prerequisites:
#   - A base directory containing the appropriate executable files for sophos.
#   - Internet access to ftp://micros.oucs.ox.ac.uk

use warnings;
use strict;
use Net::FTP;
use File::Path;

# Change these appropriately:
#  Source of the image (directory with ide files, exe files, etc):
my $img_src = '/home/michael/computing/antivirus/sophos/cd/';
#  Destination iso file to create:
my $img_dst = '/home/michael/disk_images/isos/sophos_cli.iso';

# These probably should be ok as the defaults.
#  FTP site to get IDE files from:
my $ide_srchost = 'micros.oucs.ox.ac.uk';
#  Directory on the FTP server where thi IDE files live:
my $ide_srcdir = '/antivirus/sophos/idefiles/';
#  Directory to store the downloaded IDE files in:
my $ide_dstdir = "${img_src}ides/";
#  A place to backup the old IDE files to:
my $ide_backupdir = "${img_src}ide-backup/";

# This shouldn't need changing.
#
# Do some cleanup and check IDE dirs exist.
chdir( $ide_dstdir )
	or die "Couldn't change to $ide_dstdir\n";
if ( -d $ide_backupdir ) {
	rmtree( $ide_backupdir )
		or die "Couldn't remove backup dir $ide_backupdir\n";
}

# Backup current IDEs
rename( $ide_dstdir, $ide_backupdir )
	or die "Couldn't backup ide dir ($ide_dstdir -> $ide_backupdir)\n";
mkdir( $ide_dstdir )
	or die "Can't make ide dir $ide_dstdir\n";


# FTP magic:
my $ftp = Net::FTP->new( $ide_srchost )
	or die "Couldn't connect: $@\n";
$ftp->login()
	or die "Couldn't login\n";
$ftp->cwd( $ide_srcdir )
	or die "Couldn't change directory\n";
my @ides = $ftp->ls();
foreach my $ide ( @ides ) {
	$ftp->get( $ide ) or warn "Couldn't get ide $ide.\n";
}
# we're done
$ftp->quit;

system( "mkisofs -o $img_dst $img_src" )
	or warn "Error - cannot make iso $img_src: $!\n";


# vim: tw=80
