#!/usr/bin/perl

$FEDDISRC="/usr/src/FEddi-dev";

require "importenv.pl";
print "\nFEddi 0.9 User-Config maker\n\n";

$uid = $ENV{'USER'};
$gid = getgrnam("fido");
die "User and group 'fido' doesn't exist. Run make config.\n" 
    unless (defined $uid && defined $gid);

@group=getgrnam("fido");
if ($group[3] !~ /\b$LOGNAME\b/) {
	die <<EOF;
You are no member of the fido group!
Please ask your system administrator to add you to this group and then
try again.
EOF
}

sub input {
    local($line);
    print $_[0];
    $line=<STDIN>;
    chop($line);
    $line;
}

sub makedir {
    local($path) = @_;
    local($err);
    $err=system("mkdir -p -m 0775 $path");
    if ($err!=0) { die "Error while creating $path\n"; }
    chown $uid, $gid, $path;
}

sub dectohex {
	local($hex);
	$hex=unpack("H4",pack("S",$_[0]));
	$hex =~ s/(..).(.)/$2$1/;
	$hex;
}

if (-e $HOME."/.feddirc"){
    die <<EOF;
A .feddirc exists already in your HOME-Directory, please remove it before 
using this tool to create a new .feddirc, or run XXX to reconfigure your 
old .feddirc.
EOF
}
open(FEDDIRC, ">$HOME/.feddirc") || die "Can't create $HOME/.feddirc.\n";

if (-e $HOME."/binkley.cfg"){
    die <<EOF;
A binkley.cfg exists already in your HOME-Directory, please remove it before 
using this tool to create a new binkley.cfg.
EOF
}
open(BINKLEY, ">$HOME/binkley.cfg") || die "Can't create $HOME/binkley.cfg.\n";
open(IFCONFIG, ">$HOME/config.if") || die "Can't create $HOME/config.if.\n";

@name=getpwnam($LOGNAME);
print "Hello $name[6]!\n\n";
# for ($name=""; $name eq ""; $name=&input("Please enter your REAL name: ")){}
print FEDDIRC <<EOF;
;
; This .feddirc was automatically created with config.user
;
; Profile Section
;
PROFILE $name[6]
EOF

print BINKLEY <<EOF;
FEddiNodelist
Port 2
baud     38400
LockBaud 38400
Init ATZ0|~AT&K6|~
Prefix ATDT
PreDial ~
PreInit |v``^``
LogLevel 5
LineUpdate
Gong
AutoBaud
PollTries 10
PollDelay 600
Unattended
BoxType 0
NiceOutBound  
ReadHoldTime 1
System Point of $name[6]
Sysop $name[6]
StatusLog    $HOME/fnet/log/binkley.log  200
Downloads    $HOME/fnet/inbound/
CaptureFile  $HOME/fnet/log/session.log
NetFile      $HOME/fnet/inbound/
Hold         $HOME/fnet/outbound/
Nodelist     $HOME/fnet/nodelist/
EOF

print IFCONFIG <<EOF;
logfile		$home/fnet/log/iflog
debugfile       $home/fnet/log/ifdebug
verbose		0
inbound		$HOME/fnet/inbound
listinbound	$HOME/fnet/inbound
protinbound	$HOME/fnet/inbound
outbound	$HOME/fnet/outbound
public		/var/spool/uucppublic
nodelist	$HOME/fnet/nodelist/
ModemPort	ttyS1:L38400
ModemReset	ATZ0\\r
ModemDial		ATDT\\T\\r
ModemHangup	ATZ0\\r
ModemOK		OK
ModemConnect	CONNECT
ModemError	BUSY
ModemError	NO\\sCARRIER
ModemError	NO\\sDIAL
ModemError	RING\\r
ModemError	ERROR
TimeoutReset	3
TimeoutConnect	70
options (time Any) NoHold
Name	        Point of $name[6]
Location	Nowhere
SysOp	        $name[6]
Phone		49-irgendwas
Speed		9600
Flags		XA,V32B,V42B
EOF

for ($mailer="nix";
     $mailer ne "" && $mailer ne "ifcico" && $mailer ne "binkley";
     $mailer=
     &input("Do you use binkley or ifcico? [ifcico] ")){}

if ($mailer eq "") { $mailer="ifcico"; }

print "\n";

for ($address=""; 
     $address eq "" || $address !~ /\d+:\d+\/\d+\.\d+/;
     $address = 
     &input("Enter now your main address (e.g. 2:2454/130.103): ")){}

