#!/usr/bin/perl -w
use strict;
use Getopt::Long;

((my $progname = $0) =~ s/^.*(\/|\\)//ig); # basename $0

# Parse options
my $no_verbose_progress = 0;
my $usage = 0;
GetOptions('quiet' => \$no_verbose_progress, # be quiet
           'help|?' => \$usage # help!
          );

# Print usage
if ($usage) {
	usage();
}

unless ($ARGV[0]) {
	print "Specify a CSV file name as the program's argument (e.g. qm.csv)\n";
	usage();
}

open (CSV, "< $ARGV[0]");

# Start writing table file, and write table header
open TABLE, "> atoz.html" or die $!;
print TABLE << "END";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- *** This file is auto-generated by $progname - edit $ARGV[0] instead -->
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="../../css/main2.css" />
<title>1623:204 Passage Names Glossary</title>
</head>

<body>

<h1>Steinbr&uuml;ckenh&ouml;hle Passage Names Glossary</h1>

<div style="text-align: center">
<a href="#a">A</a> -- <a href="#b">B</a> -- <a href="#c">C</a> -- 
<a href="#d">D</a> -- <a href="#e">E</a> -- <a href="#f">F</a> -- 
<a href="#g">G</a> -- <a href="#h">H</a> -- <a href="#i">I</a> -- 
<a href="#j">J</a> -- <a href="#k">K</a> -- <a href="#l">L</a> -- 
<a href="#m">M</a> -- <a href="#n">N</a> -- <a href="#o">O</a> -- 
<a href="#p">P</a> -- <a href="#q">Q</a> -- <a href="#r">R</a> -- 
<a href="#s">S</a> -- <a href="#t">T</a> -- <a href="#u">U</a> -- 
<a href="#v">V</a> -- <a href="#w">W</a> -- <a href="#x">X</a> -- 
<a href="#y">Y</a> -- <a href="#z">Z</a></div>

<hr />

END


<CSV>; # starting to read in csv file, eat header line

my ($name, $file, $fragment, $desc, $ety);
my $output="";
my $templine;
my $lcletter;
my $letter = "";
print "Progress: *";

my %areanames = 
("ariston.html", "Deep South",
 "entrance.html", "Near End",
 "midlevel.html", "Midlevel",
 "millennium.html", "Millennium",
 "nopain.html", "NPNG / Pleasuredome",
 "rhino.html", "Rhino",
 "swings.html", "Swings",
 "treeumphant.html", "Tree",
 "uworld.html", "Underworld");

# While loop which reads in each line of csv file
while (<CSV>) {
	chomp;
	# Split single line into all the fields
	($name, $file, $fragment, $desc, $ety) = &parse_csv($_);

	# Last field of CSV file can have weird form-feeds etc. Kill them
	$ety =~ s/\r//;

	if(uc(substr($name, 0, 1)) ne $letter)
	{
		$letter = uc(substr($name, 0, 1));
		$lcletter = lc($letter);
		$output = "$output<h3><a id=\"$lcletter\">$letter</a></h3>\n";
	}

	# Construct XHTML line
	$templine = "<dt><a href=\"$file#$fragment\">$name</a></dt>";

	$templine = "$templine\n<dd>[$areanames{$file}] $desc";

#	if ($ety)
#	{
#		$templine = "$templine\n<em>($ety)</em>";
#	}
	$templine = "$templine</dd>\n";
	
	$output = "$output$templine";
	
	print "*";
}

print TABLE << "END";
<dl>
$output
</dl>

<hr />
<ul>
<li><a href="204.html">Back to 204 index page</a></li>
<li><a href="../../smkridge/index.htm#id204">Schwarzmooskogel ridge area index and description</a></li>
<li><a href="../../indxal.htm#id204">Full Index</a></li>
<li><a href="../../areas.htm">Other Areas</a></li>
<li><a href="../../index.htm">Back to Expedition Intro page</a></li>
</ul>
</body>
</html>
END

close TABLE;
print "\nInformation: Done\n";

# Parse the CSV file
sub parse_csv {
	my $line = $_[0];
	my @parsedline = ();
	my $field = '';

	while ($line =~ m{ \G(?:^|,)
			(?: "((?> [^"]*) (?> "" [^"]*)*)" | ([^",]*)) }gx) {
		if ($2) {
			$field = $2;
		} elsif ($1) {
			$field = $1;
			$field =~ s/""/"/g;
		} else {
			$field = '';
		}

		push(@parsedline, $field);
	}

	return(@parsedline);
}

# Usage
sub usage {
	print << "EOF";
USAGE: $progname [-options] <CSV file>
	-q, --quiet         Be quiet about progress
	-h, --help          Show this message
EOF
	exit(0);
}
