#!/usr/bin/perl
#
# $Id: monout,v 1.3 2002/08/04 02:18:39 dyff Exp $
# monout is a hacked version of out-ls made to work with xmonout
# v .1
#
# This script lists the outbound of fidogate. It is currently tailored
# to a UNIX system using ifcico from ifmail as the mailer. 
#
# modified Nov 97 by Len Ovens (len@studio.cbbs.org)

# Usage monout -lsabptB
# -s	size of files
# -p	point directories
# -b	show .bsy files
# set default below (also see bottem of this file)

#set this to match your outbound root
$OUTBOUND = "/var/spool/bt";


require "getopts.pl";

&Getopts('spb');

if($opt_b) {
	$bsy = "bsy";
	}
	else{
	$bsy = "bxy";
	}


# this is set up for unix style outbounds
# see out-ls in the scripts/outbound directory of the source tree
# for how to do dos style outbounds
#%dirs = (
#    '/v', '/v',
#);


sub file2addr {
    local($zone, $name) = @_;

    $net   = hex(substr($name,  0, 4));
    $node  = hex(substr($name,  4, 4));
    $point = hex(substr($name, 17, 4));

    if($point != 0) {
	return "$zone:$net/$node.$point";
    }
    else {
	return "$zone:$net/$node";
    }
}



sub do_file {
    local($zone,$dir,$file) = @_;

    local($flavor, $isflo, $isout, $addr, $t, $s, $n, $nl);


    if($file =~ /pnt/) {
	$flavor = substr($file, 22, 3);
    }
    else {
	$flavor = substr($file, 9, 3);
    }
    $flavor =~ tr/a-z/A-Z/;
    $isflo  =  $file =~ /\.[fhdc]lo$/;
    $isout  =  $file =~ /\.[ohdc]ut$/;
    $addr   =  &file2addr($zone, $file);

    print $flavor, " ", $addr;

    if($isflo) {
	open(FLO, "$dir/$file")      || die "Can't open $dir/$file";
	$s = 0;
	$n = 0;
	while(<FLO>) {
	    s/\cM?\cJ$//;
	    next if( /^;/ );
	    $s += &print_flo_entry( $dir, $_ );
	    $n++;
	}
	print "  ", &ksize($s), sprintf(" in %d files.;", $n) if $n>0;
    }
    if($isout) {
	($s, $t) = &size_time("$dir/$file");
	print "    ";
	print &ksize($s),   "  ";
	print ";";
    }
    unless($isflo || $isout) {
	print ";";
	}
}



sub print_flo_entry {
    local($dir, $line) = @_;

    local($type, $drive, $file, $short, $t, $s);

    $type  = substr($line, 0, 1);
    if($type =~ /[#~^]/ ) {
	$line = substr($line, 1, length($line)-1);
    }
    else {
	$type = " ";
    }
    $file  = $line;

    ($s, $t) = &size_time($file);

    return $s;
}



sub size_time {
    local($file) = @_;

    local($t, $s, $d);

    ($d,$d,$d,$d,$d,$d,$d, $s, $d, $t) = stat($file);

    return ($s, $t);
}



sub ksize{
    local($size) = @_;

    local($k);

    if($size eq "") {
	return "   N/A";
    }
    else {
	if($size == 0) {
	    $k = 0;
	}
	elsif($size <= 1024) {
	    $k = 1;
	}
	else {
	    $k = $size / 1024;
	}
	return sprintf("%5dK", $k);
    }
}



sub do_point_dir {
    local($zone, $dir, $pdir) = @_;

    opendir(DIR, "$dir/$pdir")       || die "Can't open $dir/$pdir";
    local(@files) = readdir(DIR);
    closedir(DIR);
    @files = sort(@files);

    for(@files) {
	if( /^0000[0-9a-f]{4}\.([fhdc]lo|[ohdc]ut|${bsy})$/ ) {
	    &do_file($zone,$dir,"$pdir/$_");
	}
    }
}



sub do_dir {
    local($zone,$dir) = @_;

    opendir(DIR, $dir)               || die "Can't open $dir";
    local(@files) = readdir(DIR);
    closedir(DIR);
    @files = sort(@files);

    for(@files) {
	if( /^[0-9a-f]{8}\.([fhdc]lo|[ohdc]ut|${bsy}|\$\$.)$/ ) {
	    &do_file($zone,$dir,$_);
	}
	if( !$opt_p && /^[0-9a-f]{8}\.pnt$/ ) {
	    &do_point_dir($zone,$dir,$_);
	}
    }
}



&do_dir(2,     "$OUTBOUND/out");
&do_dir(242,   "$OUTBOUND/out.0f2");
# &do_dir(8,   "$OUTBOUND/outb");
# &do_dir(220, "$OUTBOUND/cccnet.0dc");
# &do_dir(242, "$OUTBOUND/out.0f2");
# &do_dir(254, "$OUTBOUND/out.0fe");
