#!/usr/bin/perl
#
# script to build the omino adobe scripts site
# dvb07
#

use strict;
use doIncludes;

my $ROOT = "../";
my $SRC = "$ROOT/src/";
my $BUILD = "$ROOT/product/ominoAdobeScriptsSuite/";

sub stamp
{
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    my $stamp = sprintf "%4d.%02d.%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec;
    return $stamp;
}

# +-------------------
# | pretty time stamp print
# | for progress logging
sub mention
{
    my $stamp = stamp();
    print "$stamp @_\n";
}

sub Cp($$)
{
    my ($src,$dst) = (@_);
    mention("copying $src to $dst");
    `cp $src $dst`;
}

sub Mkdir($)
{
    my ($dir) = (@_);
    if(!-e $dir)
    {
        mention "mkdir $dir";
        `mkdir -p $dir`;
    }
}

sub ReadDir($)
{
    my ($dir) = (@_);
    opendir DIR,$dir;
    my @files = readdir DIR;
    closedir DIR;
    return @files;
}

sub readFile($)
{
    my ($srcPath) = (@_);
    open SRC,$srcPath;
    my $contents;
    read(SRC,$contents,10000000) or die "could not read $srcPath";
    return $contents;
}

sub writeFile($$)
{
    my ($file,$contents) = (@_);
    mention "writing $file";
    open F,">$file" or die "cant write to $file";
    print F $contents;
    close F;
}

sub appendFile($$)
{
    my ($file,$contents) = (@_);
    mention "appending $file";
    open F,">>$file" or die "cant write to $file";
    print F $contents;
    close F;
}

sub getFileSize($)
{
    my ($fileName) = (@_);
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks)
                  = stat($fileName);
    return $size;
}

sub FlattenDir($$$)
{
    my ($endsWith,$srcDir,$dstDir) = (@_);
    Mkdir($dstDir);
    my @files = ReadDir($srcDir);
    for my $file (@files)
    {
        next if($file =~ /^\..*$/); # no starts with dot
        next if(!($file =~ /^.*$endsWith$/)); # must end with $ext
        mention "flattening $file from $srcDir to $dstDir";
        my $pFile = preprocess("$srcDir/$file");
        writeFile("$dstDir/$file",$pFile);
    }
}

sub CpDir($$$)
{
    my ($endsWith,$srcDir,$dstDir) = (@_);
    Mkdir($dstDir);
    my @files = ReadDir($srcDir);
    for my $file (@files)
    {
        next if($file =~ /^\..*$/); # no starts with dot
        next if(!$file =~ /^.*$endsWith$/); # must end with $ext
        Cp("$srcDir/$file","$dstDir/$file");
    }
}

sub AddLinks1File($$$$)
{
    my ($fileName,$relPath,$file,$indexHtml) = (@_);
    mention "linking $file";
    my $desc = getTaggedSection($file,"Description");
    my $size = getFileSize($file);
    my $x = <<EOP
    <div class="box1">
    <p><a href="$relPath">$fileName</a> ($size bytes)  $desc</p>\n
    </div>
EOP
;
    appendFile($indexHtml,$x);
}

sub getTaggedSection($$)
{
    my ($filePath,$tag) = (@_);
    my $contents = readFile($filePath);
    if($contents =~ /^.*$tag:(.*?)::.*$/si)
    {
        my $result = $1;
        return $result;
    }
    # Also try xml-ish
    if($contents =~ /^.*\<$tag\>(.*?)\<\/$tag\>.*$/si)
    {
        my $result = $1;
        return $result;
    }

    return "";
}

sub AddLinks($$$$$$)
{
    my ($title,$endsWith,$relDir,$srcDir,$iconDstPath,$dest) = (@_);
    mention "linking $srcDir";

    my $iconImg;
    if($iconDstPath)
    {
        $iconImg = <<EOP
<img src="$iconDstPath" width="40" height="40" />
EOP
;
    }
    my $section = <<EOP
    <br />
    <div class="box1">
      <h2> $iconImg $title </h2>
    </div>
EOP
;
    appendFile($dest,$section);
    my @files = ReadDir($srcDir);
    for my $file (@files)
    {
        next if($file =~ /^\..*$/); # no starts with dot
        next if(!$file =~ /^.*$endsWith$/); # must end with $ext
        AddLinks1File($file,"$relDir/$file","$srcDir/$file",$dest);
        #AddLinks1File("$srcDir/$file",$file,$dest);
    }
}


sub main()
{
    Mkdir($BUILD);
    my $indexFile = "$BUILD/index.html";
    my $stamp = stamp();
    my $header = <<EOP ;

<link rel="stylesheet" type="text/css" href="/omino.css" title="Default" />
<div class="frame">

    
    <h1>Omino Adobe Scripts Suite</h1>
    <small>built: $stamp</small><br />

    <br />
    The Omino Adobe Scripts Suite consists of various specialized and
    general purpose scripts I've written now and then. Perhaps you
    will find them useful too.
    <br />
    <br />
    Provided as-is, help yourself, and so forth.
    <br />
    <br />
    Questions to poly\@omino.com, subject must begin "plugin".
    <br />
    <br />

EOP
    writeFile($indexFile,$header);

    for my $x (
        "illustrator:ai:Illustrator",
        "afterEffects:ae:After Effects",
        "photoshop:ps:Photoshop",
	"shared::Shared")
    {
        my ($kind,$id,$title) = split(/:/,$x);
        my $srcDir = "$SRC/$kind/";
        my $dstDir = "$BUILD/$kind/";
        FlattenDir(".jsx",$srcDir,$dstDir);
        my $iconSrcFile = "$srcDir/icon.png";
        my $iconDstPath;
        my $iconDstFile;
        if(-e $iconSrcFile)
        {
            $iconDstPath = "img/${id}_icon.png";
            $iconDstFile = "$BUILD/$iconDstPath";
            Mkdir("$BUILD/img/");
            Cp($iconSrcFile,$iconDstFile);
        }
        AddLinks($title,".jsx",$kind,"$BUILD/$kind",$iconDstPath,$indexFile);
    }

    my $tail = <<EOP
    </div>
EOP
;
    appendFile($indexFile,$tail);
}


main();
