#!/usr/bin/perl

# lists colour schemes in favoured order first
# (so display will be all pretty and rainbow like,
# and in the same order for all template styles);
# then scan directory for any new colour schemes.

require "wiz_resources";

header("List Colours","","edit");

if ($user ne "") {
  if ($template eq "") {
    headline("Error: No template defined","red","","You should not use this page unless you have set the template style, as the number of colours available is unknown at this time.  Please <a href=\"/cgi-bin/edit\">edit your details</a> and try again.");
  } else {
$tablecount=-1;
print <<EOF;
<form method="post" action="/cgi-bin/edit">
<input type="hidden" name="action" value="setcolour">

<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td colspan="4" bgcolor="#999999"><img src="/gfx/pad.gif" width="600" height="1" alt="-"></td>
</tr>

EOF

opendir LIST, "$templatedir"
        or die("Unable to open directory $templatedir for listing");

undef @favouredcolours;
push @favouredcolours, "red";
push @favouredcolours, "orange";
push @favouredcolours, "yellow";
push @favouredcolours, "darkgreen";
push @favouredcolours, "lightgreen";
push @favouredcolours, "cyan";
push @favouredcolours, "skyblue";
push @favouredcolours, "mediumblue";
push @favouredcolours, "darkblue";
push @favouredcolours, "purple";
push @favouredcolours, "pink";
push @favouredcolours, "silver";
push @favouredcolours, "gold";
push @favouredcolours, "midnight";
push @favouredcolours, "midnightinthegarden";
push @favouredcolours, "midnightgold";
push @favouredcolours, "midnightblood";
push @favouredcolours, "midnightgrey";
push @favouredcolours, "invert";
push @favouredcolours, "grey";
push @favouredcolours, "totally_brown";
push @favouredcolours, "totally_red";
push @favouredcolours, "totally_orange";
push @favouredcolours, "totally_yellow";
push @favouredcolours, "totally_green";
push @favouredcolours, "totally_cyan";
push @favouredcolours, "totally_light_blue";
push @favouredcolours, "totally_dark_blue";
push @favouredcolours, "totally_purple";
push @favouredcolours, "totally_cerise";
push @favouredcolours, "totally_pink";
push @favouredcolours, "totally_silver";
push @favouredcolours, "totally_tan";

for ($loop=0; $loop<@favouredcolours; $loop++) {
  if (-d "$templatedir/$favouredcolours[$loop]") {
    $blocked{$favouredcolours[$loop]}=1;
    show_colour($favouredcolours[$loop]);
  }
}

print "\n";

while ($file = readdir(LIST)) {
  unless ($blocked{$file}) {
    if ((-d "$templatedir/$file") && ($file ne ".") && ($file ne "..") && (!($file =~ /~/)) ) {
      show_colour($file);
    }
  }
}
closedir LIST;
if (($tablecount % 4) != 3) {
  print "</tr>\n";
    print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"/gfx/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
}
print "<tr><td colspan=\"4\" align=\"center\"><input type=\"submit\" value=\"continue\"></td></tr>\n\n";
    print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"/gfx/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
print "</table></form>\n";

}#close "no template";
} else {
  headline("Error: Access denied","red","","You should not use this page unless you have logged into the system.  Please <a href=\"/cgi-bin/login\">log in</a> and try again.");
}
footer();

sub show_colour {
  $showcolour=$_[0];
  $tablecount++;
  
  $printname=ucfirst($showcolour);
  if (-e "$templatedir/$showcolour/defaults.txt") {
    open READ, "<$templatedir/$showcolour/defaults.txt";
    while ($line = <READ>) {
      if ($line =~ m~^name[\t| ]+(.*?)$~is) {
        $printname=$1;
      }
    }
    close READ;
  }
  

  if (($tablecount % 4) == 0) {
    print "<tr valign=\"top\" align=\"center\" cols=\"4\">\n";
  }
  print "<td width=\"25%\">";
  print "<b>$printname</b><br>\n";
  if (-e "$templatedir/$showcolour/thumb.gif") {
    print "<img src=\"$templateroot/$showcolour/thumb.gif\" alt=\"Thumbnail\"><br>\n";
  } else {
    print "<img src=\"/gfx/noimage.gif\" alt=\"No thumbnail available\" width=\"150\" height=\"100\"><br>\n";
    
  }
  print "<input type=\"radio\" name=\"colour_scheme\" value=\"$showcolour\"";
  print " checked" if ($showcolour eq $colourscheme);
  print ">\n";
  print "</td>";
  if (($tablecount % 4) == 3) {
    print "</tr>\n\n";
    print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"/gfx/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
  }
}