#!/usr/bin/env perl
use strict;
$^W=1;

my $prg_zip = 'zip';
my $prg_ziptimetree = 'ziptimetree';

my $dir_build = 'build';
my $dir_texmf = "$dir_build/texmf"; 
my $dir_tex = "$dir_texmf/tex/latex/hyperref";
my $dir_doc = "$dir_texmf/doc/latex/hyperref";
my $dir_src = "$dir_texmf/source/latex/hyperref";
chomp(my $cwd = `pwd`);

system("rm -rf $dir_build");
unlink "hyperref-tds.zip";
mkdir $dir_build;
mkdir $dir_texmf;
system("mkdir -p $dir_tex");
system("mkdir -p $dir_doc");
system("mkdir -p $dir_src");
mkdir "$dir_src/doc";
mkdir "$dir_src/test";

### TDS:tex
chdir $dir_build;
symlink '../hyperref.ins', 'hyperref.ins';
symlink '../hyperref.dtx', 'hyperref.dtx';
symlink '../nameref.dtx', 'nameref.dtx';
symlink '../backref.dtx', 'backref.dtx';

open(OUT, '>', 'docstrip.cfg') or die "!!! Error: Cannot write `docstrip.cfg'!\n";
print OUT <<'END_FILE';
\BaseDirectory{texmf}
\UseTDS
\endinput
END_FILE
close(OUT);

system("tex hyperref.ins");
chdir $cwd;

foreach (qw[minitoc-hyper.sty ntheorem-hyper.sty xr-hyper.sty
            hylatex.ltx
            ]) {
    link $_, "$dir_tex/$_";
}

### TDS:doc
foreach (qw[README README.pdf
            ChangeLog ChangeLog.pdf
            hyperref.pdf
            ]) {
    link $_, "$dir_doc/$_";
}
foreach (qw[manual.pdf
            options.pdf
            slides.pdf paper.pdf
            manual.html manual2.html manual3.html
            manual.css cmmi10-22.gif cmsy10-21.gif
            ]) {
    link "doc/$_", "$dir_doc/$_";
}

### TDS:source
foreach (qw[hyperref.ins
            hyperref.dtx backref.dtx nameref.dtx
            bmhydoc.sty
            ]) {
    link $_, "$dir_src/$_";
}
foreach (qw[fdl.tex manual.tex options.tex]) {
    link "doc/$_", "$dir_src/doc/$_";
}
open(IN, 'test/files.xml') or die "!!! Error: Cannot open `test/files.xml'!\n";
for (<IN>) {
    if (/<file name="([^"]+)"/) {
        link "test/$1", "$dir_src/test/$1";
    }
}
close(IN);

### Generate ZIP file
system("$prg_ziptimetree --verbose --noroot $cwd/hyperref-tds.zip "
       . "$dir_build/texmf");

__END__
