#!/usr/bin/perl 
use File::Basename;
# parse TVZ Schedule .csv and add features from articles pictures posters Photos setlists etc
# derived from splitter.pl
#use List::MoreUtils qw(uniq);
my $art_ind = "";
my $art_name = "";
my $aitem = "";
my %date_done = "";
my $myline = "";
my %any = "";
my %arts = "";
my %artst = "";
my @alldates = "";
my @uniqdates = "";
my @featuredates = ""; # NEW 2017

sub uniq {
  my %seen;
  return grep { !$seen{$_}++ } @_;
}

# exclusion list specified by Harold Eggers  FIXME need to program this in:
# clncombined/Townes.jpg
# clncombined/1990-xx-xx-Kevin_Eggers-TvZ-Steve_Mendel-Harold_Eggers_at_Fire_Station_Studio-San_Marcos-TX.*
# clngigs/1991-xx-xx-Kevin_Eggers-TvZ-Steve_Mendel_and_Harold_Eggers_at_the_Fire_Station_Studio-San*
# clncombined/1994-xx-xx-Late-TvZ_.*
# clnpost/1994-xx-xx-NoDeeperTour.*
# clngigs/1994-11-01-Bergen-Norway_2.*
# clngigs/1994-12-02-Townes_met_Rick_Danko_and_Harold_Eggers_in_Italy.*
# clngigs/1995-02-10_McCabes-Santa_Monica-CA.*
# clngigs/1995-11-29_Loft-Berlin-Germany_with_Luc_Wouters.*
# clncombined/1996-xx-xx-TvZ_with_Stephan_and_Peggy_Nolan-Massachusetts.*
# clncombined/1996-xx-xx-Frankfurt-Germany.*
# clncombined/1996-xx-xx-Wolfgang_Pracht_and_TvZ.*
#while ($#ARGV >= 0) {
#my $file = shift(@ARGV);
#my $mainfile = "./tvztimeline-20190720.csv";
#my $mainfile = "./tvztimeline-20200820.csv";
my $mainfile = "./TVZ_timeline_2022_04_26.csv";

my $version = "12/30/2022 (partial update)";
my $articles =  "clncombined"; # now all items called articles; posters pics gpics arts gigs others
my $twidth = 250;
my $pwidth = 250;

my $othercount = 0;
my $postercount = 0;
my $piccount = 0;
my $gpiccount = 0;
my $artcount = 0;
my $gigcount = 0;

my $colorP = "#D4DDC0";
#my $colorP = "#D4D4C0";
#my $colorLE ="#F3E2AS";
my $colorLE ="#99CCFF";
#my $colorLE ="#43BFC7";
#my $colorO = "#C4CCC0";
my $colorO = "#FFF380";

my $count = "01";

