#!/usr/bin/perl -w
use strict;
use Cwd;
use File::Copy;

show_introduction();

while(1) {

	my ($response) = 
	promptyn("Would you like to continue?");

	if (!$response) {

		print "\nInstallation script terminated!\n\n";

		exit;
	}

	last;
}

my (%DICTIONARY_CFG) = ();

$DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} = 
prompt_installation_directory();

my ($SLASH) = "/";

$SLASH = "\\"
if ($DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} =~ /\\/);

#These are all the required directories and 
#files associated with this project.
#They are followed by default permissions.
#Change these permissions to best suite your needs 
#Add your own files and directories if applicable
#Changing the paths to these files is likely cause
#the project to fail
my (%manifest) = (
"backups" => 0703,
"docs" => 0700,
"mysql" => 0703,
"php" => 0701,
"utilities" => 0700,
"web" => 0700,
"mysql${SLASH}administration.sql" => 0606,
"mysql${SLASH}administrators.sql" => 0606,
"mysql${SLASH}alphabet.sql" => 0606,
"mysql${SLASH}create.sql" => 0604,
"mysql${SLASH}destroy.sql" => 0604,
"mysql${SLASH}dictionary.sql" => 0606,
"mysql${SLASH}hits.sql" => 0606,
"mysql${SLASH}misses.sql" => 0606,
"mysql${SLASH}parts_of_speech.sql" => 0606,
"mysql${SLASH}pronunciation.sql" => 0606,
"mysql${SLASH}requests.sql" => 0606,
"mysql${SLASH}searches.sql" => 0606,
"mysql${SLASH}tasks.sql" => 0606,
"php${SLASH}Dictionary.php" => 0604,
"php${SLASH}Indonesia.php" => 0604,
"php${SLASH}Statistics.php" => 0604,
"php${SLASH}Web.php" => 0604,
"utilities${SLASH}config_app.pl" => 0700,
"utilities${SLASH}config_db.pl" => 0700,
"web${SLASH}admin.php" => 0604, 
"web${SLASH}admins.php" => 0604, 
"web${SLASH}backup.php" => 0604, 
"web${SLASH}cpwd.php" => 0604, 
"web${SLASH}delete.php" => 0604, 
"web${SLASH}dictionary.php" => 0604, 
"web${SLASH}download.php" => 0604, 
"web${SLASH}email.php" => 0604, 
"web${SLASH}insert.php" => 0604, 
"web${SLASH}login.php" => 0604, 
"web${SLASH}logout.php" => 0604, 
"web${SLASH}misses.php" => 0604, 
"web${SLASH}offline.php" => 0604, 
"web${SLASH}online.php" => 0604, 
"web${SLASH}pos.php" => 0604, 
"web${SLASH}reference.php" => 0604, 
"web${SLASH}request.php" => 0604, 
"web${SLASH}requests.php" => 0604, 
"web${SLASH}search.php" => 0604, 
"web${SLASH}sql.php" => 0604, 
"web${SLASH}statistics.php" => 0604,
"web${SLASH}update.php" => 0604,
"web${SLASH}upload.php" => 0604
);

my (%class_files) = (

"php${SLASH}Indonesia.php" =>
"php${SLASH}Dictionary.php",

"php${SLASH}Statistics.php" =>
"php${SLASH}Dictionary.php");

my (%web_files) = (

"web${SLASH}admin.php" => 
"php${SLASH}Indonesia.php",

"web${SLASH}admins.php" => 
"php${SLASH}Indonesia.php",

"web${SLASH}backup.php" => 
"php${SLASH}Indonesia.php",

"web${SLASH}cpwd.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}delete.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}download.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}dictionary.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}email.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}insert.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}login.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}logout.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}misses.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}offline.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}online.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}pos.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}reference.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}request.php" =>  
"php${SLASH}Indonesia.php",

"web${SLASH}requests.php" =>  
"php${SLASH}Indonesia.php",

"web${SLASH}search.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}sql.php" =>
"php${SLASH}Indonesia.php",

"web${SLASH}statistics.php" =>  
"php${SLASH}Statistics.php",

"web${SLASH}tasks.php" =>  
"php${SLASH}Indonesia.php",

"web${SLASH}update.php" =>  
"php${SLASH}Indonesia.php",

"web${SLASH}upload.php" =>  
"php${SLASH}Indonesia.php");

