#!/usr/bin/perl

##########################################################################
# COPYRIGHT NOTICE:
# 
# Copyright FocalMedia.Net
# 
# This program is free to use for commercial and non commercial use. 
# This script may be used and modified by anyone, so long as this copyright 
# notice and the header above remain intact. Selling the code for this 
# program without prior written consent is expressly forbidden.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied.
#
##########################################################################

use FindBin;
use lib $FindBin::Bin;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

### WINDOWS USERS EDIT BELOW ##############################################
$cfile = "config.cfg";
#$cfile = "e:/full/server/path/to/config.cfg";
###########################################################################

$q = CGI->new;

### GET SETUP

&get_setup;

$mainlayout = &get_file_contents("$data_dir/layout.html");
if ($mainlayout eq "") { print "Could not find $data_dir/main_layout.html or the file is empty"; exit;}


##############################

if ($q->param('fct') eq "") {&start;}

sub start
{

$mainlayout = &get_file_contents("$data_dir/layout.html");
$advanced = &get_file_contents("$data_dir/advanced.html");

$mainlayout =~ s/!!replacement_data!!/$advanced/g;

$mainlayout =~ s/!!web_url!!/$web_url/g;

if ($use_mysql eq "Yes")
	{
	$mainlayout =~ s/!!script_url!!\/fmsearch\.cgi/$script_url\/fmsearch2\.cgi/g;
	}

$mainlayout =~ s/!!script_url!!/$script_url/g;


print "Content-type: text/html\n\n";
print "$mainlayout";
}





sub get_file_contents
{

my ($filename) = @_;
my ($filesize, $filesize, $thefile);

$filesize = (-s "$filename");
open (TFILECNTS, "$filename");
	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);

return ($thefile);
}





#### GET CONFIGURATION ########################################################

sub get_setup
{

$csize = (-s "$cfile");
open (RVF, "$cfile");
	read(RVF,$data_dir,$csize);
close (RVF);

$estr = ""; $dta = "";
for ($ms = 0; $ms < length($data_dir); $ms++)
	{
	$ch = substr($data_dir, $ms, 1);
	$estr = ord($ch); $estr = $estr - 5;
	$dta = $dta . chr($estr);
	}
	$data_dir = $dta;
	
$data_dir =~ s/\n//g; $crit = chr(13); $crit =~ s/$crit//g; $crit = chr(10); $crit =~ s/$crit//g;	


$exists = (-e "$data_dir/setup.cfg");
if ($exists > 0)
	{
	
	open (STP, "$data_dir/setup.cfg");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	

if ($line =~ /^WEBROOT/){$webroot = &get_setup_line($line, WEBROOT);}
if ($line =~ /^URLROOT/){$urlroot = &get_setup_line($line, URLROOT);}
if ($line =~ /^WEB_URL/){$web_url = &get_setup_line($line, WEB_URL);}
if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line($line, SCRIPT_URL);}
if ($line =~ /^USERNAME/){$username = &get_setup_line($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line($line, PASSWORD);}

if ($line =~ /^USE_MYSQL/){$use_mysql = &get_setup_line($line, USE_MYSQL);}
if ($line =~ /^DB_NAME/){$db_name = &get_setup_line($line, DB_NAME);}
if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line($line, DB_USERNAME);}
if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line($line, DB_PASSWORD);}

			}
	close (STP);
	
	}
}

sub get_setup_line
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}

#### END CONFIGURATION ########################################################







