#!/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 $set_ind = "";
my $set_name = "";
my $pos_ind = "";
my $pos_name = "";
my $gpic_ind = "";
my $gpic_name = "";
my $oth_ind = "";
my $oth_name = "";
my $end = "";
my $item = "";
my $oitem = "";
my $gitem = "";
my $gitemt = "";
my %date_done = "";
my $myline = "";
my %any = "";
my %sets = "";
my %arts = "";
my %artst = "";
my %pics = "";
my %picst = "";
my %poss = "";
my %posst = "";
my %gpics = "";
my %gpicst = "";
my %oth = "";
my %otht = "";
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:
# clnpics/Townes.jpg
# clnpics/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*
# clnpics/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.*
# clnpics/1996-xx-xx-TvZ_with_Stephan_and_Peggy_Nolan-Massachusetts.*
# clnpics/1996-xx-xx-Frankfurt-Germany.*
# clnpics/1996-xx-xx-Wolfgang_Pracht_and_TvZ.*
#while ($#ARGV >= 0) {
#my $file = shift(@ARGV);
#my $mainfile = "./TvZ -Timeline 2015-03-31b.csv";
#my $mainfile = "./tvztimeline-20160407.csv";
#TvZ-Timeline_Appearances_schedule-2017-06-30.csv
#my $mainfile = "./tvztimeline-20170630.csv";
my $mainfile = "./TVZ_Timeline_2018_07_21.csv";
my $version = "8/03/18";
#my $articles = "TvZ articles";
#my $postures = "TvZ pictures";
#my $gpictures = "TvZ Photos made @ Gigs";
#my $articles =  "articles";
my $articles =  "clnarts";
#my $postures =  "pictures";
my $pictures =  "clnpics";
my $gpictures = "clngigs";
#my $posters =   "posters";
my $posters =   "clnpost";
my $setlists =  "clnset";
#my $others =    "others";
my $others =     "clnoth";
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";
#print "file: $file\n";
#my ($base,$dir,$ext) = fileparse($file,'\..*');

my $count = "01";


my $contents = "./tvz60s70s.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>
<H2>Townes Van Zandt Timeline & Appearance Schedule 1940s-1970s</H2>
<A HREF="clnpics/19xx-xx-xx-Townes_with_his_cousin_brownie_in_Kerrville.jpg"><IMG SRC="clnpics/19xx-xx-xx-Townes_with_his_cousin_brownie_in_Kerrville.t.jpg" width=200></A>
<A HREF="clnpics/19xx-xx-xx-Early_70ties-Colorado.jpg"><IMG SRC="clnpics/19xx-xx-xx-Early_70ties-Colorado.t.jpg" width=200></A>
<A HREF="clnpics/19xx-xx-xx-Townes_Archive_Photo-Townes_in_army.jpg"><IMG SRC="clnpics/19xx-xx-xx-Townes_Archive_Photo-Townes_in_army.t.jpg" width=200></A>
<A HREF="clnpics/19xx-xx-xx-Townes_with_Cindy_and_Bidy_Taylor.jpg"><IMG SRC="clnpics/19xx-xx-xx-Townes_with_Cindy_and_Bidy_Taylor.t.jpg" width=240></A>
<H3>with selected setlists, photos, posters, articles, and links</H3>
<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=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>
<H2>Townes Van Zandt Timeline & Appearance Schedule 1980s</H2>
<A HREF="clnpics/19xx-xx-xx-Townes_and_Mickey_White.jpg"><IMG SRC="clnpics/19xx-xx-xx-Townes_and_Mickey_White.t.jpg" width=200></A>
<A HREF="clnpics/19xx-xx-xx-Townes_and_Mickey_White-Rare_Picture-Mickey_at_Piano.jpg"><IMG SRC="clnpics/19xx-xx-xx-Townes_and_Mickey_White-Rare_Picture-Mickey_at_Piano.t.jpg" width=200></A>
<H3>with selected setlists, photos, posters, articles, and links</H3>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s70s.html>1940s-70s</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>
<H2>Townes Van Zandt Timeline & Appearance Schedule 1990s</H2>

