#!/usr/bin/perl
$ver = "0.1";
#
# this perl script is designed for the ifmail package by eugene grosser.
#

# ifcico-config-file:
$config = "/etc/ifmail/config";

# this is the base directory, where the hlo-files are stored and the
# default outbound
$flo_path = "/var/spool/fnet";
$outbound = "/out"; # don't be confused, it means "/var/spool/fnet/out"

# end of configuration #############################################

# main()
#
if ((@ARGV < 1) || @ARGV > 2 || $ARGV[0] eq "-?") {
    &usage;
} elsif ($ARGV[1] ne "") {
    $node = $ARGV[0];
}
$file = $ARGV[1];

if (! ($node =~ /\d+:\d+\/\d+/)) {
    die "Wrong address: $node!!!";
}

($zone, $netnode) = split (/:/, $node);
($fnode, $fpnt) = split (/\./, $netnode);
$hlofile = sprintf("%04x%04x", split(/\//, $netnode));
$pnt = sprintf("%08x", $fpnt);


if ($hlofile eq "00000000") {
    print ("can't resolve the fido address \"$node\"\n");
    &usage;
} else {
    if ( $zone == 2 ) {
        $reqfile = $flo_path . $outbound . "/" . $hlofile . ".hlo";
          if ( $fpnt > 0 ) {
		  $reqfile = $flo_path . $outbound . "/" . $hlofile . ".pnt" . "/" . $pnt . ".hlo";
          }
    } elsif ( $zone < 7 ) {
        $reqfile = $flo_path . $outbound . ".00$zone/" . $hlofile . ".hlo";
          if ( $fpnt > 0 ) {
              $reqfile = $flo_path . $outbound . ".00$zone/" . $hlofile . ".pnt" . "/" . $pnt . ".hlo";
          }
    } else {
        $outbound = "";
        open (CONFIG, "< $config") || die "can't open $config";
        while ($_ = <CONFIG>) {
            if ( /^address.*$zone:.*\@(\w*)/ ) { $outbound = $1; }
        }
        if ( $outbound eq "" ) {
            die "No Zone $zone found in $config";
        }
        $reqfile = $flo_path . "/$outbound/" . $hlofile . ".hlo";
          if ( $fpnt > 0 ) {
              $reqfile = $flo_path . "/$outbound/" . $hlofile . ".pnt" . "/" . $pnt . ".hlo";
          }
    }

    # seperate the file names of the first argument
    @files = split (/\s+/, $ARGV[1]);

    # open the flofile for appending
    open(FLOFILE, ">>" . $reqfile) || die "can't open $reqfile";
        while (@files) {
            print (FLOFILE shift(@files), "\r\n");
        }
    close(FLOFILE);

    print ("--- fido send-file $ver ---------------------------\n");
    print ("sending \"$file\" to fido-node $node\n");
    print ("--- executing ifstat .. ------------------------\n");
    system("/usr/local/lib/ifmail/ifstat");
}

#
# subroutine "usage"
#
sub usage {
    printf ("--- fido sending files %s -------------\n", $ver);
    printf ("usage: ifhold <4-d address> <file> [<file>]\n");
    printf ("e.g. : ifhold 2:2410/305 /pub/allfiles.lzh\n");
    exit 1;
}
