#!/usr/bin/env perl

# install - Installation wizard.
# Copyright (C) 2010-2011 Xelhua Development Group, et al.
# 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/lib"); }
use Lib::Install;

# Installation script.
our $VERSION = 1.00;
our $ERROR = 0;

# Iterate through the arguments passed to us.
my $features = 'base ssl sqlite';
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 ' --disable-ssl - Disable support for SSL.';
    		println '*** End of Help ***';
    		exit 1;
    	}
    	elsif ($_ eq '--enable-sasl') {
    		$features .= ' sasl';
    	}
        elsif ($_ eq '--disable-ssl') {
            $features =~ s/ ssl//g;
        }
        elsif ($_ eq '--enable-ipv6') {
            $features .= ' ipv6';
        }
        elsif ($_ eq '--with-mysql') {
            $features =~ s/(sqlite|pgsql)/mysql/g;
        }
    	elsif ($_ eq '--with-pgsql') {
            $features =~ s/(sqlite|mysql)/pgsql/g;
        }
        else {
    		println "Warning: Unknown option '$_'";
    	}
    }
}

# Check Perl version.
println "Checking Perl version..... $^V";
eval {
    require 5.010_000;
} or println "This version of Perl is too old. Upgrade to Perl 5.10.0 or later and try again." and exit;

# 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 for Perl core modules.
println "Checking for core Perl modules.....";
checkcore();
println "\0";
if ($ERROR) {
    println "This Perl install is missing some core modules. Consider reinstalling Perl.";
    exit;
}
else {
    println "All good.";
}

# Check for required CPAN modules.
println "\0";
println 'Checking for required CPAN modules......';
println "\0";
#modfind("Mouse");
modfind('DBI');
modfind('DBD::SQLite') if $features =~ /sqlite/;
modfind('DBD::mysql') if $features =~ /mysql/;
modfind('DBD::Pg') if $features =~ /pgsql/;
modfind('Class::Unload');
modfind('IO::Socket::INET6') if $features =~ /ipv6/;
modfind('IO::Socket::SSL') if $features =~ /ssl/;
modfind('MIME::Base64') if $features =~ /sasl/;
println "\0";

if ($ERROR) {
    println "Failed to install. Please install the missing CPAN modules and try again.";
    exit;
}
else {
    println "All good.";
}

# 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);
println 'Done.';

println q{};
installmods();
println q{};

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

# vim: set ai sw=4 ts=4:
