#!/usr/bin/perl
#
# XIRC Configuration Script
# by Ken Hollis

# Clear the screen
system("clear");

# Set internal stuff
$VERSION = "0.0.1J";
$DEFIRCSERVER = "irc-2.texas.net";

# Header
print <<"EOT";
Welcome to the XIRC Configuration Script!
This will configure version ${VERSION} of XIRC

Pressing [RETURN] at all prompts will accept the default value in brackets.

EOT

# Get the IRC Server name
if ($ENV{'IRCSERVER'} ne "") {
	print "You already have an IRC Server set for default with IRC.  You can use\n";
	print "this server if you wish, or you may override it here.\n\n";
	print "[$ENV{'IRCSERVER'}]: ";
	$IRCSERVER = <STDIN>;
	chop($IRCSERVER);
	if ($IRCSERVER eq "") {
		$IRCSERVER = $ENV{'IRCSERVER'};
	}
} else {
	print "You have not specified a default IRC Server.  This is specified in\n";
	print "the environmental variable \"IRCSERVER\" for IrcII.  Please enter one\n";
	print "below, or you may use the default given.\n\n";
	print "[$DEFIRCSERVER]: ";
	$IRCSERVER = <STDIN>;
	chop($IRCSERVER);
	if ($IRCSERVER eq "") {
		$IRCSERVER = $DEFIRCSERVER;
	}
}

# Get defaults for nickname, and the like
print "\nEnter a nickname (under 9 chars, please): ";
$NICK = <STDIN>;
chop($NICK);

if ($NICK eq "") {
	print "\nYou must specify a nickname.  Rerun configure.\n\n";
	exit;
}

$WHOAMI = `whoami`;
chop($WHOAMI);

print "\nUsing \"$WHOAMI\" as the default username.\n\n";

# Get a realname field
print "Enter your realname field (under 80 chars please)\n";
$REALNAME = <STDIN>;
chop($REALNAME);

if ($REALNAME eq "") {
	print "\nNo realname specified.  Rerun configure.\n\n";
	exit;
}

# Get fully qualified domain name
$FQDN = `hostname`;
chop($FQDN);
$FQDN .= ".";

if (-e "/bin/dnsdomainname") {
	$FQDN .= `/bin/dnsdomainname`;
	chop($FQDN);
} elsif (-e "/bin/domainname") {
	$FQDN .= `/bin/domainname`;
	chop($FQDN);
} else {
	$FQDN .= "unknown.domain";
}

# Prompt for a FQDN entry
print "\nYou need to specify the fully qualified domain name (FQDN) of your\n";
print "system.  Please enter it below, or hit [RETURN] if what you see is correct.\n\n";
print "[$FQDN]: ";

$OUR_FQDN = <STDIN>;
chop($OUR_FQDN);

if (($FQDN =~ /unknown.domain/) && ($OUR_FQDN eq "")) {
	print "\nConfigure could not find your domain.  Please rerun Configure and\n";
	print "enter it in manually.\n\n";
	exit;
} elsif ($OUR_FQDN ne "") {
	$FQDN = $OUR_FQDN;
}

# Create the configuration file
print "\n";
print "Creating Config.h...\n";

open(CF, "Config.h.in");
open(CFO, ">Config.h");

while(<CF>) {
	chop;
	s/\$0/#define\tDEFAULT_IRC_SRV\t\t\"$IRCSERVER\"/;
	s/\$1/#define\tDEFAULT_NICK\t\t\"$NICK\"/;
	s/\$2/#define\tDEFAULT_USERNAME\t\"$WHOAMI\"/;
	s/\$3/#define\tDEFAULT_REALNAME\t\"$REALNAME\"/;
	s/\$4/#define\tFQDN\t\t\t\"$FQDN\"/;

	print CFO "$_\n";
}

close(CF);
close(CFO);

print <<"EOT";

Okay, we're done.  You can either edit Config.h if you need to, or you 
can type \"make\" to begin making XIRC.  You may want to edit the 
\"xirc.conf\" file before running XIRC for the first time.

EOT
