#!/usr/bin/perl

$s_user_file = 'survey.users';
$s_cookie_name = 'FFS';

sub validate_user {
    my($userid) = get_user();
    if(!defined($userid)) {
	my($userid) = create_user();
	if(!defined($userid)) {
	    #problem!
	} else {
	    my($time) = time();
	    my(@lt) = localtime($time);
	    $lt[5]+=1901;
	    $lt[4];
	    my $dayname = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")[$lt[6]];
	    my $monname = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$lt[4]];
	    my $cookie_expire = "$dayname, $lt[3]-$monname-$lt[5] 23:00:00 GMT";

	    print "Set-Cookie: $s_cookie_name=
	}
    } elsif($userid->{valid}) {
	return 1;
    } else {
	return -1;
    }
}


    print "Set-Cookie: legotrivia=".("_" x (scalar (keys %{$questions})))."; expires=$cookie_expire\n";
    print "Status: 302 Moved Temporarily\n";
    print "Location: $ENV{SCRIPT_NAME}?new_user=1\n\n";
    exit(0);
  }







sub get_user {
    my(%user);
    my(@cookies) = split /; /,$ENV{'HTTP_COOKIE'};
    foreach my $cookie (@cookies) {
	my($key,$value) = split /=/,$cookie,2;
	if($key eq $s_cookie_name) {
	    $user{userid} = $value;
	    my($FILE);
	    if(open($FILE,$s_user_file)) {
		while(<$FILE>) {
		    my($uid,@data) = split /\t/,$_;
		    if($uid eq $value) {
			foreach my $kv (@data) {
			    my($k,$v) = split /:/,$kv,2;
			    $user{$k} = $v;
			}
		    }
		}
		close($FILE);
	    } else {
		#error!
	    }
	    return \%user;
	}
    }
    return undef;
}

sub create_user {
    
}
