#!/usr/bin/perl -w

# ------------------
# script basics
# ------------------

# if errors tell us what they are
use CGI::Carp qw(fatalsToBrowser);

# enable parameters
use CGI qw(:standard);

# output to browser
print "Content-type: text/html\n\n";

# this file's name
$_ = $ENV{'SCRIPT_NAME'} ;
     s/([^\/\\]+)$//;
$thispage = $1  ;

# ------------------
# View code
# ------------------
  $viewcode = param('viewcode');
  if ($viewcode eq 'true') {
    open(CODE, $thispage) || die("Could not open file!") ;
      @code=<CODE> ;
    close(CODE) ;
    print qq(<pre>);
    foreach $line (@code) {
      $line =~ s/</\</g;
      print $line ;
    }
    print qq(</pre>);
    exit;
  }

# ----------------------
# determine program flow
# ----------------------
$path = qq(/tmp) ;

  $file = param('file');
  if ( (!defined $file) || (!$file) || ( $file =~ m/^\s$/ ) ) {
    $proceed = 0;
  }
  else {
    $proceed = 1;
    $outFile = $path . '/tempPalette.txt';
  }

# ------------------
# Part 1 - upload
# ------------------
if ($proceed) {
  # ------------------
  # Upload the file
  # ------------------
  open (OUTFILE, ">$outFile") || die("Could not open file!") ;
    binmode OUTFILE;
    while ($bytes = read($file,$buffer,1024)) {
      $z_size += $bytes;
      print OUTFILE $buffer;
    }
  close(OUTFILE);
  # ------------------
  # Check it got there
  # ------------------
  if  ( ($z_size < 500) || ($z_size > 4000) ) {
    unlink("$outFile");
    print qq(Improper file size);
    exit;
  }
  # ------------------
  # Read file
  # ------------------
  open(DAT, $outFile) || die("Could not open file!") ;
    @palette=<DAT> ;
  close(DAT) ;
  $colorcount = 0;
  $count = 0;
  foreach $line (@palette) {
    $count++;
    $line =~ s/\r$//;
    $line =~ s/\c$//;
    $line =~ s/\n$//;
    if ( ($count > 3 ) ) {
     ($r[$colorcount], $g[$colorcount], $b[$colorcount]) = split(/ /,$line);
      $colorcount++;
    }
  }
  # ------------------
  # Convert palette
  # ------------------
  @indices  = (   4,   8, 15,   16,  24,  31,  56, 105, 134, 152, 156, 186, 214, 218 );
  @replaceR = (  33,  66, 123, 132, 197, 255, 121, 216, 158, 191, 255,  28,  20,  28 );
  @replaceG = (  33,  66, 123, 132, 197, 255, 163,  89, 158,  24,  33,  28, 158, 223 );
  @replaceB = (  33,  66, 123, 132, 197, 255, 118,  16,  20,  24,  33, 223,  20,  28 );

  $moveto = 230;
  $count = 0;
  foreach $index (@indices) {
    # copy mtm2 to mtm1 palette values
    $r[$moveto] = $r[$index];
    $g[$moveto] = $g[$index];
    $b[$moveto] = $b[$index];
    $moveto++;
    # insert mtm1 default palette values
    $r[$index] = $replaceR[$count];
    $g[$index] = $replaceG[$count];
    $b[$index] = $replaceB[$count];
    $count++;
  }

  # -------------------
  # Print results
  # -------------------
  # set up table
  # -------------------
  print qq(
    <table width=520 align=center>
      <tr valign=top>
        <td>
  );

  # -------------------
  # display old palette
  # -------------------
  print qq(
    <table align="center" cellpadding="0" cellspacing="1" border="1" style="width: 256px; height: 256px;">
      <tbody>
        <tr>
  );
    $count = 0;
    foreach $value (@palette) {
      $count++;
      #if ($line ne '') {
        if  ( $count >= 4 )  {
          $line = $value;
          $line =~ s/ /,/g;
          print qq(\n      <td style="height: 16px; width: 16px; background-color: rgb(${line});">   </td>);
          if  ( ( ($count - 3) % 16 ) == 0 ) {
            print qq(    </tr>\n    <tr>);
          }
        }
      #}
    }
  print qq(
        </tr>
      </tbody>
    </table></td>
  );

  # -------------------
  # display new palette
  # -------------------
  print qq(<td>
    <table align="center" cellpadding="0" cellspacing="1" border="1" style="width: 256px; height: 256px;">
      <tbody>
        <tr>
  );
    $count = 0;
    for ($i=0; $i<256 ; $i++)  {
      $count++;
      print qq(\n          <td style="height: 16px; width: 16px; background-color: rgb($r[$i], $g[$i], $b[$i]);">   </td>);
      if  (($count % 16) == 0) {
        print qq(    </tr>\n    <tr>);
      }
    }
  print qq(
        </tr>
      </tbody>
    </table></td></tr>
  );

  # -------------------
  # print old palette
  # -------------------
  print qq(
      <tr valign=top>
        <td><pre><b>Old Pal</b>
);
  foreach $line (@palette) {
    print $line . "\n";
  }

  # -------------------
  # print new palette
  # -------------------
  print qq(
        </td>
        <td><pre><b>New Pal</b>\nJASC-PAL\n0100\n256
);

  for ($i=0; $i<256 ; $i++)  {
    print qq($r[$i] $g[$i] $b[$i]\n);
  }

  print qq(
        </td>
      </tr>
    </table>
  );

  # ------------------
  # Clean up
  # ------------------
  if (-e "$outFile") {
    unlink("$outFile");
  }
  @r = undef;
  @g = undef;
  @b = undef;
  @indices  = undef;
  @replaceR = undef;
  @replaceG = undef;
  @replaceB = undef;
  $colorcount = undef;
  $count = undef;
  $moveto = undef;
  $i = undef;

} # end upload

# ---------------------
# Part 2 - default page
# ---------------------
else {
  print qq(
    MTM2 to MTM1 Track Palette Converter <p>
    <form action="$thispage" method="post" enctype="multipart/form-data">
      <input type="file"  name="file"  size="48" maxlength="80">
      <input type="submit"  value="Do it!" >
    </form>
    <p><a href="$thispage?viewcode=true">view code</a>
  );
}



# end of file