#!/usr/bin/env perl
# Auto IRC Bot. An advanced, lightweight and powerful IRC bot.
# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS)
# This program is free software; rights to this code are stated in doc/LICENSE.
package Install;
use strict;
use warnings;
use English qw(-no_match_vars);
use FindBin qw($Bin);
our $Bin = $Bin;
BEGIN { unshift(@INC, "$Bin/src"); }
use Lib::Install;

# Installation script.
our $VERSION = 1.00;

# Iterate through the arguments passed to us.
my $features = "base";
if (defined $ARGV[0]) {
	foreach (@ARGV) {
		if ($_ eq "-h" or $_ eq "--help") {
			println "*** ./install help ***";
			println " --enable-sasl - Enable support for SASL.";
            println " --enable-ipv6 - Enable support for IPv6.";
			println "*** End of Help ***";
			exit;
		}
		elsif ($_ eq "--enable-sasl") {
			$features .= " sasl";
		}
        elsif ($_ eq "--enable-ssl") {
            $features .= " ssl";
        }
        elsif ($_ eq "--enable-ipv6") {
            $features .= " ipv6";
        }
		else {
			println "Warning: Unknown option '".$_."'";
		}
	}
}

# Check operating system.
print "Checking operating system..... $OSNAME - ";
if ($OSNAME =~ /dos/i) {
	print "DOS is not supported.\r\n";
}
elsif ($OSNAME eq "MSWin32") {
	print "Microsoft Windows is not supported. Support is planned for the future.\r\n";
}
elsif ($OSNAME eq "NetWare") {
	print "NetWare is not supported.\r\n";
}
elsif ($OSNAME eq "linux") {
	print "OK\n";
}
elsif ($OSNAME eq "os2") {
	print "IBM OS/2 is not supported.\r\n";
}
elsif ($OSNAME =~ /mac/i or $OSNAME =~ /darwin/i) {
	print "OK\r";
}
elsif ($OSNAME eq "freebsd") {
	print "OK\n";
}
elsif ($OSNAME eq "openbsd") {
	print "OK\n";
}
else {
	print "Unknown operating system. Contact support.\r\n";
}		

# Check Perl version.
print "Checking Perl version..... $] - ";
if ($] >= 5.010000) {
	println "OK";
}
else {
	println "Incompatible";
	exit;
}

# Check for required CPAN modules.
println "\0";
println "--- CPAN Modules ---";
modfind("Mouse");
modfind("Class::Unload");
modfind("IO::Socket::INET6") if $features =~ /ipv6/;
modfind("IO::Socket::SSL") if $features =~ /ssl/;
modfind("MIME::Base64") if $features =~ /sasl/;

# Create build.
println "\0";
println "Building.....";
if (!-d "$Bin/build") {
	system("mkdir $Bin/build");
}
if (!-e "$Bin/build/time") {
	system("touch $Bin/build/time");
}
if (!-e "$Bin/build/os") {
	system("touch $Bin/build/os");
}
if (!-e "$Bin/build/perl") {
	system("touch $Bin/build/perl");
}
if (!-e "$Bin/build/ver") {
	system("touch $Bin/build/ver");
}

build($features);

# Success!
println "Done. Auto successfully installed.";
