#!/usr/bin/perl
# install-webserver
# A script to install Apache, PHP and MySQL.
# Needs to be run as root.
# Copyright Donncha O Caoimh <donncha@linux.ie>
# http://members.xoom.xom/xeer/
# Released under the GPL.


if ($< != 0) {
	print "You must run this script as root.\nPlease become root and run this script again.\n";
	die ("Stopped");
}

print "This script will install Apache, PHP, and MySQL on your machine.
This script has been tested on Linux, RedHat 6.0 and it should work on any 
modern installation of Linux. Your millage may vary with other versions of
Unix.\n";
print "Make sure you have the tar.gz files for Apache, PHP and MySQL in this directory.\n";
print "Press RETURN to continue";
$resp=<STDIN>;



system ("tar zxvf mysql*.tar.gz") == 0 or die ("\nError uncompressing MySQL");
system ("tar zxvf apache*.tar.gz") == 0 or die ("\nError uncompressing Apache");
system ("tar zxvf php-*.tar.gz") == 0 or die ("\nError uncompressing PHP");

opendir(DIR, ".");
@directory=readdir(DIR);
@mysql = grep(/mysql/, @directory);
@apache = grep(/apache_/, @directory);
@php = grep(/php-[34]/, @directory);
closedir(DIR);
foreach (@mysql) {
	$mysql_dir=$_ unless !-d;
}
foreach (@apache) {
	$apache_dir=$_ unless !-d;
}
foreach (@php) {
	$php_dir=$_ unless !-d;
}
if (length($mysql_dir) < 4) {
	print "MySQL directory not found!\nAre you sure you put the MySQL tar.gz archive into this directory?\n";
	print "The tar command may have failed. Check the output above.\n";
	die ("MysQL not found");
}

if (length($apache_dir) < 4) {
	print "Apache directory not found!\nAre you sure you put the Apache tar.gz archive into this directory?\n";
	print "The tar command may have failed. Check the output above.\n";
	die ("Apache not found");
}

if (length($php_dir) < 4) {
	print "PHP directory not found!\nAre you sure you put the PHP tar.gz archive into this directory?\n";
	print "The tar command may have failed. Check the output above.\n";
	die ("PHP not found");
}

print "cd $mysql_dir; ./configure; make; make install-strip; ./scripts/mysql_install_db; cp support-files/mysql.server /etc/rc.d/init.d/mysql; chmod a+x /etc/rc.d/init.d/mysql\n";
system("cd $mysql_dir; ./configure; make; make install-strip; ./scripts/mysql_install_db; cp support-files/mysql.server /etc/rc.d/init.d/mysql; chmod a+x /etc/rc.d/init.d/mysql");

print "cd $apache_dir ;./configure --prefix=/usr/local/apache\n";
system("cd $apache_dir ;./configure --prefix=/usr/local/apache");
if (-e "$php_dir/do-conf") {
	print "cd $php_dir; ./do-conf; make; make install; cp php3.ini-dist /usr/local/lib/php3.ini\n";
	system("cd $php_dir; sh ./do-conf; make; make install; cp php3.ini-dist /usr/local/lib/php3.ini");
} else {
	print "cd $php_dir; sh ./setup; make; make install; cp php3.ini-dist /usr/local/lib/php3.ini\n";
	system("cd $php_dir; sh ./setup; make; make install; cp php3.ini-dist /usr/local/lib/php3.ini");
}

print "cd $apache_dir; ./configure --prefix=/usr/local/apache --activate-module=src/modules/php3/libphp3.a; make; make install\n";
system("cd $apache_dir; ./configure --prefix=/usr/local/apache --activate-module=src/modules/php3/libphp3.a; make; make install");

print "now start MySQL and Apache by typing:\n";
print "/etc/rc.d/init.d/mysql start\n";
print "/usr/local/apache/bin/apachectl start\n";
print "These commands should be put into your startup files somewhere.\n";
print "Any comments and suggestions should be sent to donncha\@linux.ie\n";
print "Check http://members.xoom.com/xeer/ for new versions\n\n";