my ($dictionary_class) = "php${SLASH}Dictionary.php";

my ($web_class) = "php${SLASH}Web.php";

my (%modified_manifest) = ();

print "Verifying setup directories and files...\n\n";

#Now verify that all directories and files exist
#We will change the permissions only after the 
#configuration is finished.
foreach(sort(keys(%manifest))) {

	my ($path) = $_;

	my ($permissions) = $manifest{$path};

	$path = 
	$DICTIONARY_CFG{INSTALLATION_DIRECTORY} . $SLASH . $path; 

	$modified_manifest{$path} = $permissions;

	print "${path} ";

	show_error("\nCan't find ${path}", -1) if (!-e "$path");

	print "[OK]\n";
}

$DICTIONARY_CFG{"DB_HOST"} = 
prompt("What is or will be the name or IP address of the database server?", 1);

$DICTIONARY_CFG{"DB_NAME"} = 
prompt("What is or will be the database name for the dictionary database?", 1);

$DICTIONARY_CFG{"DB_USER"} = 
prompt("What is or will be the database user's name?", 1);

$DICTIONARY_CFG{"DB_PASSWORD"} = 
prompt("What is or will be $DICTIONARY_CFG{DB_USER}'s password?", 1);

while(1) {

	$DICTIONARY_CFG{"EMAIL"} = 
	prompt("What is or will be the website's primary e-mail address?", 1);

	last if (is_email($DICTIONARY_CFG{"EMAIL"}));

	show_error(
	"This doesn't appear to be a valid e-mail address.  Try again.", 0);
}

my (%modified_web_files) = ();

#Qualify the default directories by prepending the installation 
#directories then check to ensure the files exist then assign 
#then to hash that indicates which class file to use.  These files
#pertain to the PHP web code.
foreach(sort(keys(%web_files))) {

	my ($web_file) = 
	$DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
	$SLASH . ${_};

	my ($class_file) = 
	$DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
	$SLASH . $web_files{$_};

	#Check here if exists...abort if doesn't
	show_error(
	"Can't find PHP web file ${web_file}\n" .
	"Please make sure you installation path is correct.", -1)
		if (!-f "$web_file");

	#Check here if exists...abort if doesn't
	show_error(
	"Can't find PHP class file ${class_file}\n" .
	"Please make sure you installation path is correct.", -1)
		if (!-f "$class_file");

	$modified_web_files{"$web_file"} = "$class_file";
}

my (%modified_class_files) = ();

#Do the same for child class files 
foreach(sort(keys(%class_files))) {

	my ($child_class) = 
	$DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
	$SLASH . ${_};

	my ($parent_class) = 
	$DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
	$SLASH . $class_files{$_};

	#Check here if exists...abort if doesn't
	show_error(
	"Can't find PHP class file ${child_class}\n" .
	"Please make sure you installation path is correct.", -1)
		if (!-f "$child_class");

	#Check here if exists...abort if doesn't
	show_error(
	"Can't find PHP parent class file ${parent_class}\n" .
	"Please make sure you installation path is correct.", -1)
		if (!-f "$parent_class");

	$modified_class_files{"$child_class"} = "$parent_class";
}

#Dictionary class is the parent class of everything 
$dictionary_class = $DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
$SLASH . $dictionary_class;

#Check here if exists...abort if doesn't
show_error(
"Can't find PHP parent dictionary class file ${dictionary_class}\n" .
"Please make sure you installation path is correct.", -1)
	if (!-f "$dictionary_class");