if ($address =~ /^[1-6]:/) {
    print "\nThis seems to be FidoNet!\n\n";
    $netname="FidoNet";
    $routing="1:* 2:* 3:* 4:* 5:* 6:*";
    &makedir("$HOME/fnet/outbound.001");
    &makedir("$HOME/fnet/outbound");
    &makedir("$HOME/fnet/outbound.003");
    &makedir("$HOME/fnet/outbound.004");
    &makedir("$HOME/fnet/outbound.005");
    &makedir("$HOME/fnet/outbound.006");
} else {
    print"\nUhoh! I don't know this Net!\n";
    for ($netname="";
	 $netname eq "";
	 $netname = &input("How do you call this net (e.g. FidoNet): ")){}
    $routing=$address;
    $routing =~ s/^(\d+:).*/$1*/;
    $zone=$address;
    $zone =~ s/^(\d+).*/$1/;
    $zone=&dectohex($zone);
    &makedir("$HOME/fnet/outbound.$zone");
}

for ($password="";
     $password eq "" || length($password) > 8;
     $password = &input("Enter your session password (e.g. BORNDEAD): ")){}

print FEDDIRC "$address $netname $password outbound $routing\n";

print BINKLEY "Address ".$address."\@".$netname.".org\n";
$node=$address;
$node =~ s/(\d+:\d+\/\d+)\.\d+/$1/;
print BINKLEY "Key !".$password." ".$node."\n";
print BINKLEY "Domain ".$netname.".org outbound\n";

print IFCONFIG "address ".$address."\@".$netname.".org\n";
print IFCONFIG "password $node $password\n";

print "Now you enter further points...(Enter empty address to end.)\n"; 

for ($address = "dummy"; $address ne "";){
    print "\n";
    for ($address = "dummy";
	 $address ne "" && $address !~ /\d+:\d+\/\d+\.\d+/;
	 $address =		
	 &input("Enter an address (e.g. 2:2454/130.103): ")){}

    if ($address ne ""){
	for ($netname="";
	     $netname eq "";
	     $netname = &input("How do you call this net (e.g. FidoNet): ")){}
	$routing=$address;
	$routing =~ s/^(\d+:).*/$1*/;
    $zone=$address;
    $zone =~ s/^(\d+).*/$1/;
    $zone=&dectohex($zone);
    &makedir("$HOME/fnet/outbound.$zone");

	for ($password="";
	     $password eq "" || length($password) > 8;
	     $password = &input("Enter the session password (e.g. BORNDEAD): ")){}
 
	print FEDDIRC "$address $netname $password outbound $routing\n";

 	print BINKLEY "Address ".$address."\@".$netname.".org\n";
	$node=$address;
	$node =~ s/(\d+:\d+\/\d+)\.\d+/$1/;
	print BINKLEY "Key !".$password." ".$node."\n";
 	print BINKLEY "Domain ".$netname.".org outbound\n";

	print IFCONFIG "address ".$address."\@".$netname.".org\n";
	print IFCONFIG "password $node $password\n";
    }
}
print FEDDIRC <<EOF;
END
;
; Paths
;
MsgBasePath  ~/fnet/msgbase/
InboundPath  ~/fnet/inbound/
OutboundPath ~/fnet/
UtilityPath  ~/fnet/utility/
Log          ~/fnet/log/feddi.log 200
CopyPath     ~/fnet/copy/
NodelistPath ~/fnet/nodelist/
;
; Misc
;
Packer       /usr/bin/zip -q -m -k -j %s %s
Beep         Yes
AutoDelEmpty Yes
KeepPKT      No
KeepNL       Yes
KeepBackups  No
ShowAllAddr  Yes
MaxMsgLength 64k
Mailer       $mailer
;
; End of .feddirc
;
EOF

&makedir("$HOME/fnet/msgbase");
&makedir("$HOME/fnet/copy");
&makedir("$HOME/fnet/log");
&makedir("$HOME/fnet/inbound");
&makedir("$HOME/fnet/utility");
&makedir("$HOME/fnet/nodelist");

close(FEDDIRC);
close(BINKLEY);
close(IFCONFIG);

print "\nCopying utilitys...\n";

system("cp $FEDDISRC/utils/* $HOME/fnet/utility/");

print "\nConfiguration done.\n";

print "\nRemember to change all relevant files to 664/775\n";
print "and fido group!\n\n";
