#!/usr/bin/perl
#
#  @Pel.Net OpenClient Installer 
#
# Copyright (C) 2008  PelliX
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/
#
#
my $platform = $ARGV[0];
my $xmlenable = $ARGV[1];

# if no argument, then error.
if ((length($platform)) < 1)
	{
	&error;
	}
	else
	{
	# check syntax
	if ($platform eq "windows")
		{
		&install_windows;
		}
		elsif ($platform eq "unix")
		{
		&install_unix;
		}
		elsif ($platform eq "os2")
		{
		&install_os2;
		}
		elsif ($platform eq "generic")
		{
		&install_generic;
		}
		else
		{
		&error;
		}
	}

sleep 1;
exit();

# Subroutines
# incorrect syntax
sub error
	{
	print "\n\n  Error!\n\n".
		" Your syntax was invalid, or your shell didn't pass it to us correctly";
	print "\n\n Please make sure that you enter one of the following install types:\n";
	print " windows, unix, os2 or generic (minimal)\n\n";
	sleep 1;
	print " usage: perl ./install.pl [windows]|[unix]|[os2]|[generic]\n";
	print " ------------------------------ \n";
	print " You can also enable installing the XML dependancies on Win32 by adding\n";
	print " \"--with-xml\" to the syntax.\n";
	return;
	}

# install for Windows
sub install_windows
	{
	$osver = `ver`;
	chomp $osver;
	print "running on ".$osver;
	print "\n.......\n";
	sleep 1;
	print "\nloading installer......\n";
	sleep 1;
	print "\n--------------------------------------------------\n\n".
			" You have selected the installation for Windows.\n\n".
			" The following packages will be installed:\n\n".
			" Tk, Data::Dumper, LWP::Simple, LWP::UserAgent, \n".
			" SOAP::Lite, Authen::NTLM and Authen::NTLM::HTTP.\n\n".
			" Please select 1 for the ActiveState ppm installer, \n".
			" or 2 for the traditional Perl CPAN method [1] \n";
	my $choice = <STDIN>;
	chomp $choice;
	if ($choice eq "1")
		{
		&ppm_install;
		}
		elsif ($choice eq "2")
		{
		&cpan_install_win;
		}
		else
		{
		&error;
		}
	if($xmlenable eq "--with-xml")
			{
			&ppm_install_xml;
			}
	&end_install;
	}

# UNIX installation
sub install_unix
	{
	$osver = `uname -a`;
	chomp $osver;
	print "running on ".$osver;
	print "\n.......\n";
	sleep 1;
	print "\nloading installer......\n";
	sleep 1;
	print "\n--------------------------------------------------\n\n".
			" You have selected the installation for Unix.\n\n".
			" The following packages will be installed:\n\n".
			" Tk, Data::Dumper, LWP::Simple, LWP::UserAgent, \n".
			" SOAP::Lite, Authen::NTLM and Authen::NTLM::HTTP.\n\n".
			" Please select 1 create a local user config location \n".
			" and prog dir in /etc/ or 2 to skip this step. [1] \n";
	my $choice = <STDIN>;
	chomp $choice;
	&cpan_install;
	if ($choice eq "1")
		{
		`mkdir $HOME/.opensearch && chmod 755 $HOME/.opensearch`;
		`mkdir /etc/opensearch && chmod 775 /etc/opensearch`;
		if (-e "$HOME/.opensearch")
			{
			print "Personal config dir has been created.\n";
			}
			else
			{
			print "Could not set up the personal config dir.\n";
			}
		if (-e "/etc/opensearch")
			{
			print "Created /etc/opensearch. \n";
			`cp -vf ./service.xml /etc/opensearch/`;
			`cp -vf ./README.unix /etc/opensearch/`;
			`cp -vf ./README.txt /etc/opensearch/`;
			`cp -vf ./bugs.readme /etc/opensearch/`;
			`cp -vf ./Changelog.txt /etc/opensearch/`;
			`cp -vf ./GPL.txt /etc/opensearch/`;
			`cp -vf ./LGPL.txt /etc/opensearch/`;
			}
			else
			{
			print "Setting up config dir in /etc/ failed.\n";
			}
		}
		elsif ($choice eq "2")
		{
		print "No personal config dir created";
		}
		else
		{
		&error;
		}
	&end_install;
	}

# OS/2 Warp installation (needs dome work)
sub install_os2
	{
	$osver = `ver`;
	chomp $osver;
	print "running on ".$osver;
	print "\n.......\n";
	sleep 1;
	print "\nloading installer......\n";
	sleep 1;
	print "\n--------------------------------------------------\n\n".
			" You have selected the installation for OS/2 Warp.\n\n".
			" The following packages will be installed:\n\n".
			" Tk, Data::Dumper, LWP::Simple, LWP::UserAgent, \n".
			" SOAP::Lite, Authen::NTLM and Authen::NTLM::HTTP.\n\n".
			" Please select 1 for a minimal installation, or 2 \n".
			" for the installation into the Home directory [1] \n";
	my $choice = <STDIN>;
	chomp $choice;
	if ($choice eq "1")
		{
		&cpan_install;
		}
		elsif ($choice eq "2")
		{
		&cpan_install;
		&setup_os2;
		}
		else
		{
		&error;
		}
	&end_install;
	}

