#!/usr/bin/perl

###############################################################################
###############################################################################
#
# genDrillChart.pl
#
# Author : Dan Hay ( dan@onepointed.com   http://www.onepointed.com/dan/ )
#
# Version 1.0
#
# Date: April 1, 2005
#
# Copyright 2005 Dan Hay
#
# This script will generate an HTML page containing a scale conversion chart
# for various drill bit sizes.
#
# This script will read drill size data from a text data file.  The data file
# shall contain one entry per line with each line containing the following]
# data:
#
#   <drill-bit-label>:<diameter-inches>:<diameter-mm)
#
# A few sample data lines would be as follows:
#
#   80:.0135:.3429
#   79:.0145:.3683
#   1/64 ":.015625:.3969
#
# A line break may be specified in the data file with a data line of the
# following format:
#
#   <BR>:<BR>:<BR>
#
###############################################################################3
###############################################################################3

use strict;

if ( $#ARGV < 1 ) {
  print "Usage:: $0 <data-file> <output-dest-file>\n";
  print "Please try again...\n\n";
  exit (-1);
}

my ( $infile ) = ( $ARGV[0] );
my ( $outfile ) = ( $ARGV[1] );

if ( ! open ( DATA, "<$infile" ) ) {
  print "Unable to open the input data file '" . $infile ."' for reading.\n";
  print "Fatal error...exiting\n\n";
  exit (-1);
}

if ( ! open ( OUTFILE, ">$outfile" ) ) {
  print "Unable to open the output destination file '" . $outfile ."' for writing.\n";
  print "Fatal error...exiting\n\n";
  exit (-1);
}

my ( @dataLines ) = ( <DATA> );

close DATA;

print OUTFILE "<HTML>\n";
print OUTFILE "  <HEAD>\n";
print OUTFILE "    <TITLE>Scale conversions for drill bits</TITLE>\n";
print OUTFILE "  </HEAD>\n";

print OUTFILE "  <STYLE TYPE=\"text/css\">\n";
print OUTFILE "    BODY {font-family: verdana, arial;}\n";
print OUTFILE "    TD {font-size: x-small; font-family: verdana, arial;}\n";
print OUTFILE "  </STYLE>\n";

print OUTFILE "  <BODY BGCOLOR=\"white\">\n";
print OUTFILE "    <H2>Scale conversions for drill bits</H2>\n";

print OUTFILE "    <TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">\n";

print OUTFILE "     <TR VALIGN=\"top\" ALIGN=\"center\">\n";
print OUTFILE "       <TD ROWSPAN=\"3\"><B>Drill bit</B></TD>\n";
print OUTFILE "       <TD COLSPAN=\"2\"><B>Drill diameter</B></TD>\n";
print OUTFILE "	      <TD COLSPAN=\"8\"><B>Scale size</B></TD>\n";
print OUTFILE "	    </TR>\n\n";

print OUTFILE "	    <TR VALIGN=\"top\" ALIGN=\"center\">\n";
print OUTFILE "	      <TD ROWSPAN=\"2\"><B>in</B></TD>\n";
print OUTFILE "	      <TD ROWSPAN=\"2\"><B>mm</B></TD>\n";
print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/72</B></TD>\n";
print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/76</B></TD>\n";
print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/48</B></TD>\n";
print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/35</B></TD>\n";
print OUTFILE "	    </TR>\n\n";

print OUTFILE "	    <TR VALIGN=\"top\" ALIGN=\"center\">\n";
print OUTFILE "	      <TD><B>in</B></TD>\n";
print OUTFILE "	      <TD><B>mm</B></TD>\n";
print OUTFILE "	      <TD><B>in</B></TD>\n";
print OUTFILE "	      <TD><B>mm</B></TD>\n";
print OUTFILE "	      <TD><B>in</B></TD>\n";
print OUTFILE "	      <TD><B>mm</B></TD>\n";
print OUTFILE "	      <TD><B>in</B></TD>\n";
print OUTFILE "	      <TD><B>mm</B></TD>\n";
print OUTFILE "	    </TR>\n\n";

my ( $line ) = ( undef );
my ( $drillLabel, $sizeInches, $sizeMilimeters ) = ( undef, undef, undef );

my ( $darkColour, $lightColour ) = ( "lightskyblue", "white" );
my ( $rowColourFlag ) = ( 1 );

