#!/usr/bin/env perl

#A  Greg Gamble ... based in part on Frank Lbeck's packpack
## 
#H  $Id: make_zoo,v 1.13 2006/01/31 16:00:14 gap Exp $
##
##  Usage: make_zoo
##
##  Uses perl and zoo to create <pname>-<version>.zoo and
##  <pname>doc-<version>.zoo archives and copies the README 
##  to README.<pname> (each is placed in the pkg directory).
##

#set value of <pname>
$pname = "example";

#read <version> from file VERSION
$version = `cat VERSION`;
chomp $version;
#if you don't store <version> in such a file, just do it explicitly
#(comment out two previous lines, and uncomment next line after editing ...)
#$version = "...";

#package documentation prefix
$pdoc = $pname . "doc";

#remove files created during installation
system( "make clean" );

#remove old .zoo files
system( "rm -f ../$pname*-$version.zoo" );

#copy README in pkg directory 
system( "cp -f README ../README.$pname" );

### Create new .zoo files in pkg directory ##################################

# substrings (mainly suffixes) that determine files we want to exclude
$prune = "[._]cvs|[.]bbl|[.]aux|[.][ib]lg|[.]log|[.]ind|[.]idx".
#         "|zoo|make_".         # uncomment to exclude make_doc, make_zoo etc.
         "|[.]sw[op]|[.-]bak|~|[.][#]";

# patterns that indicate a file is a text file (first line has suffixes)
$text = "(([.](txt|ps|g(|[id])|htm(|l)|[ch]|xml|tex|toc|lab|bib|six|[tm]st)\$)".
        "|(VERSION|README|INSTALL|Makefile|configure|make_))";

# Create doc .zoo file (containing just the doc and htm directories)
addtoarchive( "$pdoc-$version.zoo", "doc", "htm" );
system( "mv $pdoc-$version.zoo .." );

chdir "..";

# Create the main .zoo file
addtoarchive( "$pname-$version.zoo", $pname );

### Subroutine to create archive or add to existing archive #################
# Usage: addtoarchive( archive, dir1, dir2, ... )
# Synopsis: adds files in directories dir1, dir2, ... to zoo-archive archive
#           ignoring files in CVS directories or with suffixes listed in $prune
#           and marking files that match the pattern $text as text files
sub addtoarchive {
  ($archive, @dir) = @_;
  for $dir ( @dir ) {
    system( "find $dir -name CVS -prune -o -print | egrep -v '$prune' | ".
            "zoo ahI $archive");
    my @textfiles = `find $dir -name CVS -prune -o -print | egrep '$text'`;
    for $file ( @textfiles ) {
      chomp $file;
      system( "(echo '!TEXT!'; echo '/END') | zoo c $archive $file" );
    }  
  }
}