<A HREF="clnpics/19xx-xx-xx-TvZ_his_Hobby-Sailing.jpg"><IMG SRC="clnpics/19xx-xx-xx-TvZ_his_Hobby-Sailing.t.jpg" width=240></A>
<A HREF="clnpics/1996-xx-xx-TvZ.jpg"><IMG SRC="clnpics/1996-xx-xx-TvZ.t.jpg" width=240></A>

<H3>with selected setlists, photos, posters, articles, and links</H3>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s70s.html>1940s-70s</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>
<H2>Townes Van Zandt Timeline & Appearance Schedule<B>with selected setlists, photos, posters, articles, and links</H2>
<H2>Miscellaneous Undated Materials</H2>
<H4>Version $version by Hans & Len with assistance from Patrick</H4>
<H4>Jump to Townes in the <A HREF=tvz60s70s.html>1940s-70s</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>
<TABLE BORDER=0>
<TR>
<TD><B>Year</B></TD>
<TD><B>Mon</B></TD>
<TD><B>Day(s)</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>Links</B></TD>
<TD><B>Certainty</B></TD>
<TD><B> </B></TD></TR>
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|htm/i;
  $artcount++;
  if ($aitem =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.txt|\.pdf|\.jpg|\.png|\.gif\.htm)/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\.htm)/i;
    $art_date = $art_i1 . "-" . $art_i2 . "-" . "xx";
    $sdate = $art_i1 . $art_i2 . "xx";
  } 
  $art_ind = $art_date . "_" . $artcount;
  $any{$art_ind} ++;

 #$sdate = $art_i1 . $art_i2 . $art_i3;
 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;

  $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);


# fill hash from text file
opendir(SDIR, "$setlists") || die("cannot open $setlists");
foreach $item (readdir(SDIR)) {
  #print "$item\n";
  next unless $item =~ /txt|doc|jpg/i;
  ($set_i1,$set_i2,$set_i3,$set_name,$set_end) = $item =~ /(\d+)-(\d+|xx)-(\d+|xx)(.*)(\.txt|\.doc|\.docx|\.jpg)/i;
  $set_ind = $set_i1 . "-" . $set_i2 . "-" . $set_i3;
  #$setemt = $set_ind . $set_name . ".t" . $set_end;
  #($set_ind,$set_name,$end) = $item =~ /(\d+-\d+-\d+)(.*)(\.\w+)/;
  #next if ($art_ind =~ /199|198/);
 $sdate = $set_i1 . $set_i2 . $set_i3;
 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;
  $sets{$set_ind} = $item;
  $any{$set_ind} ++;
  #print "$item:$set_ind:$set_name :$set_end\n";
}
close(SDIR);
#exit;


# fill pic hash from text file
$piccount = 0;
opendir(PDIR, "$pictures") || die("cannot open $pictures");
foreach $item (readdir(PDIR)) {
  next if $item =~ /\.t\./;
  next unless $item =~ /jpg|png|gif|htm/i;
  $piccount++;
  if ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3;
    $sdate = $pic_i1 . $pic_i2 . $pic_i3;
  }
  elsif ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . "xx";
    $sdate = $pic_i1 . $pic_i2 . "xx";
  } 
 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;
  $pic_ind = $pic_date . "_" . $piccount;
  $any{$pic_ind} ++;
  $itemt = $pic_date . $pic_name . ".t" . ".jpg";
  $pics{$pic_ind} = $item;
  $picst{$pic_ind} = $itemt;
  #print "PICTURE count:$piccount item:$item index:$pic_ind name:$pic_name end:$pic_end 1:$pic_i1 2:$pic_i2 3:$pic_i3 \n";
#  print "ARRAY: @uniqdates \n";
#  print "ARRAY: @sortdates \n";
}
close(PDIR);

