#!/usr/bin/perl -w
use CGI qw(:all);

#open categories file for reading
open(CATG,"anim_categories.csv")|| die "cannot open categories list";

#open TOC html file for writing/and write the top stuff
open(TOC,">PATOC.html")|| die "cannot open PATOC.html file for writing";
print TOC  start_html(-title=>' ', -bgcolor=>'#000099', -text=>'#FFFFEE', 
    -link=>'#EEEEFF', vlink=>'#ffffcc', alink=>'#ccffcc');   
print TOC <<TOP_O_TOC;

<DL>
<DT>
<strong>Categories</strong>
<font size=-1>
TOP_O_TOC
 
#read and discard first TOC line, that's just header/descriptors
$line = <CATG>;
#for each category file line
while($line=<CATG>){
    #parse categories line
    @fld = split(/","/,$line);
        #field              field 2 indexin
        #field 0 category   field 3 indexout
        #field 1 directory  field 4 title
    $fld[0] =~ s/"//;
    $fld[4] =~ s/"//;
    
    #write TOC entry to TOC html file
    $TOClink = $fld[1]."/".$fld[3];
    $TOCdata = $fld[1]."/".$fld[2];
    print TOC "<dt>";
    print TOC a({-href=>$TOClink, -target=>"main"},$fld[0]);
    print TOC "\n";
    #Process each TOC category file SUB EACHCAT 
    #GOSUB EACHCAT(indexin, indexout,directory,title)
     eachcat($TOCdata,$TOClink,$fld[1],$fld[4]);
}

#finish and close TOC html file
print TOC <<END_O_TOC;
<dt><A HREF="student" TARGET="main">Student</A>
<dt><A HREF="Phys_anim_credits.html" TARGET="main">Credits</A>
</DL>
</font>
END_O_TOC
print TOC end_html();
close(TOC);

#close categories file
close(CATG);



#SUB process list of directories and export to TOC html file
#EACHCAT(indexin, indexout,directory,title)
sub eachcat{  
    $infile=$_[0];
    $outfile=$_[1];
    $dir=$_[2];
    $title=$_[3];
    #open subcategory data file
    open(IND_IN,$infile)|| die "cannot open $_[0] for reading";
    #read 'n drop first line, its just column headers
    $iline=<IND_IN>;

    #open subcategory html file for writing
    open(IND_OUT,">".$outfile)|| die "cannot open TOC file for writing";

    #write top of subcategory html file
    print IND_OUT  start_html(-title=>$fld[4], -bgcolor=>'#000099', -text=>'#FFFFEE', 
        -link=>'#EEEEFF', vlink=>'#ffffcc', alink=>'#ccffcc');   
    print IND_OUT <<TOP_O_THIS_IND;
    <H2 ALIGN=CENTER>$fld[4]</H2>
    <TABLE  WIDTH=100% BORDER=1 CELLPADDING=2 CELLSPACING=0 >
TOP_O_THIS_IND

    #dummy first subcat
    $thissubcat="zzz";
    #for each subcategory file line
    while($iline=<IND_IN>){
        #parse item line
        #$itemfld[0 through 5] = ID  subcategory    media   thumb   comment html
        @itemfld = split(/"?,"?/,$iline);
        #if its a new category, write the category header
        if($itemfld[1] ne $thissubcat){
            $thissubcat=$itemfld[1];
            print IND_OUT <<SUBCAT_THIS_IND;
            <TR>
                <TD COLSPAN=2 WIDTH=100%>
                    <P ALIGN=CENTER><font size=+1>$thissubcat</font></P>
                </TD>
            </TR>
SUBCAT_THIS_IND
        }
        #write the component table row
            $em1="embeder".$itemfld[0].".html";
            $em2="embederQ".$itemfld[0].".html";
            print IND_OUT <<ITEM_ROW_THIS_IND1;        
            <TR>
                <TD WIDTH=29%>
                    <P ALIGN=CENTER>
                    <a name=$itemfld[0] >
                    <IMG SRC="$itemfld[3]"  ALIGN=BOTTOM  BORDER=0>
                    </a>
                    </P>
            </TD>
                <TD WIDTH=71% VALIGN=TOP>
                    <P> $itemfld[4] </P>
                    <P><font size=-2>
ITEM_ROW_THIS_IND1
            if($itemfld[5] ){ 
                chomp($itemfld[5]);
                $itemfld[5] =~ s/"//;
                if($itemfld[5] ne ""){
                #print IND_OUT "| <A HREF= $itemfld[5] TARGET="_new"> HTML</A> |";
                print IND_OUT a({-href=>$itemfld[5], -target=>"_new"},"HTML");

                }
            }
            print IND_OUT <<ITEM_ROW_THIS_IND2;        
                    <A HREF="$em1" TARGET="_new">
                    Embedded</A> | 
                    <A HREF="$em2" TARGET="_new">
                    QT Embedded</A> | 
                    <A HREF="$itemfld[2]" TARGET="_new">
                    Media </A> |
                    </font></P>
                </TD>
            </TR>
ITEM_ROW_THIS_IND2
        
        
        #write the various html files for the mediafiles
        #write embedder1(directory,embedding html file,media file, comment)
         embedder1($dir,$em1,$itemfld[2],$itemfld[4]);
         embedder2($dir,$em2,$itemfld[2],$itemfld[4]);
        
     }

    #finish off and close this  indexer file
    print IND_OUT <<END_O_THIS_IND;
    </TABLE>
END_O_THIS_IND
    print IND_OUT end_html();
    close(IND_OUT);
    
    #close the subcategory data file 
    close(IND_IN);
}