my $contents = "./tvz60s.html";
# prepare table of contents file
open(CONT, "> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Townes Van Zandt Concert Appearances and Timeline</TITLE>
   <META NAME="AUTHOR" CONTENT="Hans, Len & Patrick">
   <META NAME="GENERATOR" CONTENT="VI">
   <META NAME="KEYWORDS" CONTENT="Townes Van Zandt, Tour Schedule, Appearance Schedule, Live in Concert">
</HEAD>
<BODY>
<H1>Townes Van Zandt Timeline & Appearance Schedule 1940s-1960s</H1>
<A HREF="clncombined/19xx-xx-xx-Townes_and_Hank_A_Miserable_Night_.jpg"><IMG SRC="clncombined/19xx-xx-xx-Townes_and_Hank_A_Miserable_Night_.t.jpg" width=200></A>
<A HREF="clncombined/1956-12-25_Townes_first_guitar_under_the_Christmas_tree.jpg"><IMG SRC="clncombined/1956-12-25_Townes_first_guitar_under_the_Christmas_tree.t.jpg" width=200></A>
<A HREF="clncombined/1969-11-26_TvZ_at_the_Carnegie_Hall-NY.jpg"><IMG SRC="clncombined/1969-11-26_TvZ_at_the_Carnegie_Hall-NY.t.jpg" width=200></A>
<A HREF="clncombined/1960-xx-xx-Barrington_HighSchool-Barrington-IL.jpg"><IMG SRC="clncombined/1960-xx-xx-Barrington_HighSchool-Barrington-IL.t.jpg" width=240></A>
<H2>with selected setlists, photos, posters, articles, and links</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>See also <A HREF=http://www.townesvanzandt20yearshfe.com/timeline/>Harold Eggers' 20 years with Townes Timeline</A></H4>
<H4>Jump to Townes in the <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A>, <A HREF=tvz90s.html>1990s</A>, or <A HREF=tvzmisc.html>Misc. undated</A>. Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>

<TABLE><TR>
<TD BGCOLOR="FFFFFF"><B>EVENT TYPE: </B></TD>
<TD BGCOLOR="$colorP"><B> P=Performance </B></TD>
<TD BGCOLOR="$colorLE"><B> LE=Life event </B></TD>
<TD BGCOLOR="$colorO"><B> O=Record releases & other career events</B></TD>
</TR></TABLE>

<TABLE BORDER=0>
<TR>
<TD style="width:10px"><B>Year</B></TD>
<TD style="width:10px"><B>Mon</B></TD>
<TD style="width:10px"><B>Day(s)</B></TD>
<TD style="width:10px"><B>Ty pe</B></TD>
<TD><B>Location</B></TD>
<TD><B>City</B></TD>
<TD><B>State /Country</B></TD>
<TD><B>Notes</B></TD>
<TD><B>Sources</B></TD>
<TD><B>Other source info</B></TD>
<TD><B></B></TD>
<TD><B>Certainty</B></TD>
<TD><B></B></TD></TR>
EOF
close CONT;

my $contents = "./tvz70s.html";
# prepare table of contents file
open(CONT, "> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Townes Van Zandt Concert Appearances and Timeline</TITLE>
   <META NAME="AUTHOR" CONTENT="Hans, Len & Patrick">
   <META NAME="GENERATOR" CONTENT="VI">
   <META NAME="KEYWORDS" CONTENT="Townes Van Zandt, Tour Schedule, Appearance Schedule, Live in Concert">
</HEAD>
<BODY>
<H1>Townes Van Zandt Timeline & Appearance Schedule 1970s</H1>
<A HREF="clncombined/1970-xx-xx-ties-Colorado.jpg"><IMG SRC="clncombined/1970-xx-xx-ties-Colorado.t.jpg" width=200></A>
<A HREF="clncombined/1970-xx-xx-05.jpg"><IMG SRC="clncombined/1970-xx-xx-05.t.jpg" width=200></A>
<A HREF="clncombined/19xx-xx-xx-Townes_with_Cindy_and_Bidy_Taylor.jpg"><IMG SRC="clncombined/19xx-xx-xx-Townes_with_Cindy_and_Bidy_Taylor.t.jpg" width=240></A>
<H2>with selected setlists, photos, posters, articles, and links</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>See also <A HREF=http://www.townesvanzandt20yearshfe.com/timeline/>Harold Eggers' 20 years with Townes Timeline</A></H4>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1960s</A>, <A HREF=tvz80s.html>1980s</A>, <A HREF=tvz90s.html>1990s</A>, or <A HREF=tvzmisc.html>Misc. undated</A>. Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>

<TABLE><TR>
<TD BGCOLOR="FFFFFF"><B>EVENT TYPE: </B></TD>
<TD BGCOLOR="$colorP"><B> P=Performance </B></TD>
<TD BGCOLOR="$colorLE"><B> LE=Life event </B></TD>
<TD BGCOLOR="$colorO"><B> O=Record releases & other career events</B></TD>
</TR></TABLE>

<TABLE BORDER=0>
<TR>
<TD style="width:10px"><B>Year</B></TD>
<TD style="width:10px"><B>Mon</B></TD>
<TD style="width:10px"><B>Day(s)</B></TD>
<TD style="width:10px"><B>Ty pe</B></TD>
<TD><B>Location</B></TD>
<TD><B>City</B></TD>
<TD><B>State /Country</B></TD>
<TD><B>Notes</B></TD>
<TD><B>Sources</B></TD>
<TD><B>Other source info</B></TD>
<TD><B></B></TD>
<TD><B>Certainty</B></TD>
<TD><B></B></TD></TR>
EOF
close CONT;






my $contents = "./tvz80s.html";
# prepare table of contents file
open(CONT, "> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Townes Van Zandt Concert Appearances and Timeline</TITLE>
   <META NAME="AUTHOR" CONTENT="Hans, Len & Patrick">
   <META NAME="GENERATOR" CONTENT="VI">
   <META NAME="KEYWORDS" CONTENT="Tour Schedule, Appearance Schedule, Live in Concert">
</HEAD>
<BODY>
<H1>Townes Van Zandt Timeline & Appearance Schedule 1980s</H1>
<A HREF="clncombined/1981-11-14_UALR_Fine_Arts_Concert_Hall-Little_Rock-AR.jpg"><IMG SRC="clncombined/1981-11-14_UALR_Fine_Arts_Concert_Hall-Little_Rock-AR.t.jpg" width=200></A>
<A HREF="clncombined/1983-03-14-Townes_and_Jeanene_weddingday.jpg"><IMG SRC="clncombined/1983-03-14-Townes_and_Jeanene_weddingday.t.jpg" width=200></A>
<H2>with selected setlists, photos, posters, articles, and links</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz90s.html>1990s</A>, or <A HREF=tvzmisc.html>Misc. undated</A>.  Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
<TABLE><TR>
<TD BGCOLOR="FFFFFF"><B>EVENT TYPE: </B></TD>
<TD BGCOLOR="$colorP"><B> P=Performance </B></TD>
<TD BGCOLOR="$colorLE"><B> LE=Life event </B></TD>
<TD BGCOLOR="$colorO"><B> O=Record releases & other career events</B></TD>
</TR></TABLE>

<TABLE BORDER=0>
<TR>
<TD style="width:10px"><B>Year</B></TD>
<TD style="width:10px"><B>Mon</B></TD>
<TD style="width:10px"><B>Day(s)</B></TD>
<TD style="width:10px"><B>Ty pe</B></TD>
<TD><B>Location</B></TD>
<TD><B>City</B></TD>
<TD><B>State /Country</B></TD>
<TD><B>Notes</B></TD>
<TD><B>Sources</B></TD>
<TD><B>Other source info</B></TD>
<TD><B></B></TD>
<TD><B>Certainty</B></TD>
<TD><B> </B></TD></TR>
EOF
close CONT;

my $contents = "./tvz90s.html";
# prepare table of contents file
open(CONT, "> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Townes Van Zandt Concert Appearances and Timeline</TITLE>
   <META NAME="AUTHOR" CONTENT="Hans, Len & Patrick">
   <META NAME="GENERATOR" CONTENT="VI">
   <META NAME="KEYWORDS" CONTENT="Tour Schedule, Appearance Schedule, Live in Concert">
</HEAD>
<BODY>
<H1>Townes Van Zandt Timeline & Appearance Schedule 1990s</H1>

<A HREF="clncombined/19xx-xx-xx-Unknown_Year-Townes_and_Guy_at_the_Fitzgerald-Houston-TX_backstage_with_David_Nowels.jpg"><IMG SRC="clncombined/19xx-xx-xx-Unknown_Year-Townes_and_Guy_at_the_Fitzgerald-Houston-TX_backstage_with_David_Nowels.t.jpg" width=240></A>
<A HREF="clncombined/19xx-xx-xx-With_Chris_Buhalis.jpg"><IMG SRC="clncombined/19xx-xx-xx-With_Chris_Buhalis.t.jpg" width=240></A>
<A HREF="clncombined/19xx-xx-xx-the_90ties_TvZ_and_Cowboy_Jack_Clement.jpg"><IMG SRC="clncombined/19xx-xx-xx-the_90ties_TvZ_and_Cowboy_Jack_Clement.t.jpg" width=240></A>

<H2>with selected setlists, photos, posters, articles, and links</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A>, or <A HREF=tvzmisc.html>Misc. undated</A>.  Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
<TABLE><TR>
<TD BGCOLOR="FFFFFF"><B>EVENT TYPE: </B></TD>
<TD BGCOLOR="$colorP"><B> P=Performance </B></TD>
<TD BGCOLOR="$colorLE"><B> LE=Life event </B></TD>
<TD BGCOLOR="$colorO"><B> O=Record releases & other career events</B></TD>
</TR></TABLE>

<TABLE BORDER=0>
<TR>
<TD style="width:10px"><B>Year</B></TD>
<TD style="width:10px"><B>Mon</B></TD>
<TD style="width:10px"><B>Day(s)</B></TD>
<TD style="width:10px"><B>Ty pe</B></TD>
<TD><B>Location</B></TD>
<TD><B>City</B></TD>
<TD><B>State /Country</B></TD>
<TD><B>Notes</B></TD>
<TD><B>Sources</B></TD>
<TD><B>Other source info</B></TD>
<TD><B></B></TD>
<TD><B>Certainty</B></TD>
<TD><B> </B></TD></TR>
EOF
close CONT;

# prepare table of contents file
my $contents = "./tvzmisc.html";
open(CONT, "> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Townes Van Zandt Timeline & Appearances</TITLE>
   <META NAME="AUTHOR" CONTENT="Hans, Len & Patrick">
   <META NAME="GENERATOR" CONTENT="VI">
   <META NAME="KEYWORDS" CONTENT="Tour Schedule, Appearance Schedule, Live in Concert">
</HEAD>
<BODY>
<H1>Townes Van Zandt Timeline & Appearance Schedule<B>with selected setlists, photos, posters, articles, and links</H1>
<H2>Miscellaneous Undated Materials</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A>, or <A HREF=tvz90s.html>1990s</A>.</H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
EOF
close CONT;


# fill hash from text file
$artcount = 0;
opendir(ADIR, "$articles") || die("cannot open $articles");
foreach $aitem (readdir(ADIR)) {
  next if $aitem =~ /\.t\./;
  next unless $aitem =~ /txt|pdf|jpg|png|gif|html|wav/i;
  $artcount++;
  if ($aitem =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.txt|\.pdf|\.jpg|\.png|\.gif\.html|.wav)/i) {
    ($art_i1,$art_i2,$art_i3,$art_name,$art_end) = $aitem =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.txt|\.pdf|\.jpg|\.png|\.gif\.htm)/i;
    $art_date = $art_i1 . "-" . $art_i2 . "-" . $art_i3;
    $sdate = $art_i1 . $art_i2 . $art_i3;
  }
  elsif ($aitem =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.txt|\.pdf|\.jpg|\.png|\.gif\.htm)/i) {
    ($art_i1,$art_i2,$art_name,$art_end) = $aitem =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.txt|\.pdf|\.jpg|\.png|\.gif\.html|\.wav)/i;
    $art_date = $art_i1 . "-" . $art_i2 . "-" . "xx";
    $sdate = $art_i1 . $art_i2 . "xx";
  } 
#  $art_ind = $art_date . "_" . $artcount;
#  $any{$art_ind} ++;

 $sdate =~ s/x/0/g;
print "SDATE: $sdate\n";
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;

  $art_ind = $art_date . "_" . $artcount;
  $any{$art_ind} ++;

  $aitemt = $art_date . $art_name . ".t" . ".jpg";
  $arts{$art_ind} = $aitem;
  $artst{$art_ind} = $aitemt;
  print "ARTICLE count:$artcount item:$aitem index:$art_ind name:$art_name end:$art_end 1:$art_i1 2:$art_i2 3:$art_i3 \n";
  #print "ARRAY: @uniqdates \n";
  #print "ARRAY: @sortdates \n";
}
close(ADIR);


################### Main Program to print out timeline dates and items ####################

my $row_count;
my $linecount = 0;
#exit;
# fill hash with MAIN labels and section text (db) filenames from text file
open(MDB,"<$mainfile") || die "cannot open $mainfile";
while (<MDB>) {
  $linecount++;
  if ($row_count) {
       $row_phase=0;
       $bgcolor = "$colorP";
   } else {
       $row_count=1;
       $bgcolor = '#FFFFFF';
   }
  chomp $_;
  #@Md = split(/,/,$_, 9999);
# $_ =~ s/\|\|\|\|\|\|\|\|\|\|\|/<HR>/; 
  next if ($_ =~ /^\s+$/);
  next if $linecount == 1;
  @Md = split(/\|/,$_, 9999);
   $yr   = $Md[0];
   $mon1 = $Md[1];
   $day1 = $Md[2];
   $type = $Md[3];
if ($type eq "O") {
       $bgcolor = "$colorO";
}
elsif ($type eq "LE") {
       $bgcolor = "$colorLE";
}
   $loc  = $Md[4];
   $city = $Md[5];
   $anotes=$Md[6];
#next if ($date =~ /199|198/);
#if ($mon1 == "spring/summer") { $mon1 = "spring or summer"; }
$mon = sprintf("%02d",$mon1);
# parse all dates here
  #@Md = split(/\|/,$_, 9999);
$days = $day1;
$days =~ s|early|xx|g;
$days =~ s|first week|xx|g;
$days =~ s|\(||g;
$days =~ s|\?||g;
$days =~ s|\)||g;
$days =~ s|\/|,|g;
$days =~ s|\*||g;
$days =~ s|\sor\s|,|g;
$days =~ s|\s\&\s|,|g;
my ($d1,$d2,$d3,$d4,$d5,$d6,$d7) = split(",",$days,999);
#print "$day1 $days D1 D2 D3 $d1 $d2 $d3\n";

$days = sprintf("%02d",$day1);
if ($mon == "xx" or $days == "xx") {
  my $date = $yr . "-" . $mon . "-" . $days; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK xx $date \n";
}
if ($mon == "00" or $days == "00") {
  my $date = $yr . "-" . $mon . "-" . $days; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK 00 $date \n";
}

if ($yr == "19xx" or $yr == "20xx") {
  my $date = $yr . "-" . "xx" . "-" . "xx"; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK 19xx added generic misc $date \n";
}


#print "reform: $mon $days $yr\n" if $yr =~ /197/;
#my $date = $yr . "-" . $mon . "-" . $days;
@featuredates = ""; #need to clear out
if ($d1) { 
$d1 = sprintf("%02d",$d1);
  my $date = $yr . "-" . $mon . "-" . $d1; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d1 $date \n";
}
if ($d2) { 
$d2 = sprintf("%02d",$d2);
  my $date = $yr . "-" . $mon . "-" . $d2; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d2 $date \n";
}
if ($d3) { 
$d3 = sprintf("%02d",$d3);
  my $date = $yr . "-" . $mon . "-" . $d3; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d3 $date \n";
}
if ($d4) { 
$d4 = sprintf("%02d",$d4);
  my $date = $yr . "-" . $mon . "-" . $d4; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d4 $date \n";
}
if ($d5) { 
$d5 = sprintf("%02d",$d5);
  my $date = $yr . "-" . $mon . "-" . $d5; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d5 $date \n";
}
if ($d6) { 
$d6 = sprintf("%02d",$d6);
  my $date = $yr . "-" . $mon . "-" . $d6; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d6 $date \n";
}
if ($d7) { 
$d7 = sprintf("%02d",$d7);
  my $date = $yr . "-" . $mon . "-" . $d7; 
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK $d7 $date \n";
}

# new force YYYY-xx-xx entries 
if ($yr =~ /\d\d\d\d/) {
  my $date = $yr . "-xx-xx";
  $any{$date} = $date;
  push @featuredates, $date;
  print "OK \d\d\d\d added generic misc $date \n";
# OK push in all months
  my $date = $yr . "-01-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-02-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-03-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-04-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-05-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-06-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-07-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-08-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-09-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-10-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-11-xx";
  $any{$date} = $date; push @featuredates, $date;
  my $date = $yr . "-12-xx";
  $any{$date} = $date; push @featuredates, $date;
}
#my $dateb = $yr . "-" . $mon . "-" . $days . "2";
#my $datec = $yr . "-" . $mon . "-" . $days . "23";
#my $dated = $yr . "-" . $mon . "-" . $days . "234";
#my $datee = $yr . "-" . $mon . "-" . $days . "2345";
#my $datef = $yr . "-" . $mon . "-" . $days . "23456";
#my $dateg = $yr . "-" . $mon . "-" . $days . "234567";
#my $dateh = $yr . "-" . $mon . "-" . $days . "2345678";
#my $datei = $yr . "-" . $mon . "-" . $days . "23456789";
#my $datej = $yr . "-" . $mon . "-" . $days . "23456789a";

if ($days =~ /../) {
#print "$_ article found: $arts{$date} \n" ;
 
$myline = $_;
$myline =~ s/^year/<b>year/;
$myline =~ s/spring\/summer/spring or summer/;
$myline =~ s|8/9/10/11|8/9 /10/11|;
$myline =~ s|23/24/25/26|23/24 /25/26|;
$myline =~ s|1/2/3/4/5/6/7/8/9/10/11/12/13|1/2/3/4 /5/6/7/8 /9/10/11 /12/13|;
$myline =~ s|5/6/7/8/9/10/11/12|5/6/7/8 /9/10 /11/12|;
$myline =~ s|5/6/7/8/9/10|5/6/7/8 /9/10|;
$myline =~ s|22/23/24/25|22/23 /24/25|;
$myline =~ s|26/27/28/29|26/27 /28/29|;
$myline =~ s|27/28/29|27 /28/29|;
$myline =~ s|19/20/21/22|19/20 /21/22|;
$myline =~ s|19/20/21|19/20 /21|;
$myline =~ s|Washington/Georgetown|Wash DC /Georgetown|;
$myline =~ s|Roslyn/Long Island|Roslyn /Long Island|;
$myline =~ s|(North) San Antonio|N. San Antonio|;
$myline =~ s|Millerville/Lancaster|Millerville /Lancaster|;
$myline =~ s|Los Angeles/Hollywood|Los Angeles /Hollywood|;
$myline =~ s|March-|Mar |;
$myline =~ s|June-|Jun |;
$myline =~ s|North.Island/UK|North Island/UK|;
$myline =~ s|/UK| /UK|;
$myline =~ s|middle/late|middle /late|;
$myline =~ s|1963/1964|1963 /1964|;
$myline =~ s/notes$/notes<\/b>/;
#$myline =~ s/\|\|\|\|\|\|\|\|\|\|\|/<TR><TD COLSPAN=13><HR COLOR=green><\/TD><\/TR>/; 
$myline =~ s/\|/<\/TD><TD>/g;
$myline =~ s/^/<TR BGCOLOR=$bgcolor><TD>/;
$myline =~ s/$/<\/TD><\/TR>/;
#print STDERR "year:$yr ";

close CONT;
if ($yr =~ /194|195|196/) {
  $contents = "./tvz60s.html";
}
elsif ($yr =~ /197/) {
  $contents = "./tvz70s.html";
}
elsif ($yr =~ /198/) {
  $contents = "./tvz80s.html";
}
elsif ($yr =~ /199/) {
  $contents = "./tvz90s.html";
}
elsif ($yr =~ /19x|20x/) { # leave the same
  $contents = "./tvzdump.html";
  $any{$date} = $date;
}
else { # leave the same
  $contents = "./tvzmisc.html";
}
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT "$myline\n" unless $myline =~ m{<TR BGCOLOR=#D4D4C0><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD>};

###################### now add features content
# check and print features if 1st time date match 
foreach $date (@featuredates) {
next unless (defined $any{$date}); 

# eliminate multiple showings here?
next if ($date_done{$date} == 1);
  # add all content
my $piccnt = 0;
print CONT "\n<TR><TD COLSPAN=13><TABLE><TR> \n";
for (my $i=1; $i<=$artcount; $i++) {
  $dateind = $date . "_" . $i; 
  if (-f "$articles/$arts{$dateind}") {
    $piccnt++;
    my $resourcename = $arts{$dateind};
    $resourcename =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif|\.html|\.pdf|\.txt)/i;
    my ($art_i1,$art_i2,$art_i3,$art_name,$art_end) = ($1,$2,$3,$4,$5);
my $desc2 = "unk file";
if ($art_end =~ /txt/) { 
  $desc2 = " (txt)" 
}
elsif ($art_end =~ /pdf/) { 
  $desc2 = " (pdf)" 
}
    my $descrip = $art_i1 . "-" . $art_i2 . "-" . $art_i3 . "_" . $art_name;
    $descrip =~ s/_/ /g;
    if ($piccnt % 3 == 0) { print CONT "</TR><TR>\n"; }
    if (-f "$articles/$artst{$dateind}") {
      print CONT " <TD>$piccnt <A HREF=\"$articles/$arts{$dateind}\"><IMG SRC=\"$articles/$artst{$dateind}\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$artcnt<A HREF=\"$articles/$arts{$dateind}\">$descrip $desc2</A></TD>\n";
    }
  }
} 
print CONT "</TR></TABLE></TD></TR> \n";
$date_done{$date} = 1; # OK mark this date as done
}

print CONT "\n"; 

}
else {
print CONT "$_\n";
}
close CONT;
} # this is main loop
close(MDB);

# add loop for 19xx- content that did not match spreadsheet into tvzmisc
$mcnt = 0;
$contents = "./tvzmisc.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT "<TABLE><TR>\n"; 
opendir(ADIR, "$articles") || die("cannot open $articles");
foreach $miscitem (readdir(ADIR)) {
  next if $miscitem =~ /t.jpg/;
  next unless $miscitem =~ /19xx/;
  next unless $miscitem =~ /\.jpg|\.png|\.pdf/;
  $mcnt++;
  $miscitem =~ /(\d\dxx-xx-xx)(.*)(\.jpg|\.png|\.pdf)/i;
  my ($m1,$m_name,$m_end) = ($1,$2,$3);
  my $miscthumb = $m1 . $m_name . ".t.jpg";
print "$mcnt:    $miscitem and $miscthumb\n";
  my $descrip = $m_name;
  $descrip =~ s/_/ /g;
  $descrip =~ s/-/ /g;
  if ($mcnt % 3 == 0) { print CONT "</TR><TR>\n"; }
  if (-f "$articles/$miscthumb") {
      print CONT " <TD>$mcnt <A HREF=\"$articles/$miscitem\"><IMG SRC=\"$articles/$miscthumb\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$mcnt <A HREF=\"$articles/$miscitem\">$descrip</A></TD>\n";
    }
  }
print CONT "</TR><TR>\n"; 
close CONT;
close ADIR;

# close off files
my $contents = "./tvz60s.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A>, <A HREF=tvz90s.html>1990s</A> or <A HREF=tvzmisc.html>Misc. undated</A></H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
</body>
</html>
EOF
close(CONT);

my $contents = "./tvz70s.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz80s.html>1980s</A>, <A HREF=tvz90s.html>1990s</A> or <A HREF=tvzmisc.html>Misc. undated</A></H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
</body>
</html>
EOF
close(CONT);

my $contents = "./tvz80s.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz90s.html>1990s</A> or <A HREF=tvzmisc.html>Misc. undated</A></H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
</body>
</html>
EOF
close(CONT);

my $contents = "./tvz90s.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A> or <A HREF=tvzmisc.html>Misc. undated</A></H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
</body>
</html>
EOF
close(CONT);

my $contents = "./tvzmisc.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <A HREF=tvz60s.html>1940s-60s</A>, <A HREF=tvz70s.html>1970s</A>, <A HREF=tvz80s.html>1980s</A> or <A HREF=tvz90s.html>1990s</A></H4>
<H4>Back to <A HREF=/coopl/tvzindex.html>Blue Sky Home Page</A></H4>
</body>
</html>
EOF
close(CONT);


