#!/usr/bin/perl

#require "../cgi-lib.pl";
#ReadParse(*in);
#print "Content-type: text/html\n\n";
require "../pagelib.pl";

header();
init();
head();
body();
foot();

sub body {
    def_js();
    print "<br>\n";
    print "<table width=\"100\%\"><tr><td>\n";
    print "<font size=+1><b>Lego Scale Converter</b></font><br>\n";
    print "<form>\n";
    print "<table><tr><td>\n";
    print "How much is: <input type=text name=cval size=5 value=\"$in{cval}\">\n";
    $in{cval} =~ s/,/./g;
    print "<select name=cunit>\n";
    foreach (sort { $units{$a}{order} <=> $units{$b}{order}; } (keys %units)) {
	print "<option value=\"$_\"";
	print " selected" if($in{cunit} eq $_);
	print ">$units{$_}{name}</option>\n";
    }
    print "</select>\n";

    print "<input type=checkbox".(($in{precision}) ? " checked" : "")." name=precision value=1><font size=-1>Extra Geeky Precision!</font>\n";

    print "<br>Fig scale:\n";
    print "<select name=fscale>\n";
    foreach (sort { $scales{$a}{order} <=> $scales{$b}{order}; } (keys %scales)) {
	print "<option value=\"$_\"";
	print " selected" if($in{fscale} eq $_);
	print ">$scales{$_}{name}</option>\n";
    }
    print "</select>\n";
    $in{fheight} ||= "6";
    print "&nbsp;Assumed fig height: <input type=text size=4 name=fheight value=\"$in{fheight}\">\n";
    $in{fheight} =~ s/,/./g;
    $in{funit} ||= "foot";
    print "<select name=funit>\n";
    foreach (sort { $units{$a}{order} <=> $units{$b}{order}; } (keys %units)) {
	next if($units{$_}{mtype} ne "real");
	print "<option value=\"$_\"";
	print " selected" if($in{funit} eq $_);
	print ">$units{$_}{name}</option>\n";
    }
    print "</select>\n";
    print "</td></tr><tr><td align=center>";
    print "<input type=submit value=\"Convert\">";
    print "<input type=reset value=\"Reset\">";
    print "</td></tr></table\n";
    print "</td><td>\n";
    print "<table border=0>\n";
    #print "<tr><td><img width=100 src=\"/~dave/images/scale.gif\"></td></tr>\n";
    print "<tr><td><font size=-1>Common Scales</font></td></tr>\n";
    print "<tr><td><a href=Javascript:use6ft()><font size=-1>Use fig = 6 feet tall</font></a></td></tr>\n";
    print "<tr><td><a href=Javascript:use1ft()><font size=-1>Use 1 stud = 1 foot</font></a></td></tr>\n";
    print "<tr><td><a href=Javascript:use1m()><font size=-1>Use 3 studs = 1 meter</font></a></td></tr>\n";
    print "</table>\n";
    print "</td></tr>\n";
    print "</table>\n";
    print "</form>\n";

    if($in{cval} =~ /^\s*\d+\.?\d*\s*$/ || $in{cval} =~ /^\s*\.\d+\s*$/) {
	if($in{fheight} =~ /^\s*\d+\.?\d*\s*$/ || $in{fheight} =~ /^\s*\.\d+\s*$/) {
	    if($scales{$in{fscale}}{hlen} =~ /^([\d\.]+)mm$/) { $fmmht = $1; }
	    else { $fmmht = 42; } #why wouldnt it?
	    if($units{$in{cunit}}{hlen} =~ /^[\d\.]+(mm|in)$/) {
		if($units{$in{cunit}}{hlen} =~ /^([\d\.]+)mm$/) {
		    $mmlen = $1*$in{cval};
		    $inlen = $mmlen/25.4;
		} elsif($units{$in{cunit}}{hlen} =~ /^([\d\.]+)in$/) {
		    $inlen = $1*$in{cval};
		    $mmlen = $inlen*25.4;
		}
		if($units{$in{funit}}{hlen} =~ /^([\d\.]+)in$/) {
		    $mfinlen = $mmlen/(($fmmht)/($in{fheight}*$1));
		    $mfmmlen = $mfinlen*25.4;
		}
		elsif($units{$in{funit}}{hlen} =~ /^([\d\.]+)mm$/) {
		    $mfmmlen = $mmlen/(($fmmht)/($in{fheight}*$1));
		    $mfinlen = $mfmmlen/25.4;
		}
	    } else {
		if($units{$in{cunit}}{hlen} =~ /^([\d\.]+)mfmm$/) {
		    $mfmmlen = $1*$in{cval};
		    $mfinlen = $mfmmlen/25.4;
		} elsif($units{$in{cunit}}{hlen} =~ /^([\d\.]+)mfin$/) {
		    $mfinlen = $1*$in{cval};
		    $mfmmlen = $mfinlen*25.4;
		}
		if($units{$in{funit}}{hlen} =~ /^([\d\.]+)in$/) {
		    $mmlen = ($mfinlen*$fmmht)/($in{fheight}*$1);
		    $inlen = $mmlen/25.4;
		} elsif($units{$in{funit}}{hlen} =~ /^([\d\.]+)mm$/) {
		    $mmlen = ($mfmmlen*$fmmht)/($in{fheight}*$1);
		    $inlen = $mmlen/25.4;
		}
	    }
	    print "<table>\n";
	    my($ratio1,$ratio2);
	    foreach $m ("brick","fig","real") {
		print "<tr><td>\n";
		print "<table border=1 width=100\%>\n";
		$r1 = "";
		$r2 = "";
		foreach $unit (sort { $units{$a}{order} <=> $units{$b}{order}; } (keys %units)) {
		    next if($units{$unit}{mtype} ne $m);
		    $r1 .= "<td><B>";
		    if($m eq "fig") {
			$r1 .= ($in{fscale} eq "minifig") ? "mini" : "tech. ";
		    }
		    $r1 .= "$units{$unit}{name}</B></td>\n";
		    $r2 .= "<td>";
		    $val = 0;
		    if($units{$unit}{hlen} =~ /^([\d\.]+)(\w+)$/) { ($tmult,$tunit) = ($1,$2); }
		    else { $val = "ERROR!\n"; ($tmult,$tunit) = (1,"mm"); }
		    if($tunit eq "mm") { $val = ($mmlen/$tmult); }
		    elsif($tunit eq "in") { $val = ($inlen/$tmult); }
		    elsif($tunit eq "mfmm") { $val = ($mfmmlen/$tmult); }
		    elsif($tunit eq "mfin") { $val = ($mfinlen/$tmult); }
		    else { $val = "error"; }
		    if($unit eq "mfft") {
			$ratio1 = $val;
		    } elsif($unit eq "foot") {
			$ratio2 = $val;
		    }
		    if($in{precision}) { $r2 .= $val; } 
		    else { $r2 .= sprintf("%2.2f",$val); }
		    $r2 .= "</td>\n";
		}
		print "<tr>$r1</tr>\n<tr>$r2</tr>\n";
		print "</table>\n";
		print "</td></tr>\n";
	    }
	    print "</table>\n";
	    if($ratio1 && $ratio2) {
		my $ratio = $ratio1/$ratio2;
		print "<br><b>Scale Ratio</b>: 1 : ".($in{precision} ? $ratio : sprintf("%2.2f",$ratio))."<br>\n";
	    }
	} else {
	    print "Invalid assumed height!<br>\n";
	}
    } elsif($in{cval}) {
	print "Invalid value!<br>\n";
    }
}