# fill gig pic hash from text file
$gpiccount = 0;
opendir(GDIR, "$gpictures") || die("cannot open $gpictures");
foreach $item (readdir(GDIR)) {
  next if $item =~ /\.t\./;
  next unless $item =~ /jpg|png|gif|htm/i;
  $gpiccount++;
#print "$gpiccount \n";
  if ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3;
    $sdate = $pic_i1 . $pic_i2 . $pic_i3;
  }
  elsif ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . "xx";
    $sdate = $pic_i1 . $pic_i2 . "xx";
  } 

 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;
  $pic_ind = $pic_date . "_" . $gpiccount;
  $any{$pic_ind} ++;
  $itemt = $pic_date . $pic_name . ".t" . ".jpg";
  $gpics{$pic_ind} = $item;
  $gpicst{$pic_ind} = $itemt;
  print "\n\n GPICTURE count:$gpiccount item:$item index:$pic_ind name:$pic_name end:$pic_end 1:$pic_i1 2:$pic_i2 3:$pic_i3 \n";
  #print "ARRAY: @uniqdates \n";
  #print "ARRAY: @sortdates \n";
}
close(GDIR);



# fill hash from text file
$postercount = 0;
opendir(PDIR, "$posters") || die("cannot open $posters");
foreach $item (readdir(PDIR)) {
  next if $item =~ /\.t\./;
  next unless $item =~ /jpg|png|gif|htm/i;
  $postercount++;
  if ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3;
    $sdate = $pic_i1 . $pic_i2 . $pic_i3;
  }
  elsif ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . "xx";
    $sdate = $pic_i1 . $pic_i2 . "xx";
  } 
 #$sdate = $pic_i1 . $pic_i2 . $pic_i3;
 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;
  $pic_ind = $pic_date . "_" . $postercount;
  $any{$pic_ind} ++;
  $itemt = $pic_date . $pic_name . ".t" . ".jpg";
  $post{$pic_ind} = $item;
  $postt{$pic_ind} = $itemt;
  #print "POSTER count:$postercount item:$item index:$pic_ind name:$pic_name end:$pic_end 1:$pic_i1 2:$pic_i2 3:$pic_i3 \n";
  #print "ARRAY: @uniqdates \n";
  #print "ARRAY: @sortdates \n";
}
close(PDIR);



# fill hash from text file
$othercount = 0;
opendir(ODIR, "$others") || die("cannot open $others");
foreach $item (readdir(ODIR)) {
  next if $item =~ /\.t\./;
  next unless $item =~ /jpg|png|gif|htm/i;
  $othercount++;
  if ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3;
 $sdate = $pic_i1 . $pic_i2 . $pic_i3;
  }
  elsif ($item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i) {
    ($pic_i1,$pic_i2,$pic_name,$pic_end) = $item =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    $pic_date = $pic_i1 . "-" . $pic_i2 . "-" . "xx";
    $sdate = $pic_i1 . $pic_i2 . "xx";
  } 
 #$sdate = $pic_i1 . $pic_i2 . $pic_i3;
 $sdate =~ s/x/0/g;
 push @alldates, $sdate; 
 @uniqdates = uniq @alldates;
 @sortdates = sort @uniqdates;
  $pic_ind = $pic_date . "_" . $othercount;
  $any{$pic_ind} ++;
  $itemt = $pic_date . $pic_name . ".t" . ".jpg";
  $oth{$pic_ind} = $item;
  $otht{$pic_ind} = $itemt;
  #print "OTHER count:$othercount item:$item index:$pic_ind name:$pic_name end:$pic_end 1:$pic_i1 2:$pic_i2 3:$pic_i3 \n";
  #print "ARRAY: @uniqdates \n";
  #print "ARRAY: @sortdates \n";
}
close(ODIR);


################### 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 == "00") { $mon = "xx"; }
#print "orig:   $mon1 $day1 $yr REV: $mon $days $_\n" if $yr =~ /1969/;
#if ($days == "00") { $days = "xx"; }
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";
}
#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|197/) {
  $contents = "./tvz60s70s.html";
}
elsif ($yr =~ /198/) {
  $contents = "./tvz80s.html";
}
elsif ($yr =~ /199/) {
  $contents = "./tvz90s.html";
}
elsif ($yr =~ /19x|20x/) { # leave the same
  $contents = "./tvzmisc.html";
  $any{$date} = $date;
}
else { # leave the same
  $contents = "./tvzmisc.html";
}
#print STDERR "$contents \n";
open(CONT, ">> $contents") or die "cant open $contents: $!";

