blob: 508b9eb28d0745fb54f6aebfc7aa56c7a416f1b6 (
plain) (
tree)
|
|
#! /usr/bin/perl -w
use strict;
use FileHandle;
use File::Spec;
use Digest::MD5;
my $system = "@SYSTEM@";
my $outdir = File::Spec->rel2abs($ARGV[0]);
my $netdir = File::Spec->rel2abs($ARGV[1]);
my %donetmpls = ();
sub fetchFile {
my $loc = shift;
if ($loc =~ /^([+\w\d\.\/-]+)$/) {
return $1;
} elsif ($loc =~ /^url\((.*)\)$/) {
my $url = $1;
$url =~ /\/([^\/]+)$/ || die "invalid url $url";
my $fn = "$netdir/$1";
if (! -f $fn) {
print "fetching $url...\n";
system "cd $netdir; wget --quiet -N $url";
if ($? != 0) {
unlink($fn);
die;
}
}
return $fn;
} else {
die "invalid file specified $loc";
}
}
sub hashFile {
my $file = shift;
open FILE, "< $file" or die "cannot open $file";
# !!! error checking
my $hash = Digest::MD5->new->addfile(*FILE)->hexdigest;
close FILE;
return $hash;
}
sub convert {
my $descr = shift;
if (defined $donetmpls{$descr}) {
return $donetmpls{$descr};
}
my ($x, $dir, $fn) = File::Spec->splitpath($descr);
print "$descr\n";
my $IN = new FileHandle;
my $OUT = new FileHandle;
my $tmpfile = "$outdir/$fn-tmp";
open $IN, "< $descr" or die "cannot open $descr";
open $OUT, "> $tmpfile" or die "cannot create $tmpfile";
print $OUT "system : $system\n";
while (<$IN>) {
chomp;
if (/^(\w+)\s*=\s*([^\#\s]*)\s*(\#.*)?$/) {
my ($name, $loc) = ($1, $2);
my $file = fetchFile($loc);
$file = File::Spec->rel2abs($file, $dir);
my $hash = hashFile($file);
print $OUT "$name = $hash\n";
} elsif (/^(\w+)\s*<-\s*([+\w\d\.\/-]+)\s*(\#.*)?$/) {
my $name = $1;
my $file = $2;
$file = File::Spec->rel2abs($file, $dir);
$file = convert($file);
my $hash = hashFile($file);
print $OUT "$name <- $hash\n";
} else {
print $OUT "$_\n";
}
}
close $OUT;
close $IN;
my $hash = hashFile($tmpfile);
my $outfile = "$outdir/$hash-$fn";
rename($tmpfile, $outfile) or die "cannot rename $tmpfile to $outfile";
$donetmpls{$descr} = $outfile;
return $outfile;
}
for (my $i = 2; $i < scalar @ARGV; $i++) {
convert(File::Spec->rel2abs($ARGV[$i]));
}
|