#!/usr/bin/env perl

# check the usage
if ($ARGV[0] eq "-h") { &usage (); }
if ($ARGV[0] eq "-help") { &usage (); }
if (@ARGV != 4) { &usage (); }

# assign the command line arguments
$list   = $ARGV[0];
$tenbin = $ARGV[1];
$medbin = $ARGV[2];
$range  = $ARGV[3];

# check for files to be processed
@status = stat ($list);
if ($status[7] == 0) {
    print STDERR "no files in source list, skipping\n";
    exit 1;
}

# find the appropriate script file
$confdir = `gconfig -q CONFDIR`; chop ($confdir);
$script  = "$confdir/mana/fringe.pro";

# test named Xserver, or start vnc on specified machine
$xhost=`gconfig -q XHOST`; chop ($xhost);
$xdisp=`gconfig -q XDISP`; chop ($xdisp);
if (vsystem ("xdpyinfo -display $xdisp")) {
    print STDERR "X server is not running. start it now on $xhost\n";
    exit 1;
}
$ENV{'DISPLAY'} = $xdisp;

# run mana script:
open (MANA, "|mana --norc");
print MANA "input $script\n";
print MANA "\$RANGE = $range\n";
print MANA "mkjpegs $list $tenbin $medbin $stats\n";
print MANA "exit 1\n";
close (MANA);
if ($?) { die "ERROR problem with fr.jpegs\n"; }

print STDOUT "SUCCESS: finished with fr.jpegs\n";
exit 0;

sub vsystem {
    print STDERR "@_\n";
    $status = system ("@_");
    $status;
}

sub goodbye {
    die "@_\n";
}

sub usage {
    print "USAGE: fr.jpegs (list) (tenbin) (medbin) (range)\n";
    print "  create big and small jpeg images from the input list\n";
    exit 1;
}

# mana within perl:  we embed the mana functions within a macro to trap the 
# exit status.  If mana has an error with one of the steps, it will exit the macros and 
# the second exit will be called. Otherwise, the "go" macro will perform the exit 0
# and mana will exit with status 0