#embedder1(directory,embedding html file,media file, comment)
sub embedder1{
    $dir=$_[0];
    $outfile=$_[1];
    $media=$_[2];
    $comment=$_[3];
    #print "$dir $outfile  \n";
    #open embedder html file
    $fname=$_[0]."/".$_[1];
    open(EMBD,">".$fname)|| die "cannot open $fname for writing";
    #write embedder html file
    print EMBD <<ALL_OF_EMBD;
        <html>
        <head>
        <title> embedded media file </title>
        </head>
        <body link="#EEEEFF" alink="#ccffcc" vlink="#ffffcc" bgcolor="#000099" text="#FFFFEE">
            <center><table>
            <tr><td>
                <center><embed src="$media" loop=-1 ></center>
            </td></tr>
            <tr><td><center>$comment </center></td></tr>
            </table></center>
        </body>
ALL_OF_EMBD

    #close embedder html file
   close(EMBD);
}

#embedder1(directory,embedding html file,media file, comment)
sub embedder2{
    $dir=$_[0];
    $outfile=$_[1];
    $media=$_[2];
    $comment=$_[3];
    #print "$dir $outfile  \n";
    #open embedder html file
    $fname=$_[0]."/".$_[1];
    open(EMBD2,">".$fname)|| die "cannot open $fname for writing";
    #write embedder html file
    print EMBD2 <<ALL_OF_EMBD2;
<html>
<head>
<title> quicktime embedded media file </title>
<style type="text/css">
<!--
/* object stuff here */
/*  mrg note: adapted from page example at http://realdev1.realise.com/rossa/rendertest/quicktime.html */

/* hides the second object from all versions of IE */
* html object.avi {
    display: none;
}

/* displays the second object in all versions of IE apart from 5 on PC */
* html object.avi/**/ {
    display: inline;
}

/* hides the second object from all versions of IE >= 5.5 */
* html object.avi {
    display/**/: none;
}
-->
</style>
</head>
<body LANG="en-US" link="#EEEEFF" alink="#ccffcc" vlink="#ffffcc" bgcolor="#000099" text="#FFFFEE">
<center><table BORDER=0 CELLPADDING=0 CELLSPACING=0>
<tr><td><center> 
            <p><object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" 
                codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="512" height="400">
                <param name="src" value="$media" />
                <param name="controller" value="true" />
                <param name="LOOP" value="TRUE" />
                <object type="video/quicktime" data="$media" width="512" height="400" class="avi">
                    <param name="controller" value="true" />
                    <param name="LOOP" value="TRUE" />
                    Error text.
                </object>
            </object></p></td></tr><tr><td><center>$comment </td></tr>
</table></center>
</body>
ALL_OF_EMBD2

    #close embedder html file
   close(EMBD2);
}