# generic CPAN installation, and offer to make a dir in /etc/
sub install_generic
	{
	$osver = `uname`;
	chomp $osver;
	if ((length($osver)) < 1)
		{
		$osver = "unknown";
		}
	print "running on ".$osver;
	print "\n.......\n";
	sleep 1;
	print "\nloading installer......\n";
	sleep 1;
	print "\n--------------------------------------------------\n\n".
			" You have selected the generic installation.\n\n".
			" The following packages will be installed:\n\n".
			" Tk, Data::Dumper, LWP::Simple, LWP::UserAgent, \n".
			" SOAP::Lite, Authen::NTLM and Authen::NTLM::HTTP.\n\n".
			" Please select 1 create a public config location in\n".
			" /etc/ or 2 to skip this step. [1] \n";
	my $choice = <STDIN>;
	chomp $choice;
	&cpan_install;
	if ($choice eq "1")
		{
		`mkdir /etc/opensearch && chmod 775 /etc/opensearch`;
		if (-e "/etc/opensearch")
			{
			print "Created directory in /etc/.\n";
			`cp -vf ./service.xml /etc/opensearch/`;
			`cp -vf ./README.unix /etc/opensearch/`;
			`cp -vf ./README.txt /etc/opensearch/`;
			`cp -vf ./bugs.readme /etc/opensearch/`;
			`cp -vf ./Changelog.txt /etc/opensearch/`;
			`cp -vf ./GPL.txt /etc/opensearch/`;
			`cp -vf ./LGPL.txt /etc/opensearch/`;
			}
			else
			{
			print "Could not install to /etc/opensearch .\n";
			}
		}
		elsif ($choice eq "2")
		{
		print "No config dir created";
		}
		else
		{
		&error;
		}
	&end_install;
	}

# CPAN traditional installation
sub cpan_install
	{
	print "Starting CPAN shell...\n";
	`perl -MCPAN -e 'install Tk'`;
	`perl -MCPAN -e 'install Data::Dumper'`;
	`perl -MCPAN -e 'install LWP::Simple'`;
	`perl -MCPAN -e 'install LWP::UserAgent'`;
	`perl -MCPAN -e 'install SOAP::Lite'`;
	`perl -MCPAN -e 'install Authen::NTLM'`;
	`perl -MCPAN -e 'install Authen::NTLM::HTTP'`;
	return;
	}

# CPAN install for Windows
sub cpan_install_win
	{
	print "Starting CPAN shell...\n";
	`perl -MCPAN -e "install Tk"`;
	`perl -MCPAN -e "install Data::Dumper"`;
	`perl -MCPAN -e "install LWP::Simple"`;
	`perl -MCPAN -e "install LWP::UserAgent"`;
	`perl -MCPAN -e "install SOAP::Lite"`;
	`perl -MCPAN -e "install Authen::NTLM"`;
	`perl -MCPAN -e "install Authen::NTLM::HTTP"`;
	return;
	}

# PPM install for Windows
sub ppm_install
	{
	print "Starting PPM shell...\n";
	print system "ppm install Tk";
	print system "ppm install Data::Dumper";
	print system "ppm install LWP::Simple";
	print system "ppm install LWP::UserAgent";
	print system "ppm install SOAP::Lite";
	print system "ppm install Authen::NTLM";
	print system "ppm install Authen::NTLM::HTTP";
	return;
	}

# PPM install of the XML dependancies
sub ppm_install_xml
	{
	print "Starting PPM to get XML utils...\n";
	print system "ppm install XML::Writer";
	print system "ppm install XML::Parser";
	print system "ppm install XML::XPath";
	return;
	}

# setup for OS/2 Warp
sub setup_os2
	{
	print system "mkdir $ENV{HOME}\\OpenSearch";
	if (-e $ENV{HOME}/OpenSearch)
		{
		print "Home config dir has been created.\n";
		print system "copy .\OpenSearch.pl $ENV{HOME}\\OpenSearch";
		print system "copy .\logo.gif $ENV{HOME}\\OpenSearch";
		print system "copy .\icon.gif $ENV{HOME}\\OpenSearch";
		print system "copy .\service.xml $ENV{HOME}\\OpenSearch";
		print system "copy .\README.txt $ENV{HOME}\\OpenSearch";
		print system "copy .\README.os2 $ENV{HOME}\\OpenSearch";
		print system "copy .\Changelog.txt $ENV{HOME}\\OpenSearch";
		print system "copy .\bugs.readme $ENV{HOME}\\OpenSearch";
		}
		else
		{
		print "Failed to set up home directory. \n";
		}
	return;
	}

# End routine
sub end_install
	{
	print "\n\n Thank you for installing the OpenSearch client\n".
			" and it's dependancies! You can now execute the script\n".
			" by running \"perl ./OpenSearch.pl\"!\n\n";
	sleep 1;
	print "Press Return to exit\n";
	my $choice = <STDIN>;
	if ((length($choice)) > 1)
		{
		exit();
		}
	}