#Web class to be inserted into web PHP files
$web_class = $DICTIONARY_CFG{"INSTALLATION_DIRECTORY"} . 
$SLASH . $web_class;

#Check here if exists...abort if doesn't
show_error(
"Can't find PHP web class file ${web_class}\n" .
"Please make sure you installation path is correct.", -1)
	if (!-f "$web_class");

#Verify that configuration values given are correct before 
#proceeding 
while(1) {

	print "\n[VERIFY THESE VALUES]\n\n";

	foreach(sort(keys(%DICTIONARY_CFG))) {

		my ($name) = $_;

		my ($value) = $DICTIONARY_CFG{$name};

		print "${name} = ${value}\n";
	}

	my ($response) = 
	promptyn("Are these values correct?");

	if (!$response) {

		print "\nInstallation script terminated!\n\n";

		exit;
	}

	last;
}

print "Configuring Dictionary.pm class file...\n";

#Configure the Dictionary.php class with given values
configure_dictionary_class("$dictionary_class", \%DICTIONARY_CFG, $SLASH);

print "Configuring other class file...\n";

#Modify the web files' require_once() statements to include full path
#to source class files
foreach(sort(keys(%modified_class_files))) {

	my ($child_class) = $_;

	my ($parent_class) = $modified_class_files{$child_class};

	configure_php_file($child_class, $parent_class, $SLASH, "");
}

print "Configuring PHP web files...\n";

#Modify the web files' require_once() statements to include full path
#to source class files
foreach(sort(keys(%modified_web_files))) {

	my ($web_file) = $_;

	my ($class_file) = $modified_web_files{$web_file};

	configure_php_file($web_file, $class_file, $SLASH, $web_class);
}

print "Setting default permissions to directories and files...\n";

change_permissions(\%modified_manifest);

#Prompt if user wants to install web files to web server
while(1) {

	my ($response) = 
	promptyn(
	"Would you like to install the PHP web files to your web server?");
	 
	if (!$response) {

		print "\nInstallation script completed!\n\n";

		exit;
	}

	last;
}

my ($web_directory) = "";

#Prompt for web server installation directory
while(1) {

	$web_directory = 
	prompt(
	"What is the name of the web directory to copy the " .
	"PHP web files?", 1); 

	#Remove trailing slash for UNIX systems if exists
	$web_directory =~ s/\/$//;

	#Remove trailing slash for Windows systems if exists
	$web_directory =~ s/\\$//;

	if (!-d "$web_directory") {

		show_error(	
		"Directory ${web_directory} does not exist.\n"  .
		"Manually create this directory or use an existing one\n" .
		"before continuing.  I'll wait.", 0);

		my ($response) =
		promptyn("Try again?", 1);  

		if (!$response) {

			print 
			"\nInstallation script done.\n" .
			"Manually copy web PHP files and sub-directories " .
			"when you are ready.\n\n"; 

			exit;
		} 

	} else {

		last;
	}
}

print "Copying PHP web files...\n";

foreach(sort(keys(%modified_web_files))) {

	my ($src) = $_;
	my ($tgt) = $web_directory . $SLASH . get_basename($src); 

	copy("$src", "$tgt");

	chmod(0604, "$tgt");

	show_error(
	"Can't copy\n${src} to\n${tgt}\n" .
	"Read documentation about manual web installation.", -1) 
		if (!-f "$tgt");
}

print 
"\nInstallation script completed!\n" .
"Once again double check permissions for directories and files.\n\n"; 
 
