#!/usr/bin/perl -w

#wirefire2.pl?x=getMovieNames&i=yourName

$formdata = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$formdata);
@pairs0 = split(/=/,$pairs[0]);
@pairs1 = split(/=/,$pairs[1]);

$iName = $pairs1[1];

$lastLines = "";

if ( $pairs0[1] eq "getMovieNames" )
	{
	print "Content-type: text/plain\n\n";
	@cats = ("actions","dreams","sounds","music","images","backgrounds","foregrounds","misc");
	$allDirs = "";
	foreach $cat (@cats)
		{
		if ( $cat ne "misc" )
			{
			$thisDir = "../movies/$cat\_$iName";
			}
		else
			{
			$thisDir = "../movies/$cat";
			}
		opendir(IMD, $thisDir) || die("Cannot open directory");
		@thefiles= readdir(IMD);
		closedir(IMD);

		my(@sorted) = sort { $a cmp $b } @thefiles;   # example using sort
		my($f);
		foreach $f (@sorted)
			{
			unless ( ($f eq ".") || ($f eq "..") )
				{
				$allDirs = "$allDirs<m cat=\"$cat\" name=\"$f\" />\n";
				}		
			}
		}
	print $allDirs;
	}
elsif ( $pairs0[0] eq "getDirList" )
	{
	print "Content-type: text/plain\n\n";
	$thisDir = $pairs0[1];
	opendir(IMD, $thisDir) || die("Cannot open directory");
	@thefiles= readdir(IMD);
	closedir(IMD);
		$allDirs = "";
	my(@sorted) = sort { $a cmp $b } @thefiles;   # example using sort
	my($f);
	foreach $f (@sorted)
		{
		unless ( ($f eq ".") || ($f eq "..") )
			{
			$allDirs = "$allDirs$f\n";
			}
		}
	print "$allDirs";
	}
elsif ( $pairs0[0] eq "loadData" )
	{
	print "Content-type: text/plain\n\n";
	$file = $pairs0[1];
	open(INFO, $file);
	@lines = <INFO>;
	foreach $line (@lines)
		{
		print "$line";
		}
	close(INFO);
	}
elsif ( $pairs0[0] eq "saveData" )
	{
	print "Content-type: text/plain\n\n";
	print "saving...";
	$thisLine = $pairs0[1];
	$thisLine =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;

	# append to data file
	$file = "data2";
	open(INFO,">>$file");
	print INFO "$thisLine\n";
	close(INFO);

	# append to old style data file
	$dataPlace = index $thisLine, "data=";
	$curLevelPlace = index $thisLine, "curLevel=";
	$thisLineLength = length $thisLine;
	$data = substr $thisLine, $dataPlace + 6, $thisLineLength - $dataPlace - ( $thisLineLength - $curLevelPlace ) - 8;
	#$data = substr $thisLine, $dataPlace + 5, $thisLineLength;
	$lastslash = rindex $data, "/";
	$datalength = length $data;
	$oldnum = substr $data, $lastslash + 1, $datalength - $lastslash - 5;
	$oldnumlength = length $oldnum;
	$moviePathlength = length $moviePath;
	$oldcat = substr $data, length $moviePath, $datalength - $moviePathlength - $oldnumlength - 5;
	$swiftvar = substr $oldcat, 0, 5;
	if ( $swiftvar eq "swift" )
		{
		$lastslash = rindex $oldcat, "/";
		$oldcatlength = length $oldcat;
		$oldcat = substr $oldcat, $lastslash + 1, $oldcatlength - $lastslash;
		$newcat = "swift$oldcat";
		$newnum = "$oldcat/$oldnum";
		$oldcat = $newcat;
		$oldnum = $newnum;
		};
	$file = "data1";
	open(INFO,">>$file");
	print INFO "\n$oldcat,$oldnum";
	close(INFO);
	print "done.";
	}
elsif ( $pairs0[0] eq "archiveData" )
	{
	$file = "data2";
	$newFile = "archive/data$pairs0[1]";
	open(INFO, $file);
	@lines = <INFO>;
	close(INFO);
	open(INFO, ">$newFile");
	foreach $line (@lines)
		{
		print INFO "$line";
		}
	close(INFO);
	rename "../movies/a.jpg","../movies/$pairs0[1]a.jpg";
	rename "../movies/z.jpg","../wirefire/movies/$pairs0[1]z.jpg";
	}
elsif ( $pairs0[0] eq "archiveChat" )
	{
	$newFile = $pairs0[1];
	$allChatLines = $pairs1[1];
	$allChatLines =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
	open(INFO, ">$newFile");
	print INFO "$allChatLines";
	close(INFO);
	}
elsif ( $pairs0[0] eq "clearData" )
	{
	print "Content-type: text/plain\n\n";
	$file = $pairs0[1];
	open(INFO, ">$file");
	print INFO "\n";
	close(INFO);
	}
elsif ( $pairs0[0] eq "status" )
	{
	print "Content-type: text/plain\n\n";
	$file = "live";
	open(INFO, ">$file");
	print INFO "<response status=\"$pairs0[1]\" />";
	close(INFO);
	}
elsif ( $pairs0[0] eq "config" )
	{
	$file = "config";
	@configData = split(/,/,$pairs0[1]);
	open(INFO, ">$file");
	print INFO "<config theServer=\"$configData[0]\" thePort=\"$configData[1]\" thePath=\"$configData[2]\" />";
	close(INFO);
	}
elsif ( $pairs0[1] eq "load" )
	{
	# read data from data file
	$file = "data";
	open(INFO, $file);
	@lines = <INFO>;
		$lineNum = 0;
		foreach $line (@lines)
			{
			$lineNum = $lineNum + 1;
			# limit output to x lines
			if ( $lineNum > $#lines - 5 )
				{
				$lastLines = "$lastLines$line<BR>";
				}
			}
	close(INFO);

	print '<HTML><HEAD><TITLE>e8z</TITLE></HEAD>';
	print '<BODY BGCOLOR="#000000" TEXT="#990000" onLoad="parent.showMovie();">';
	print '<TABLE WIDTH="100%" HEIGHT="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD ALIGN="left" VALIGN="top">';
	print '<FONT FACE="Verdana,Arial,Helvetica" SIZE=2>';
	print $lastLines;
	print '</FONT>';
	print '</TD></TR></TABLE>';
	print '<FORM NAME="lineBox">';
	print '<TEXTAREA NAME="allLines" COLS=24 ROWS=40>';
	print @lines;
	print '</TEXTAREA>';
	print '</FORM>';
	print '</BODY></HTML>';
	}

print "Content-type: text/plain\n\n";
print "done";