#!/usr/bin/perl

# GLOBALS (yes, it's a no-no, I know, but this is quick-and-dirty)
# ------------
# %categories
# %entry_fields
# $entry_dir
# *params

use Data::Dumper;
use Time::Local;
require "cgi-lib.pl";
{
    my $src = `lynx -source http://www.classic-castle.com/events/contest/cccontest6/contest.html`;
    if($src =~ /^(.*)\<\!--Enter Text Between here--\>(.*)\<\!--And here--\>(.*)$/s) {
	($header_text,$takeout,$footer_text) = ($1,$2,$3);

	$header_text =~ s/\.\.\/\.\.\/\.\.\//http:\/\/classic-castle.com\//gs;
	$header_text =~ s/\.\.\/\.\.\//http:\/\/classic-castle.com\/events\//gs;
	$header_text =~ s/\.\.\//http:\/\/classic-castle.com\/events\/contest\//gs;

	$footer_text =~ s/\.\.\/\.\.\/\.\.\//http:\/\/classic-castle.com\//gs;
	$footer_text =~ s/\.\.\/\.\.\//http:\/\/classic-castle.com\/events\//gs;
	$footer_text =~ s/\.\.\//http:\/\/classic-castle.com\/events\/contest\//gs;

	$header_text =~ s/href=\"contest.html\"/href=\"rules.cgi?contest=cccvi\"/gs;
	$header_text =~ s/href=\"index.php\"/href=\"submissions.cgi?contest=cccvi\"/gs;
	$header_text =~ s/href=\"entries.php\"/href=\"view.cgi?contest=cccvi\"/gs;
	$header_text =~ s/href=\"prizes.html\"/href=\"prizes.cgi?contest=cccvi\"/gs;

	$header_text =~ s/(<br><br>\s*<p>)/$1<a href="admin.cgi?contest=cccvi" style="text-decoration:none; color:brown"><b>&nbsp;&nbsp;Admin<\/b><\/a>\n<p>/;

    }
}

sub page_headers {
    if(!$headers_done) {
	print "Content-Type: text/html\n\n";
	$headers_done++;
    }
}

sub page_head {
    #print "<html><body>";
    print $header_text;
    print "<style>\n";
    print qq`
body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
}
.CT {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: normal;
    border-collapse: collapse;
    border-spacing: 0pt;
    padding: 1pt;
    margin: 0pt;
    width: 100\%;
    color: \#000000;
    text-align: left;
    vertical-align: top;
}
.CT th {
    background-color: \#CCCCCC;
    border-right: 1pt solid \#FFFFFF;
    border-bottom: 1pt solid \#FFFFFF;
}
.CT td {
    background-color: \#E5E5E5;
    border-right: 1pt solid \#FFFFFF;
    border-bottom: 1pt solid \#FFFFFF;
}
.CT th.CH {
    background-color: \#B2B2B2;
    font-size: 12px;
    border-right: 1pt solid \#FFFFFF;
    border-bottom: 1pt solid \#FFFFFF;
}
.CT td.EMPTY {
    background-color: \#FFFFFF;
}
`;
    print "</style>\n";
    print "<table cellpadding=3><tr><td>\n";
    #print "OUTER CONTENT<br>\n";
}

sub page_foot {
    #print "<br>OUTER CONTENT<br></body></html>\n";
    print "</td></tr></table>\n";
    print $footer_text;
}

sub page_init {
    ReadParse(*params);
    if($params{contest}) {
	$global_contest = $params{contest};
    } elsif($0 =~ /\b(ccc[ivxlcdm]+)\b/) {
	$global_contest = $1;
    } else {
	$global_contest = "cccvxxx";
    }
    
    my($catfile) = "./contests/$global_contest\_categories";
    my($fieldfile) = "./contests/$global_contest\_fields";
    my($rulesfile) = "./contests/$global_contest\_rules";
    if(!-e $catfile || !-e $fieldfile || !-e $rulesfile) {
	page_headers();
	print "Error: Invalid Contest, or could not locate config file for contest: $global_contest<br>\n";
	exit(0);
    }
    $entry_dir = "./$global_contest\_entries";
    $thumb_dir = "./$global_contest\_thumbs";
    if(!(-e $entry_dir)) { mkdir($entry_dir); }
    if(!(-e $thumb_dir)) { mkdir($thumb_dir); }
    if(!(-e $entry_dir) || !(-d $entry_dir)) {
	print "Content-Type: text/html\n\n";
	print "ERROR: Could not create entry directory!<br>\n";
	exit(1);
    }
    $backup_dir = "./$global_contest\_backup";
    if(!(-e $backup_dir)) {
	mkdir($backup_dir);
    }
    if(!(-e $backup_dir) || !(-d $backup_dir)) {
	print "Content-Type: text/html\n\n";
	print "ERROR: Could not create backup directory!<br>\n";
	exit(1);
    }

    require "$catfile";
    require "$fieldfile";
    require "$rulesfile";
}

