#!/usr/bin/perl
#!/usr/bin/perl
##########################################################
## Castle Links						
## Created: 06/17/1999			
##########################################################
# By: Castle CGI										
# WebSite: www.castellum.net				
##########################################################
##########################################################
# (C)Copyright 1999-2001 Castellum.net, All rights reserved	
##########################################################
# DISCLAIMER:						
# THIS PROGRAM IS PROVIDED WITHOUT WARRANTIES OF ANY    
# KIND, WHETHER EXPRESSED OR IMPLIED.   THIS PROGRAM IS 
# PROVIDED WIThOUT WARRANTIES AS TO PERFORMANCE, OR  	
# MERCHANTABILITY OF THIS PROGRAM.			
# TERMS OF USE:						
# THIS SCRIPT MAY BE MODIFIED, BUT NOT REDISTRIBUTED IN	
# ANY WAY, SHAPE, OR FORM.  IN ANY CASE, COPYRIGHT AND  
# SCRIPT INFORMATION MUST BE KEPT IN PLACE		
##########################################################
use Fcntl qw(:DEFAULT :flock); 


##################################
## LOAD SETUP INTO LOCAL %setup ##
##################################
my %setup = ();
open(SETUP, "clinks_setup.pl")||&TrueDie("Opening clinks_setup.pl: $!");
while(<SETUP>){
	chomp;
	my($name,$value) = split(/=/);
	$setup{$name} .= $value;
}
close(SETUP);
my $t_dir = $setup{'template_dir'};


###################################
## LOAD CATEGORIES
###################################
my %sort = ();
my $category_options = '';

open(CATS, "$setup{'category_file'}")||&Error("Opening $setup{'category_file'}: $!");
if ($setup{'flock'} == 1){flock(CATS,LOCK_SH);}
while(<CATS>){
	chomp;
	my($title,$count,$path,$addto,$desc) = split(/\|\|/);
	$sort{$path} = $_;
	push(@sort,$path);
}
if ($setup{'flock'} == 1){flock(CATS,8);}
close(CATS);

foreach my $category (sort(keys %sort)){
	$category_options .= "<OPTION value=\"$category\">$category</OPTION>";
}

###################################
## LOAD TEMPLATE
###################################


open(MASTER, "$t_dir/master.htm")||&DieNice("Openeing: $t_dir/master.htm");
my($temp1,$temp2) = split(/!INSERT!/,join('',<MASTER>));
close(MASTER);

open(TEMPLATE, "$t_dir/main.htm")||&DieNice("Opening: $file -- $!");
my $html = $temp1 . join('',<TEMPLATE>) . $temp2;
close(TEMPLATE);
$temp1 = undef;
$temp2 = undef;

my %vars = (
					'version'	=>		'4.51',
					'add_url'	=>		'add.cgi',
					'search_url'=>		'search.cgi',
					'new_url'	=>		'whatsnew.cgi',
					'old_url'	=>		'whatsold.cgi',
					'random_url'=>		'random.cgi'
);

$html =~ s/\$([vasnor]\w+)/$vars{$1}/g;

$html =~ s/<!--CATEGORY OPTIONS-->/$category_options/gi;
my($top,$category1,$middle,$category2,$bottom) = split(/<!--CATEGORY-->/,$html);
$html = undef;

##############################
## PRINT HEADER
##############################
print "Content-type: text/html\n\n";
print '<!--';
print 'ClBvd2VyZWQgYnk6IENhc3RsZSBMaW5rcwooYylDb3B5cmlnaHQgMTk5OS0yMDAwIENhc3RlbGx1';
print 'bS5uZXQsIEFsbCBSaWdodHMgUmVzZXJ2ZWQKU2NyaXB0IEF2YWlsYWJsZSBBdCBodHRwOi8vd3d3';
print 'LmNhc3RlbGx1bS5uZXQK';
print '-->';

##############################
## PRINT CATEGORIES
##############################
print $top;

my $i = 1;
foreach my $category (sort(keys %sort)){
	my %vars = ();
	my($title,$count,$path,$addto,$desc) = split(/\|\|/,$sort{$category});
	if ($category =~ /^\/[\w\!\@\#\$\%\^\&\*\(\)\_\+\-\[\]\;\,\.\<\> ]+$/){
		my $sub = undef;
		if ($i == 1){$sub = "$category1$middle"; $i = 2;}else{$sub = "$category2"; $i = 1;}
		my $url = "category.cgi?category=$category\&start=0";
		$url  =~ s/([\%\{\}])/uc sprintf("%%%02x",ord($1))/eg;
		$url =~ s/ /+/gi;
		$sub =~ s/\$description/$desc/gi;
		$sub =~ s/\$link_count/$count/gi;
		$sub =~ s/\$title/$title/gi;
		$sub =~ s/\$path/$category/gi;
		$sub =~ s/\$url/$url/gi;
		print $sub;
	}
}

print $bottom;


##############################
## DIE SUB-ROUTINE
##############################

sub DieNice {
	my $error = shift;
	print "Content-type: text/html\n\n";
	print qq~
<html>
<head>
<title>Castle Links v$version</title>
</head>
<body bgcolor="#E9E9E9" link="#800000">
<h2 align="left">Fatal Error: $error</h2>
<p align="left">The script encountered an error while trying to complete your request.  If
this is an error on your part, please press the back button and correct it.</p>
<hr>
<h6 align="center">Powered By: <a href="http://www.castellum.net/cgi/clinks/">Castle
Links</a><br>
©1999-2001 <a href="http://www.castle-cgi.com">Castle CGI</a>, All Rights
Reserved<br>
Script available at <a href="http://www.castle-cgi.com">http://www.castle-cgi.com</a></h6>
<p align="center">&nbsp;</p>
</body>
</html>
~;
exit;

}






1;

