#!/usr/bin/perl
use Text::Wrap;
require "Input.pl";	# Some input routines
$|++;
$Text::Wrap::columns = 80;
print("Energymech Configurator (c)2001 MadCamel(madcamel\@energymech.net)\n\n");
sub p { print wrap("\n\n* ", "* ", "@_\n"); print("\n"); }
&load_config_h;	# Load some values from config.h..

if ($dolink) {
     print("\n-=- Linking Configuration -=-\n\n");
     p("In order to link bots you must have a unique entity name for",
	"your mech. You cannot link to other mechs without an",
	"entity name");
     
     $bot{'ENTITY'} = getline("Enter an entity name: ");

     p("A password is needed if you want to link your bot to other bots");
     $bot{'LINKPASS'} = getline("Enter a linkpass: ");

     p("A port where this bot will listen for other bots",
	"to link with. NOTE: Firewalls may block out some",
	"ports, this could be the cause your mech to fail",
	"with linking.");

     while(1) {
	$bot{'LINKPORT'} = getline("Enter a linkport: ");
	if ($bot{'LINKPORT'} < 1024 || $bot{'LINKPORT'} > 65535) {
	     invalid("Linkport must be between 1024 and 65535\n"); 
	     next;
	}
	last;
     }

     if (yesno(1, "Would you like to autolink")) {
	$bot{'AUTOLINK'} = 1;
     }
     else { $bot{'AUTOLINK'} = 0; }
}

print("\n-=- General Configuration -=-\n");
p("Nick for the bot, make one up that suits you");
$bot{'NICK'} = getline("Enter a nickname for the bot: ");
$bot{'USERFILE'} = $bot{'NICK'}.".users";

p("We need a command character. All commands begin with this character");
while (1) {
     $bot{'CMDCHAR'} = getline("Enter a command character: ");
     if (length($bot{'CMDCHAR'}) > 1) {
	invalid("Must be a single character.");
	next;
     }
     last;
}

$bot{'LOGIN'} = $ENV{'USER'};

p("Ircname: Display this in the \"Real Name\" field");
$bot{'IRCNAME'} = getline("Enter an ircname: ");

$bot{'MODES'} = "+i-ws";

if (yesno(0, "Would you like to use a vhost: ")) {
     $bot{'VIRTUAL'} = getline("Enter a hostname or IP: ");
}

if ($doseen) {
     p("Turning off SEEN can save some memory.");
     if (yesno(0, "Disable SEEN")) {
	$bot{'NOSEEN'} = 1;
     }
}

p("We want the bot to require command char.",
  "Its possible to have to bot working without,",
  "but that could cause some confusion...");
if (yesno(1, "Use command character")) { $bot{'TOG CC'} = 1; }
else { $bot{'TOG CC'} = 0; }

if (yesno(1, "Ignore CTCP's from non-users")) { $bot{'TOG CLOAK'} = 1; }
else { $bot{'TOG CLOAK'} = 0; }

if (yesno(1, "Tell who is executing what in the partyline")) {
    $bot{'TOG SPY'} = 1;
}
else { $bot{'TOG SPY'} = 0; }

print("\n\n-=- Channel Configuration -=-\n\n");
p("We need a starting channel. Please enter a default channel",
  "DO NOT USE #emech PLEASE!");
$bot{'CHANNEL'} = getline("Enter a channel: ");

print("\n\n-=- IRC Configuration\n\n");

$bot{'SET OPMODES'} = 6;
$bot{'SET BANMODES'} = 6;      
$network = picklist(1, "What network will this bot be running on?\n",
		"Undernet",
		"EfNet",
		"DALnet",
		"Other");

if ($network == 1) {
     &load_servers("UNDERNET");
}
if ($network == 2) {
     $bot{'SET OPMODES'} = 4;
     $bot{'SET BANMODES'} = 4;
     &load_servers("EFNET");
}
if ($network == 3) {
     &load_servers("DALNET");
     &new_maxnicklen(32);
}

if ($network == 4) {
     $bot{'SET OPMODES'} = 4;	# Lets be safe
     $bot{'SET BANMODES'} = 4;
     print("Enter servers, one per-line below. type END when you ",
	"are finished\n");
     print("Example: irc.openprojects.net 6667\n\n");

     $x = 0;
     while (<STDIN>) {
	last if (/^END/ || /^end/);
	chomp($servers[$x++] = $_);
     }
}

&write_set;
print("All Done\n");

sub load_servers {
     my $x = 0;
     print("$net\n");
     open(SLIST, "servers/@_") or die ("Could not read serverlist: $!\n");
     while (<SLIST>) {
	chomp;
	$servers[$x++] = $_;
     }
     close(SLIST);
}

sub load_config_h {
     open(CONF, "../../src/config.h") 
	or die("You have not compiled yet. Please read the README file\n");

     while (<CONF>) {
	$dolink = 1 if (/ine LINKING/);
	$doseen = 1 if (/ine SEEN\n/);
	if (/MAXNICKLEN(.+)([\d+])/) {
		$maxnicklen = $2;
	}
     }
     close(CONF);
}

sub new_maxnicklen {
     print("Please stand by. Changing value of MAXNICKLEN and recompiling..\n");

     open(CONF_IN, "../../src/config.h")
	or die ("Could not open ../src/config.h for read: $!\n");
     open(CONF_OUT, ">../../src/config.h.new") 
	or die ("Could not open ../../src/config.h.new for write: $!\n");

     while (<CONF_IN>) {
	if (/ine MAXNICKLEN(.+)([\d+])/) {
	     print CONF_OUT ("\#define MAXNICKLEN @_\n");
	     next;
	}
	print CONF_OUT ("$_");
     }
     close(CONF_IN);
     close(CONF_OUT);
     system("mv -f ../../src/config.h.new ../src/config.h");
     system("cd ../..; make clean all install; cd contrib/config;");
}

sub write_set {
    # This is messy. I'm tired.
     print("Writing mech.set..\n");

     if (-f "../../mech.set") {
	print("Backing up existing set to mech.set.old\n");
	system("mv ../../mech.set ../../mech.set.old");
     }

     if (-f "../../mech.session") {
	print("Backing up mech.session to mech.session.old\n");
	system("mv ../../mech.session ../../mech.session.old");
     }

     open(SET, ">../../mech.set") or die("Couldn't write mech.set $!\n");

     @order = ("ENTITY", "LINKPASS", "LINKPORT", "AUTOLINK", "NICK", 
		"VIRTUAL", "LOGIN", "IRCNAME", "USERFILE", "MODES", 
		"CMDCHAR", "TOG SPY", "TOG CC", "TOG CLOAK", "SET OPMODES", 
		"SET BANMODES", "NOSEEN", "CHANNEL");

     foreach $key (@order) {

	if ($key eq "AUTOLINK") {
	     print SET ("AUTOLINK\n") if ($bot{'AUTOLINK'} == 1);
	     next;
	}

	if ($key eq "NOSEEN") {
	      print SET ("NOSEEN\n") if ($bot{'NOSEEN'} == 1);
	      next;
	}
	next if ($key eq "VIRTUAL" && !$bot{'VIRTUAL'});

	print SET ("$key $bot{$key}\n");
     }

     foreach $server (@servers) {
	print SET ("SERVER $server\n");
     }

}