sub text_safe {
    my($t) = @_;
    $t =~ s/\"/&quot;/g;
    $t =~ s/>/&gt;/g;
    $t =~ s/</&lt;/g;
    return $t;
}

sub html_safe {
    my($t) = @_;
    $t =~ s/\&/&amp;/g;
    $t =~ s/>/&gt;/g;
    $t =~ s/</&lt;/g;
    $t =~ s/\n/<br>\n/g;
    return $t;
}

sub get_entries {
    return read_files($entry_dir);
}

sub get_backups {
    return read_files($backup_dir);
}

sub read_files {
    my($dir) = @_;
    my($DIR,$FL);
    my(@entries) = ();
    if(opendir($DIR,$dir)) {
	my(@files) = readdir($DIR);
	closedir($DIR);
	foreach my $f (@files) {
	    next unless ($f =~ /^\d+$/);
	    my($entry) = undef;
	    $id = $f;
	    $entry->{id} = $f;
	    if(!open($FL,"$dir/$f")) {
		print "Content-Type: text/html\n\n";
		print "Error opening $dir/$f!<br>\n";
		exit(0);
	    }
	    while(<$FL>) {
		next unless(/^(\w+[^\:]*):(.*)$/);
		my($field,$val) = ($1,$2);
		$val =~ s/\\n/\n/gs;
		$val =~ s/\\\\/\//gs;
		$entry->{$field} = $val;
	    }
	    close $FL;
	    push @entries,$entry;
	}
	return \@entries;
    } else {
	print "Content-Type: text/html\n\n";
	print "Error opening $dir!<br>\n";
	exit(0);
    }
}

sub get_entry {
    my($id) = @_;
    my($FL,%entry);
    if(!(open($FL,"$entry_dir/$id"))) {
	print "Content-Type: text/html\n\n";
	print "Error opening entry id $id!<br>\n";
	exit(0);
    }
    $entry{id} = $id;
    while(<$FL>) {
	next unless(/^(\w+[^\:]*):(.*)$/);
	my($field,$val) = ($1,$2);
	$val =~ s/\\n/\n/gs;
	$val =~ s/\\\\/\//gs;
	$entry{$field} = $val;
    }
    close $FL;
    return \%entry;
}

sub pull_img_data {
    my($url,$sid,$field) = @_;
    `wget $url -O $thumb_dir/$sid\_$field`;
    my($idtext) = `identify $thumb_dir/$sid\_$field`;
    if($idtext =~ /\b(\d+)x(\d+)\b/) {
	my($width,$height) = ($1,$2);
	my($type);
	if($idtext =~ /\bjpe?g\b/i) { $type = "jpg"; }
	elsif($idtext =~ /\bbmp\b/i) { $type = "bmp"; }
	elsif($idtext =~ /\bpng\b/i) { $type = "png"; }
	elsif($idtext =~ /\bgif\b/i) { $type = "gif"; }
	else {
	    unlink("$thumb_dir/$sid\_$field");
	    return(undef,undef,undef,undef,undef);
	}
	if($width > $height) {
	    if($width > 160) {
		my $hnew = int($height*160/$width);
		`convert -geometry 160x$hnew $thumb_dir/$sid\_$field $thumb_dir/$sid\_$field\.$type`;
		unlink("$thumb_dir/$sid\_$field");
		return("$sid\_$field\.$type",160,$hnew,$width,$height);
	    }
	} else {
	    if($height > 160) {
		my $wnew = int($width*160/$height);
		`convert -geometry ${wnew}x160 $thumb_dir/$sid\_$field $thumb_dir/$sid\_$field\.$type`;
		unlink("$thumb_dir/$sid\_$field");
		return("$sid\_$field\.$type",$wnew,160,$width,$height);
	    }
	}
	rename("$thumb_dir/$sid\_$field","$thumb_dir/$sid\_$field\.$type");
	return("$sid\_$field\.$type",$width,$height,$width,$height);
    } else {
	#unidentifiable?
	unlink("$thumb_dir/$sid\_$field");
    }
    return(undef,undef,undef,undef,undef);
}

1;
