#!/usr/pkg/bin/perl
#
# $Id: ftntickpost.pl,v 4.1 1999/06/27 19:18:13 mj Exp $
#
# Postprocessor for TIC files to be run by ftntick -x.
# Currently only a skeleton.
#
 
require 5.000;

my $PROGRAM = "ftntickpost";
 
use strict;
use vars qw($opt_v $opt_c);
use Getopt::Std;
use FileHandle;

##############################################################################
#
# $Id: config.pl,v 4.4 2001/01/04 20:03:42 mj Exp $
#
# Perl functions to read FIDOGATE config file,
# included by <INCLUDE config.pl> when running subst.pl
#

my %CONFIG;

# specials for DosDrive and Zone
my %CONFIG_dosdrive;
my %CONFIG_zone;



my %CONFIG_default =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"lockdir", "/var/spool/fido/lock",
	"newsspooldir", "/var/news/spool/articles",
	"hosts", "%C/hosts",
	"seq_pkt", "%V/seq/pkt",
	"spooldir", "/var/spool/fido",
	"seq_mail", "%V/seq/mail",
	"seq_toss", "%V/seq/toss",
	"ftpinbound", "/var/spool/bt/ftpin",
	"seq_tick", "%V/seq/tick",
	"logdir", "/var/log/fido",
	"config_main", "%C/fidogate.conf",
	"seq_msgid", "%V/seq/msgid",
	"passwd", "%C/passwd",
	"newsvardir", "/var/news",
	"pinbound", "/var/spool/bt/pin",
	"aliases", "%C/aliases",
	"history", "%V/history",
	"routing", "%C/routing",
	"newslibdir", "/usr/pkg/inn/lib",
	"toss_route", "%S/toss/route",
	"acl", "%C/acl",
	"outpkt_news", "%S/outpkt/news",
	"newsetcdir", "/var/news/etc",
	"toss_toss", "%S/toss/toss",
	"seq_split", "%V/seq/split",
	"seq_news", "%V/seq/news",
	"tick_hold", "%B/tick",
	"config_ffx", "%C/fidogate.conf",
	"areas", "%C/areas",
	"seq_ff", "%V/seq/ff",
	"uuinbound", "/var/spool/bt/uuin",
	"outrfc_news", "%S/outrfc/news",
	"packing", "%C/packing",
	"inbound", "/var/spool/bt/in",
	"vardir", "/var/spool/fido",
	"lock_history", "history",
	"outpkt", "%S/outpkt",
	"logfile", "%G/log",
	"outpkt_mail", "%S/outpkt/mail",
	"config_gate", "%C/fidogate.conf",
	"ifmaildir", "/usr/pkg/sbin",
	"toss_bad", "%S/toss/bad",
	"btbasedir", "/var/spool/bt",
	"bindir", "/usr/pkg/lib/fidogate/bin",
	"seq_pack", "%V/seq/pack",
	"configdir", "/usr/pkg/etc/fidogate",
	"charsetmap", "%L/charset.bin",
	"libdir", "/usr/pkg/lib/fidogate",
	"toss_pack", "%S/toss/pack",
	"outrfc_mail", "%S/outrfc/mail",
     );
my %CONFIG_abbrev =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"P", "pinbound",
	"B", "btbasedir",
	"S", "spooldir",
	"G", "logdir",
	"I", "inbound",
	"V", "vardir",
	"K", "lockdir",
	"L", "libdir",
	"N", "bindir",
	"U", "uuinbound",
	"C", "configdir",
     );



sub CONFIG_read {
    my($file) = @_;
    my($key, $arg);
    local *C;

    $file = CONFIG_expand($file);

    open(C,"$file") || die "config.pl: can't open config file $file\n";
    while(<C>) {
	chop;
	next if( /^\s*\#/ );	# comments
	next if( /^\s*$/  );	# empty
	s/\s*$//;		# remove trailing white space
	s/^\s*//;		# remove leading white space
	($key,$arg) = split(' ', $_, 2);
	$key =~ tr/A-Z/a-z/;
	if($key eq "include") {
	    CONFIG_read($arg);
	    next;
	}
	if($key eq "dosdrive") {
	    my ($d, $path) = split(' ', $arg);
	    $CONFIG_dosdrive{lc($d)} = $path;
	    next;
	}
	if($key eq "zone") {
	    my ($z, $rest) = split(' ', $arg, 2);
	    $CONFIG_zone{$z} = $rest;
	    next;
	}
	$CONFIG{$key} = $arg if(!$CONFIG{$key});
    }
    close(C);
}


sub CONFIG_get1 {
    my($key) = @_;
    my($ukey);

    $ukey = $key;
    $ukey =~ tr/a-z/A-Z/;
    return $ENV{"FIDOGATE_$ukey"} if($ENV{"FIDOGATE_$ukey"});

    return $CONFIG{$key} if($CONFIG{$key});
    return $CONFIG_default{$key};
}


sub CONFIG_get {
    my($key) = @_;
    my($ret);
    my($exp);

    $key =~ tr/A-Z/a-z/;
    return CONFIG_expand( CONFIG_get1($key) );
}


sub CONFIG_expand {
    my($v) = @_;
    my($exp);

    if($v =~ /^%([A-Z])/) {
	$exp = CONFIG_get1($CONFIG_abbrev{$1});
	$v =~ s/^%./$exp/;
    }

    return $v;
}


sub CONFIG_debug {    
    my($key);

    for $key (keys %CONFIG) {
	print "$key = $CONFIG{$key} -> ", CONFIG_get($key), "\n";
    }
}

##############################################################################

getopts('vc:');

# read config
my $CONFIG = $opt_c ? $opt_c : "%C/fidogate.conf";
CONFIG_read($CONFIG);



# usage
die "usage: $PROGRAM [-v] [-c CONFIG] FILE.TIC\n" if($#ARGV != 0);

# read TIC
my $tic_file = $ARGV[0];
my %tic_data = TIC_read($tic_file);


##### do something not very useful #####
for (sort keys %tic_data) {
    printf "%10s = %s\n", $_, $tic_data{$_};
}



exit 0;




##### Read TIC file ##########################################################

sub TIC_read {
    my($tic) = @_;

    local(*F);
    my %v;
    my ($key, $val);

    open(F, $tic)
      || die "$PROGRAM: can't open TIC file $tic: $!\n";

    while(<F>) {
	s/\cM?\cJ?$//;
	($key, $val) = split(' ', $_, 2);
	
	if($v{$key}) {
	    $v{$key} .= " $val";
	}
	else {
	    $v{$key} = $val;
	}
    }

    close(F);

    return %v;
}
