#! /usr/bin/perl -w
use strict;
use vars qw ( $DELIMETER %css %data $BASEDIR );

$BASEDIR = '/var/www/fallman.org/daniel.fallman.org/';

$DELIMETER = ';';
%css = (
	'0' => 1,
	'1' => 1,
	'2' => 1,
	'3' => 1,
	'4' => 1,
);

%data = &GetData();

if( defined($data{'css'}) ) {
	if( defined($css{$data{'css'}}) ) {
		if( system("ln","-fs",$BASEDIR."css/".$data{'css'}.".css",$BASEDIR."css/css.css") == 0 ) {
			print <<EOF;
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Location: http://daniel.fallman.org/

EOF
		} else {
			print <<EOF;
Content-Type: text/plain

Internal error. ($?)
EOF
		};
	} else {
		print <<EOF;
Content-Type: text/plain

Wrong css.
EOF
	};
} else {
	print <<EOF;
Content-Type: text/plain

Error, no css.
EOF
};
exit;



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

sub Decode
{
	my ($string) = @_;
	$string =~ s/%([a-fA-F0-9]{1,2})/pack("C",hex($1))/ge;
	return $string;
};

sub GetData
{
	my (%user_data,$user_string,$name_value_pair,@name_value_pairs,$name,$value);
	if( $ENV{'REQUEST_METHOD'} eq "POST" ) {
		read(STDIN,$user_string,$ENV{'CONTENT_LENGTH'});
		$user_string =~ s/\+/ /g;
		@name_value_pairs = split(/$DELIMETER|&/, $user_string);  # All browsers seems to send & as delimeter on POST. grrr... /jonasf
	} else {
		$user_string = $ENV{'QUERY_STRING'};
		$user_string =~ s/\+/ /g;
		@name_value_pairs = split(/$DELIMETER|&/, $user_string);
	}

	foreach $name_value_pair ( @name_value_pairs ) {
		($name,$value) = split(/=/, $name_value_pair);
		$user_data{&Decode($name)} = &Decode($value);
	}
	return %user_data;
};

