#! /bin/perl --    # -*-Perl-*-
#
# outflow-stat - display outgoing feed statistics (#articles)
#                this works for innfeed only
#
# History: This program is based on some ideas (and source code lines) of
#          Felix Kuglers, SWITCH, inflow-package and could be part of it.
#
# 961029 V1.0   released
# 970109 V1.1   change ls to readdir for sitelist fix strip path statement
# 970113 V1.2  	aligned some paths, renumbered version (FK)
#
$Copy="(c) 1996 Gerhard.Winkler\@univie.ac.at";

$RELDATE = "Tue Jan 14 01:03:57 MET 1997";
$RELEASE = "V1.2";
#
#
# local settings ----------------------------------------------------
#
$SPOOLDIR  = "/var/spool/news/out.going";
$CONFFILE  = "/usr/local/news/config/innfeed.conf";
$OUTFILE   = "/usr/local/news/stat/outflow.html";
#
# local HTML stuff: you can put custom header & footer information into 
#                   local files; optional...
#
$HTMLHEADER = "/usr/local/news/stat/inflowhtmlheader";
$HTMLFOOTER = "/usr/local/news/stat/inflowhtmlfooter";
# 
# end local settings ------------------------------------------------

require "getopts.pl";

($path,$0) = ($0 =~ /^(.*)\/([^\/]+)$/);                # strip path...

$Usage="$0    -  $Copy

Release $RELEASE  of $RELDATE

Process information from spoolfiles in $SPOOLDIR
and show how many articles should be sent to the remote site.
Output either in Text or HTML format.

Usage: 	$0 [-dhwF] 

Parameters:

   -d:            turn on verbosity; for debugging only
   -h:            This help.
   -w:            WWW support: show results in HTML table format
   -F:            write to file (defined in script header)

Optional HTML-stuff:

header file: $HTMLHEADER
footer file: $HTMLFOOTER

\n";

#
# initialisation ----------------------------------------------------
#

&Getopts('dhwF');

if ($opt_h) { print "$Usage"; exit 0; }

if ($opt_F) {
   open(OUT,">$OUTFILE") || die "can't open $OUTFILE\n";
} else {
   open(OUT,">-");
}

$date = `date`;
$fqdn = &gethostandfqdn;

#  check for custom header & footer files
if ($opt_w) {
   if (open(IN,$HTMLHEADER)) { while (<IN>) { $htmlheader .= $_; } close(IN); }
   if (open(IN,$HTMLFOOTER)) { while (<IN>) { $htmlfooter .= $_; } close(IN); }
}

# main calculation part
# -----------------------------------------------------------------------------

&readconf;
&initpage;

chdir $SPOOLDIR || die "Can't change to $SPOOLDIR directory\n";

opendir(DIR,"$SPOOLDIR");
@sitelist = grep(/\.lock$/,readdir(DIR));
closedir(DIR);

foreach $site (sort @sitelist) {
   $site =~ s/\.lock$//;
   print "check site: $site\n" if ($opt_d);

   $scount = $icount = $ocount = $offset = 0;
   $file = $site;
   if (-e $file) {
      $scount = &countfile($file);
      print "found articles in $file: $scount\n" if ($opt_d);
   } else {
      print "no $file\n" if ($opt_d);
   }

   $file = $site . ".input";
   if (-e $file) {
      $icount = &countfile($file);
      print "found articles in $file: $icount\n" if ($opt_d);
      $check = $site . ".checkpoint";
      if (open(F,"<$check")) {
         read(F,$offset,512);
         close(F);
      }
      @status = stat($file);
      $size = @status[7];
      $icount = $icount - ($icount * $offset / $size);
      $icount = int($icount);
      print "found articles in $file minus offset: $icount\n" if ($opt_d);
   } else {
      print "no $file\n" if ($opt_d);
   }

   $file = $site . ".output";
   if (-e $file) {
      $ocount = &countfile($file);
      print "found articles in $file: $ocount\n" if ($opt_d);
   } else {
      print "no $file\n" if ($opt_d);
   }

   $count = $scount + $icount + $ocount;


   if ($opt_w) {
      print OUT "<TR>
                 <TD> $site </TD>
                 <TD> $conflist{$site} </TD>
                 <TD> $count </TD>
                 </TR>\n";
   } else {
      printf(OUT "%15s %35s %10s\n",$site,$conflist{$site},$count);
   }


}

&closepage;



# countfile
# -----------------------------------------------------------------------------
# count number of articles (lines) in file
# maybe there will be some size (byte) of articles in it
sub countfile {
   local($f) = @_;
   local($c) = 0;
   open(F,"<$f") || return $c;
   while(<F>) {
   }
   $c = $. - 1;
   close(F);
   return($c);

}



#readconf
# -----------------------------------------------------------------------------
#read innfeed configuration files and extract site names

sub readconf {
   open(F,"<$CONFFILE") || die "can't open innfeeds config\n";
   while(<F>) {
   next if (/^#/);
   next if (/^$/);
   ($a,$b) = split(/:/);
   $conflist{$a} = $b;
   }
   close(F);
}



# initpage
# -----------------------------------------------------------------------------
# Print Page Headers 
#
sub initpage {
    if ($opt_w) { 
	print OUT "<html><head><!--Content-type: text/html\nRefresh: 60-->\n\n";
	print OUT "<title>Status of outgoing feeds at $fqdn</title></head>
                   <body>
                   $htmlheader
                   <h1>Status of outgoing feeds at $fqdn</h1>
                   <p><b>calculated at:</b> $date</p>\n";
        print OUT "<center><TABLE BORDER>
                   <TR><TH>Feedname</TH>
                   <TH>Sitename</TH>
                   <TH>Articles to transfer</TH>
                   </TR>\n";

    } else {
        print OUT "Status of outgoing feeds at $fqdn\n\n";
        print OUT "calculated at: $date\n";
	printf(OUT "%15s %35s %10s\n\n","Feedname","Sitename","Articles to transfer");
    }
}


# closepage
# -----------------------------------------------------------------------------
# close page, write footers...
#
sub closepage {
    if ($opt_w) {
        print OUT "</table></center><P><P>\n";
	if ($htmlfooter) { print OUT "$htmlfooter\n"; } 
	print OUT "</body></html>\n";
    } else { print OUT "\n\n"; }
}


# gethostandfqdn
#---------------------------------------------------------------------- 
# construct fully qualified domain name...
sub gethostandfqdn {
    chop($str=`uname -n`);
    if ($str =~ /\./) {             # str is fqdn
	$fqdn = $str;
	($hostname) = ($str =~ /^([^.]+)\./);
    } else {                        # str is simple hostname
	$hostname = $str;
	$str = `/bin/grep domain /etc/resolv.conf`;
	$str =~ /domain\s*(\S+)$/;
	$fqdn = $hostname . "." . $1;
    }
}

