#!/usr/bin/env perl

#A  Greg Gamble ... based in part on Frank Lübeck's packpack
## 
#H  $Id: make_zoo,v 1.11 2003/06/23 03:37:52 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 values of <pname> and <version>
$pname = "example";
$version = "1.5";

#if <version> is in file VERSION uncomment and capture it this way
#$version = `cat VERSION`;
#chomp $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|[.]toc|[.][ib]lg|[.]log|[.]lab|[.]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|bib|six|tst|mst)\$)".
        "|(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" );
    }  
  }
}
