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



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