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



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;
}