#----------------------------------------------------------------------------
#Changes installation directories and files to default permissions 
#----------------------------------------------------------------------------
sub change_permissions {

	my ($modified_manifest) = shift;

	foreach(sort(keys(%$modified_manifest))) {

		my ($path) = $_;

		my ($permissions) = $$modified_manifest{$path};

		chmod($permissions, $path);
	}

	return;
}
#----------------------------------------------------------------------------
#Configures the Dictionary.pm class files to run under this environment.
#----------------------------------------------------------------------------
sub configure_dictionary_class {

	my ($file) = shift;
	my ($cfg) = shift;
	my ($slash) = shift;
	my ($file_backup) = $file . "." . $$ . ".bak"; 
	my ($cfg_entry) = "";

	#Compose the dictionary configuration PHP entries
	#Also change permissions to directories if applicable
	foreach(sort(keys(%$cfg))) {

		my ($name) = $_;
		my ($value) = $$cfg{$_};

		$value =~ s/\\/\\\\/g
		if (($name eq "INSTALLATION_DIRECTORY") && ($slash eq "\\"));

		$cfg_entry .= 
		"\$this->configuration[\"${name}\"] = \"${value}\";\n\n"; 
	}

	#Clean it up
	chomp($cfg_entry);

	#Remove existing backup file if exists
	unlink("$file_backup") if (-f "$file_backup");

	#Make a backup copy just to be safe
	copy("$file", "$file_backup") || goto RECOVER_AND_ABORT_A;

	#Remove the original file
	unlink("$file");

	#Create a new PHP file 
	open(NEW_FILE, ">${file}") || goto RECOVER_AND_ABORT_B;

	#Open the original file (backup)
	open(OLD_FILE, "${file_backup}") || goto RECOVERY_AND_ABORT_C;

	my ($ignore_flag) = 0;

	while(<OLD_FILE>) {

		my ($tmp) = $_;

		if ($tmp =~ /\/\/\<CONFIGURATION\>/) {

			print NEW_FILE $tmp;

			print NEW_FILE $cfg_entry; 

			$ignore_flag = 1;

			next;

		} elsif (($tmp =~ /\/\/\<\/CONFIGURATION\>/) &&
		         ($ignore_flag)) { 

			print NEW_FILE $tmp;

			$ignore_flag = 0;

		} elsif (!$ignore_flag) {

			print NEW_FILE $tmp;
		}
	}

	close(NEW_FILE);

	close(OLD_FILE);

	#Double check to ensure new file exists
	goto  RECOVERY_AND_ABORT_D if (!-f "$file");

	#Remove the backup file
	unlink("$file_backup") if (-f "$file_backup");

	#Give proper permissions
	chmod(0604, "$file");

	return;

RECOVER_AND_ABORT_A:

	#Remove backup if exists
	unlink("$file_backup") if (-f "$file_backup");

	show_error(
	"Can't copy ${file} to ${file_backup}\n" .
	"Make sure you have proper permissions or enough space.",
	-1);

RECOVER_AND_ABORT_B:

	close(NEW_FILE);

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file"); 

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 

RECOVER_AND_ABORT_C:

	close(NEW_FILE);

	close(OLD_FILE);

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file");

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 

RECOVER_AND_ABORT_D:

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file");

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 
}
#----------------------------------------------------------------------------
#Configures PHP files to include required absolute class file paths
#----------------------------------------------------------------------------
sub configure_php_file {

	my ($file) = shift; 
	my ($class_file) = shift;
	my ($slash) = shift;
	my ($web_class) = shift || "";
	my ($file_backup) = $file . "." . $$ . ".bak"; 

	#Remove existing backup file if exists
	unlink("$file_backup") if (-f "$file_backup");

	#Make a backup copy just to be safe
	copy("$file", "$file_backup") || goto RECOVER_AND_ABORT_A;

	#Remove the original file
	unlink("$file");

	#Create a new PHP file 
	open(NEW_FILE, ">${file}") || goto RECOVER_AND_ABORT_B;

	#Open the original file (backup)
	open(OLD_FILE, "${file_backup}") || goto RECOVERY_AND_ABORT_C;

	$class_file =~ s/\\/\\\\/g if ($slash eq "\\");

	#New require_once() string
	my ($new_require) = 
	"require_once(\"${class_file}\");\n";

	#Also add web class if applicable
	if ($web_class ne "") {

		$web_class =~ s/\\/\\\\/g if ($slash eq "\\");

		$new_require .= 
		"require_once(\"${web_class}\");\n";
	}

	my ($ignore_old_require_flag) = 0;

	while(<OLD_FILE>) {

		my ($tmp) = $_;

		#Insert the new one
		if (
		($tmp =~ /require_once\(/) && 
		($tmp =~ /\)\;/) && 
		(!$ignore_old_require_flag)) {

			print NEW_FILE $new_require; 

			$ignore_old_require_flag = 1;

		#Ignore the old requires
		} elsif (
		  ($tmp =~ /require_once\(/) && 
		  ($tmp =~ /\)\;/))  {

			next;

		}  else {

			print NEW_FILE $tmp; 
		}
	}

	close(NEW_FILE);

	close(OLD_FILE);

	#Double check to ensure new file exists
	goto  RECOVERY_AND_ABORT_D if (!-f "$file");

	#Remove the backup file
	unlink("$file_backup") if (-f "$file_backup");

	chmod(0604, "$file");

	return;

RECOVER_AND_ABORT_A:

	#Remove backup if exists
	unlink("$file_backup") if (-f "$file_backup");

	show_error(
	"Can't copy ${file} to ${file_backup}\n" .
	"Make sure you have propery permissions or enough space.",
	-1);

RECOVER_AND_ABORT_B:

	close(NEW_FILE);

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file"); 

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 

RECOVER_AND_ABORT_C:

	close(NEW_FILE);

	close(OLD_FILE);

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file");

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 

RECOVER_AND_ABORT_D:

	#Remove the new file if it was partially created
	unlink("$file");

	#Move backup file back to its original spot
	move("$file_backup", "$file");

	if (!-f "$file") {

		show_error(
		"Could not configure and restore PHP file ${file}\n" .
		"Correct the error and reinstall before running " .
		"this script again.", -1); 
	}

	show_error(
	"Could not configure PHP file ${file}\n" .
	"Correct the error and try again.", -1); 
}
#----------------------------------------------------------------------------
#Returns the basename of a path
#----------------------------------------------------------------------------
sub get_basename {

	my ($path) = shift;
	my (@path_tree) = shift;

	if ($path =~ /\\/) {

		@path_tree = split(/\\/, $path);

		return($path_tree[$#path_tree]);

	} elsif ($path =~ /\//) {

		@path_tree = split(/\//, $path);

		return($path_tree[$#path_tree]);
	}

	return($path);
}
#----------------------------------------------------------------------------
#Verifies given token is an e-mail address.  This can be greatly enhanced.
#It just lloks for @ and . with spacing before, in between, and after for 
#the time being.  Should be good enough though.
#----------------------------------------------------------------------------
sub is_email {

	my ($token) = shift;

	return(1) if ($token =~ /\w+\@\w+\.\w+/);
}
#----------------------------------------------------------------------------
#Generic prompt 
#----------------------------------------------------------------------------
sub prompt {

	my ($prompt) = shift;
	my ($required_flag) = shift;
	my ($response) = "";

	#Iterate until correct response is found
	while(1) {

		#Show prompt message
		print "\n${prompt}\n\n";

		#Grab response
		$response = <STDIN>;

		#Remove leading and trailing white-spaces
		trim(\$response);
		
		if (($response) eq "" && ($required_flag)) {

			show_error("This parameter requires a value!", 0);
	
			next;
		} 

		last;
	}

	return($response);
}
#----------------------------------------------------------------------------
#Generic yes or no prompt 
#----------------------------------------------------------------------------
sub promptyn {

	my ($prompt) = shift;
	my ($response);
	my ($ucresponse);
	my (%responses) = (YES => 1, NO => 1, Y => 1, N => 1);

	#Iterate until correct response is found
	while(1) {

		#Show prompt message
		print "\n${prompt} [y|n]\n\n";

		#Grab response
		$response = <STDIN>;
		
		#Clean leading and trailing white spaces
		trim(\$response);

		if ($response eq "") {

			show_error(
			"Please respond with a 'y' or 'n' answer!", 0);

			next;

		}  else {

			$ucresponse = uc($response);

			if (exists($responses{$ucresponse})) {

				last;

			} else {

				show_error(
				"Please respond with a 'y' or 'n' answer!", 0);
			}
		}
	}

	return(1) if ($ucresponse =~ /^Y/);

	return(0);
}
#----------------------------------------------------------------------------
#Generic prompt with default
#----------------------------------------------------------------------------
sub promptx {

	my ($prompt) = shift;
	my ($default) = shift;
	my ($required_flag) = shift;
	my ($response) = "";

	#Iterate until correct response is found
	while(1) {

		print "\n${prompt} [${default}]\n\n";

		$response = <STDIN>;

		chomp($response);

		if ($response eq "") {

			$response = $default;

			last;

		} else {

			trim(\$response);

			if (($response) eq "" && ($required_flag)) {

				show_error(
				"This parameter requires a value!", 0);

				next;

			} else {


				last;
			}
		}
	}

	return("$response");
}
#----------------------------------------------------------------------------
#Prompt for installation directory
#----------------------------------------------------------------------------
sub prompt_installation_directory {

	my ($pwd) = cwd();
	my ($default_dir) = "";
	my ($installation_directory) = "";

	if ($pwd) {

		if (-d "$pwd") {

			show_error(
			"You must run this installation script " .
			"under the same directory that it resides.", -1)
				if ($pwd !~ /bahasa\/utilities$/);

			$default_dir = $pwd;

			$default_dir =~ s/\/utilities$//;
		} 
	}

	if (-d "$default_dir") {

		while(1) {

			$installation_directory =
			promptx(
			"Enter the installation directory:\n", 
			"$default_dir", 1);
	
			if (!-d "$installation_directory") {

				show_error(
				"'${installation_directory}'" .
				" does not exists.  Please try again.", 0);

				next;
			}

			last;
		}

	} else {


		while(1) {
	
			$installation_directory =
			prompt("Enter the installation directory: ", 1);

			if (!-d "$installation_directory") {

				show_error(
				"'${installation_directory}'" .
				" does not exists.  Please try again.", 0);

				next;
			}

			last;
		}
	}

	#Remove trailing slash for UNIX systems if exists
	$installation_directory =~ s/\/$//;

	#Remove trailing slash for Windows systems if exists
	$installation_directory =~ s/\\$//;

	return("$installation_directory");
}
#----------------------------------------------------------------------------
#Display error message then terminate script
#----------------------------------------------------------------------------
sub show_error {

	my ($msg) = shift;
	my ($exit_flag) = shift || 0;

	print "\nInstallation Error:\n${msg}\n\n";

	exit(-1) if ($exit_flag);

	return;
}
#----------------------------------------------------------------------------
#Introduction message
#----------------------------------------------------------------------------
sub show_introduction {

	print "\n\n";

	print "-" x 77;

	print "\n";

	print <<EOF;
[BAHASA INDONESIA DICTIONARY CONFIGURATION]

The purpose of this script is to configure 
the Bahasa Indonesia Dictionary PHP files to
run on this host.  It doesn't do any 
mySQL database configuration, but it will 
prompt you for database questions.  

This script assumes that your web server is
running on this host.  You are responsible 
for setting the correct permissions for 
these files after configuration.

I've tried to make this script as portable 
as possible.  If for some reason this script 
fails then you will have to configure the files
manually.  See documentation for more details. 

Please feel free to modify this script to best 
fit your needs.

You can abort at any time by pressing CTRL C.

EOF

	print "-" x 77;

	print "\n";

	return;
}
#----------------------------------------------------------------------------
#Trim leading and trailing white-spaces from string token.  Pass token by
#reference only.
#----------------------------------------------------------------------------
sub trim {

	my ($token) = shift;

	return if (!$token);

	$$token =~ s/^\s+//;
	$$token =~ s/\s+$//;

	return;
}
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------

