#!/usr/bin/perl

package laq;
use Data::Dumper;
my($laq_dir) = "/home/dave/public_html/laq";
my($set_dir) = "$laq_dir/sets";

sub get_series {
    my($SF,%series);
    if(!open($SF,"$laq_dir/series.txt")) {
	error("Couldnt get series!");
    } else {
	while(<$SF>) {
	    if(/^\w+\t\S+/) {
		chomp;
		my($sid,$name) = split /\t/;
		$series{$sid}{sid} = $sid;
		$series{$sid}{name} = $name;
	    }
	}
	close $SF;
    }
    return \%series;
    
}

sub get_colors {
    my($COLF,%colors);
    if(!open($COLF,"$laq_dir/colors.txt")) {
	error("Couldnt get colors!");
    } else {
	while(<$COLF>) {
	    if(/^\d+\t\S+/) {
		chomp;
		my($cid,$cname,$rgb,$trgb) = split /\t/;
		$colors{$cid}{cid} = $cid;
		$colors{$cid}{name} = $cname;
		$colors{$cid}{rgb} = $rgb;
		$colors{$cid}{trgb} = $trgb;
	    }
	}
	close $COLF;
    }
    return \%colors;
}

sub get_elements {
    my($ELF,%elements);
    if(!open($ELF,"$laq_dir/elements.txt")) {
	error("Couldnt get elements!");
    } else {
	while(<$ELF>) {
	    if(/^\d+\t\S+/) {
		chomp;
		my($eid,$eabbr,$ename) = split /\t/;
		$elements{$eid}{cid} = $eid;
		$elements{$eid}{abbr} = $eabbr;
		$elements{$eid}{name} = $ename;
	    }
	}
	close $ELF;
    }
    return \%elements;
}

sub write_set {
    my($set,$id) = @_;
    my($text) = "";
    foreach my $k (keys %{$set}) {
	next if($k eq "id");
	next if($k eq "inv");
	if($k =~ /[^A-Za-z_0-9]/) {
	    error("Bad key for writing: $k");
	    return 0;
	}
	my($value) = $set->{$k};
	$value =~ s/\\/\\\\/gs;
	$value =~ s/\n/\\n/gs;
	$value =~ s/\r/\\r/gs;
	$value =~ s/\t/\\t/gs;
	$text .= "$k:$value\n";
    }
    foreach my $cid (sort {$a <=> $b} (keys %{$set->{inv}{colors}})) {
	foreach my $eid (sort {$a <=> $b} (keys %{$set->{inv}{colors}{$cid}})) {
	    if($set->{inv}{colors}{$cid}{$eid}{qty} || $set->{inv}{colors}{$cid}{$eid}{comment}) {
		$text .= "element:$cid\t$eid\t$set->{inv}{colors}{$cid}{$eid}{qty}\t$set->{inv}{colors}{$cid}{$eid}{qty}\n";
	    }
	}
    }
    my($SETF);
    if(open($SETF,"> $set_dir/$setid")) {
	print $SETF $text;
	close $SETF;
    } else {
	error("Couldn't write $setid!");
    }
}

sub get_sets {
    my($SDIR,%sets);
    if(!opendir($SDIR,$set_dir)) {
	error("Couldn't open set dir!");
    } else {
	my(@files) = readdir($SDIR);
	closedir($SDIR);
	foreach my $f (@files) {
	    #print "Checking $f<br>\n";
	    next unless($f =~ /^[A-Za-z\_0-9]+$/);
	    $sets{$f} = get_set($f);
	}
    }
    return \%sets;
}

sub get_set {
    my($setid) = @_;
    my($SETF,%set);
    #print "Reading $setid<br>\n";
    if(!open($SETF,"$set_dir/$setid")) {
	error("Couldnt open $setid set!");
    } else {
	$set{id} = $setid;
	#print "Got to $setid<br>\n";
	while(<$SETF>) {
	    if(/^element:(.*)$/) {
		my($eid,$cid,$qty,$cmt) = split /\t/,$1;
		$set{inv}{elements}{$eid}{$cid}{qty} = $qty;
		$set{inv}{elements}{$eid}{$cid}{comment} = $cmt;
		$set{inv}{colors}{$cid}{$eid}{qty} = $qty;
		$set{inv}{colors}{$cid}{$eid}{comment} = $cmt;
	    } elsif(/^(\w+):(.*)$/) {
		my($tag) = $1;
		my($value) = $2;
		$value =~ s/\\n/\n/g;
		$value =~ s/\\t/\t/g;
		$value =~ s/\\r/\r/g;
		$value =~ s/\\\\/\\/g;
		if($tag eq "inv" || $tag eq "id") {
		    #these are reserved!
		} else {
		    $set{$tag} = $value;
		}
	    }
	}
	close $SETF;
    }
    #print Dumper(\%set);
    return \%set;
}

sub error {
    if($ENV{SCRIPT_URI}) {
	print "Content-Type: text/html\n\n";
	print $_[0];
	exit(0);
    } else {
	print STDERR "$_[0]\n";
    }
}

1;