sub init {
    %units = (
	      stud => {
		  name => "studs",
		  hlen => "8mm",
		  order => 1,
		  mtype => "brick",
	      },
	      plate => {
		  name => "plates",
		  hlen => "3.2mm",
		  order => 2,
		  mtype => "brick",
	      },
	      brick => {
		  name => "bricks",
		  hlen => "9.6mm",
		  order => 3,
		  mtype => "brick",
	      },
	      base32 => {
		  name => "32x32 baseplates",
		  hlen => "256mm",
		  order => 4,
		  mtype => "brick",
	      },
	      base48 => {
		  name => "48x48 baseplates",
		  hlen => "384mm",
		  order => 5,
		  mtype => "brick",
	      },
	      milli => {
		  name => "millimeters",
		  hlen => "1mm",
		  order => 6,
		  mtype => "real",
	      },
	      centi => {
		  name => "centimeters",
		  hlen => "10mm",
		  order => 7,
		  mtype => "real",
	      },
	      inch => {
		  name => "inches",
		  hlen => "1in",
		  order => 8,
		  mtype => "real",
	      },
	      foot => {
		  name => "feet",
		  hlen => "12in",
		  order => 9,
		  mtype => "real",
	      },
	      yard => {
		  name => "yards",
		  hlen => "36in",
		  order => 10,
		  mtype => "real",
	      },
	      meter => {
		  name => "meters",
		  hlen => "1000mm",
		  order => 11,
		  mtype => "real",
	      },
	      kilo => {
		  name => "kilometers",
		  hlen => "1000000mm",
		  order => 12,
		  mtype => "real",
	      },
	      mile => {
		  name => "miles",
		  hlen => "63360in",
		  order => 13,
		  mtype => "real",
	      },
	      mfin => {
		  name => "fig inches",
		  hlen => "1mfin",
		  order => 14,
		  mtype => "fig",
	      },
	      mfft => {
		  name => "fig feet",
		  hlen => "12mfin",
		  order => 15,
		  mtype => "fig",
	      },
	      mfyd => {
		  name => "fig yards",
		  hlen => "36mfin",
		  order => 16,
		  mtype => "fig",
	      },
	      mfm => {
		  name => "fig meters",
		  hlen => "1000mfmm",
		  order => 17,
		  mtype => "fig",
	      },
	      mfkm => {
		  name => "fig kilometers",
		  hlen => "1000000mfmm",
		  order => 18,
		  mtype => "fig",
	      },
	      mfmi => {
		  name => "fig miles",
		  hlen => "63360mfin",
		  order => 19,
		  mtype => "fig",
	      },
	      );
    %scales = (
	       minifig => {
		   name => "mini",
		   hlen => "42mm",
		   order => 1,
	       },
	       technic => {
		   name => "technic",
		   hlen => "81mm",
		   order => 2,
	       },
	       );
}

sub def_js {
    print "<script language=Javascript>\n";
    print qq`

function use6ft() {
  this.document.forms[0].fheight.value='6';
  this.document.forms[0].funit.selectedIndex=3;
  this.document.forms[0].fscale.selectedIndex=0;
}
function use1ft() {
  this.document.forms[0].fheight.value='5.25';
  this.document.forms[0].funit.selectedIndex=3;
  this.document.forms[0].fscale.selectedIndex=0;
}
function use1m() {
  this.document.forms[0].fheight.value='1.75';
  this.document.forms[0].funit.selectedIndex=5;
  this.document.forms[0].fscale.selectedIndex=0;
}

`;
    print "</script>\n";
}

sub head_old {
    print "<html><body>\n";
}

sub foot_old {
    print "</body></html>\n";
}