my ( $in72, $mm72 ) = ( undef, undef );
my ( $in76, $mm76 ) = ( undef, undef );
my ( $in48, $mm48 ) = ( undef, undef );
my ( $in35, $mm35 ) = ( undef, undef );

foreach $line ( @dataLines ) {
  chomp $line;
  next if ( $line =~ /^\s*$/ );
  ( $drillLabel, $sizeInches, $sizeMilimeters ) = ( split /:/, $line );

  if ( $drillLabel eq "<BR>" ) {
    print OUTFILE "    </TABLE>\n\n";

    print OUTFILE "    <BR><BR>\n\n";

    print OUTFILE "    <TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">\n\n";
    print OUTFILE "     <TR VALIGN=\"top\" ALIGN=\"center\">\n";
    print OUTFILE "       <TD ROWSPAN=\"3\"><B>Drill bit</B></TD>\n";
    print OUTFILE "       <TD COLSPAN=\"2\"><B>Drill diameter</B></TD>\n";
    print OUTFILE "	      <TD COLSPAN=\"8\"><B>Scale size</B></TD>\n";
    print OUTFILE "	    </TR>\n\n";

    print OUTFILE "	    <TR VALIGN=\"top\" ALIGN=\"center\">\n";
    print OUTFILE "	      <TD ROWSPAN=\"2\"><B>in</B></TD>\n";
    print OUTFILE "	      <TD ROWSPAN=\"2\"><B>mm</B></TD>\n";
    print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/72</B></TD>\n";
    print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/76</B></TD>\n";
    print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/48</B></TD>\n";
    print OUTFILE "	      <TD COLSPAN=\"2\"><B>1/35</B></TD>\n";
    print OUTFILE "	    </TR>\n\n";

    print OUTFILE "	    <TR VALIGN=\"top\" ALIGN=\"center\">\n";
    print OUTFILE "	      <TD><B>in</B></TD>\n";
    print OUTFILE "	      <TD><B>mm</B></TD>\n";
    print OUTFILE "	      <TD><B>in</B></TD>\n";
    print OUTFILE "	      <TD><B>mm</B></TD>\n";
    print OUTFILE "	      <TD><B>in</B></TD>\n";
    print OUTFILE "	      <TD><B>mm</B></TD>\n";
    print OUTFILE "	      <TD><B>in</B></TD>\n";
    print OUTFILE "	      <TD><B>mm</B></TD>\n";
    print OUTFILE "	    </TR>\n\n";

    $rowColourFlag = 1;
    next;
  } #end if

  $in72 = 72 * $sizeInches;
  $mm72 = 72 * $sizeMilimeters;

  $in76 = 76 * $sizeInches;
  $mm76 = 76 * $sizeMilimeters;

  $in48 = 48 * $sizeInches;
  $mm48 = 48 * $sizeMilimeters;

  $in35 = 35 * $sizeInches;
  $mm35 = 35 * $sizeMilimeters;

  print OUTFILE "       <TR ALIGN=\"center\" VALIGN=\"top\" BGCOLOR=\"";
  if ( $rowColourFlag ) {
    print OUTFILE $darkColour;
    $rowColourFlag = 0;
  } else {
    print OUTFILE $lightColour;
    $rowColourFlag = 1;
  }
  print OUTFILE "\">\n";

  print OUTFILE "         <TD>" . $drillLabel . "</TD>\n";
  print OUTFILE "	  <TD>" . $sizeInches . "</TD>\n";
  print OUTFILE "	  <TD>" . $sizeMilimeters . "</TD>\n";

  print OUTFILE "	  <TD>" . $in72 . "</TD>\n";
  print OUTFILE "	  <TD>" . $mm72 . "</TD>\n";

  print OUTFILE "	  <TD>" . $in76 . "</TD>\n";
  print OUTFILE "	  <TD>" . $mm76 . "</TD>\n";

  print OUTFILE "	  <TD>" . $in48 . "</TD>\n";
  print OUTFILE "	  <TD>" . $mm48 . "</TD>\n";

  print OUTFILE "	  <TD>" . $in35 . "</TD>\n";
  print OUTFILE "	  <TD>" . $mm35 . "</TD>\n";

  print OUTFILE "	</TR>\n\n";

} #end foreach


print OUTFILE "</TABLE>\n\n";

print OUTFILE "</BODY>\n";
print OUTFILE "</HTML>\n\n";

close OUTFILE;

exit (0);