#print CONT "$_\n";
#print CONT "$myline\n";
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) {
#foreach $aitem (readdir(ADIR)) {

#unless (defined $date_done{$date}) {
next unless (defined $any{$date}); 
#print CONT "<TR><TD COLSPAN=12>found:$any{$date} items</TD></TR>";
print CONT "<TR><TD COLSPAN=13>";
  #insert articles that may be pictures with thumbnails or just .txt or .html etc
  if (-f "$articles/$artst{$date}") {
    print CONT " article(s): <A HREF=\"$articles/$arts{$date}\" target=\"_tvz_all\"><IMG SRC=\"$articles/$artst{$date}\" width=$twidth></A>";
    }
  elsif (-f "$articles/$arts{$date}") {
     my $displ = $arts{$date};
     $displ =~ s/_/ /g;
     print CONT "article(s): <A HREF=\"$articles/$arts{$date}\" target=\"_tvz_all\">$displ</A>";
    }



if (-f "$setlists/$sets{$date}") {
   my $displ = $sets{$date};
   $displ =~ s/_/ /g;
   print CONT " setlist: <A HREF=\"$setlists/$sets{$date}\" target=\"_tvz_all\">$displ</A>\n" 
}


  # add pictures
my $piccnt = 0;
print CONT "\n<TABLE><TR COLSPAN=3> \n";
for (my $i=1; $i<=$piccount; $i++) {
  $dateind = $date . "_" . $i; 
  if (-f "$pictures/$pics{$dateind}") {
    $piccnt++;
    my $resourcename = $pics{$dateind};
    $resourcename =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    my ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = ($1,$2,$3,$4,$5);
    my $descrip = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3 . "_" . $pic_name;
    $descrip =~ s/_/ /g;
    if ($piccnt % 3 == 0) { print CONT "</TR><TR>\n"; }
    #print "PICS CNT:$piccnt file:$pic_i1,$pic_i2,$pic_i3,$pic_name, $resourcename DESC:$descrip DIR:$contents date:$date piccount: $piccount i:$i ind:$dateind item:$pictures/$pics{$dateind} itemt:$pictures/$picst{$dateind} \n";
    if (-f "$pictures/$picst{$dateind}") {
      print CONT " <TD>$piccnt:<A HREF=\"$pictures/$pics{$dateind}\"><IMG SRC=\"$pictures/$picst{$dateind}\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$piccnt:<A HREF=\"$pictures/$pics{$dateind}\">$pictures/$pics{$dateind}</A><BR>$descrip</TD>\n>";
    }
  }
} 
print CONT "</TR></TABLE> \n";

  # add gig pictures
my $gpiccnt = 0;
print CONT "\n<TABLE><TR COLSPAN=3> \n";
for (my $i=1; $i<=$gpiccount; $i++) {
  $dateind = $date . "_" . $i; 
  if (-f "$gpictures/$gpics{$dateind}") {
    $gpiccnt++;
    my $resourcename = $gpics{$dateind};
    $resourcename =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    my ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = ($1,$2,$3,$4,$5);
    #my $descrip = $pic_name;
    my $descrip = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3 . "_" . $pic_name;
    $descrip =~ s/_/ /g;
    if ($gpiccnt % 3 == 0) { print CONT "</TR><TR>\n"; }
    print "GPICS CNT:$gpiccnt FILE:$resourcename IND:$dateind ITEM:$gpictures/$gpics{$dateind} \n";
    if (-f "$gpictures/$gpicst{$dateind}") {
      print CONT " <TD>$gpiccnt:<A HREF=\"$gpictures/$gpics{$dateind}\"><IMG SRC=\"$gpictures/$gpicst{$dateind}\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$gpiccnt:<A HREF=\"$gpictures/$gpics{$dateind}\">$gpictures/$gpics{$dateind}</A><BR>$descrip</TD>\n>";
    }
  }
} 
print CONT "</TR></TABLE> \n";





  # add  posters
# add poster content
my $postcnt = 0;
#print CONT "<HR COLOR=green>\n<TABLE><TR COLSPAN=3> \n";
print CONT "\n<TABLE><TR COLSPAN=3> \n";
for (my $i=1; $i<=$postercount; $i++) {
  $dateind = $date . "_" . $i; 
  if (-f "$posters/$post{$dateind}") {
    $postcnt++;
    my $resourcename = $post{$dateind};
    $resourcename =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    my ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = ($1,$2,$3,$4,$5);
    $pic_name =~ s/-/ /g;
    my $descrip = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3 . "_" . $pic_name;
    $descrip =~ s/_/ /g;
    if ($postcnt % 3 == 0) { print CONT "</TR><TR>\n"; }
    #print "CNT:$postcnt file:$pic_i1,$pic_i2,$pic_i3,$pic_name, $resourcename DESC:$descrip DIR:$contents date:$date postercount: $postercount i:$i ind:$dateind item:$posters/$post{$dateind} itemt:$posters/$postt{$dateind} \n";
    if (-f "$posters/$postt{$dateind}") {
      print CONT " <TD>$postcnt:<A HREF=\"$posters/$post{$dateind}\"><IMG SRC=\"$posters/$postt{$dateind}\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$othcnt:<A HREF=\"$posters/$post{$dateind}\">$posters/$post{$dateind}</A><BR>$descrip</TD>\n>";
    }
  }
} 
print CONT "</TR></TABLE> \n";



# add other content
my $othcnt = 0;
#print CONT "<HR COLOR=green>\n<TABLE><TR COLSPAN=3> \n";
print CONT "\n<TABLE><TR COLSPAN=3> \n";
for (my $i=1; $i<=$othercount; $i++) {
  $dateind = $date . "_" . $i; 
  if (-f "$others/$oth{$dateind}") {
    $othcnt++;
    my $resourcename = $oth{$dateind};
    $resourcename =~ /(\d\d\d\d|\d\dxx)-(\d+|xx)-(\d+|xx)(.*)(\.jpg|\.png|\.gif\.htm)/i;
    my ($pic_i1,$pic_i2,$pic_i3,$pic_name,$pic_end) = ($1,$2,$3,$4,$5);
    my $descrip = $pic_i1 . "-" . $pic_i2 . "-" . $pic_i3 . "_" . $pic_name;
    $descrip =~ s/_/ /g;
    if ($othcnt % 3 == 0) {
      print CONT "</TR><TR>\n";
    }
    #print "CNT:$othcnt file:$pic_i1,$pic_i2,$pic_i3,$pic_name, $resourcename DESC:$descrip DIR:$contents date:$date othercount: $othercount i:$i ind:$dateind item:$others/$oth{$dateind} itemt:$others/$otht{$dateind} \n";
    if (-f "$others/$otht{$dateind}") {
      print CONT " <TD>$othcnt:<A HREF=\"$others/$oth{$dateind}\"><IMG SRC=\"$others/$otht{$dateind}\" alt=\"$descrip\" align=top width=\"$twidth\"></A><BR>&nbsp;&nbsp;&nbsp;$descrip</TD>\n";
    } else {
      print CONT " <TD>$othcnt:<A HREF=\"$others/$oth{$dateind}\">$others/$oth{$dateind}</A><BR>$descrip</TD>\n>";
    }
  }
} 
print CONT "</TR></TABLE> \n";


#print CONT "$any{$date} items<HR WIDTH=900 COLOR=grey></TD></TR>";
#print CONT "<HR WIDTH=900 COLOR=grey></TD></TR>";
$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);


my $contents = "./tvz60s70s.html";
open(CONT, ">> $contents") or die "cant open $contents: $!";
print CONT <<EOF;
</TABLE>
<H4>Jump to Townes in the <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=tvz60s70s.html>1940s-70s</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=tvz60s70s.html>1940s-70s</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=tvz60s70s.html>1940s-70s</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);


