#!/usr/bin/perl

# builddoc - build Spong documentation in various formatted
#
# This utility is designed to build the Spong documentation from all of
# the POD document sources. It will build various formats: html, text and man
#

# $Id: builddoc,v 1.2 2001/01/06 06:36:13 sljohnson Exp $

use File::Basename;
use Getopt::Long;

@suffixlist = ('.pm','.pl','.pod');

Getopt::Long::Configure("pass_through");

if (! GetOptions("dir:s" => \$dstdir, "type:s" =>$doctype )) {
   print STDERR "Incorrect Usage\n";
   usage();
}

if (! defined $doctype && ! defined $ARGV[0] ) { 
   print STDERR "You have to specify documentation type: html, text, man , etc\n\n";
   usage();
}

$doctype = $ARGV[0] unless defined $doctype;

if ($doctype eq "html" || $doctype eq "dist" ) { $suffix = ".html"; }
elsif ($doctype eq  "text") { $suffix = ".txt"; }
elsif ($doctype eq "man") { $suffix = ".man"; }
else {
   print STDERR "Invalid documentation type. Must be: html, text, man\n";
   usage();
}

$files = `find ../ -type f -print \| egrep '*.pod\$\|*.pm\$\|plugins' \| grep -v CVS`;
@files = split /\s+/,$files;

system "rm -f pod2html-*" if $doctype eq "html";

# Don't try this at home. I use this option to build the documentation for
# Spong distributions. I have some customized programs which do this.
# Write me at sjohnson@monsters.org if you want to know what I use.
if ($doctype eq "dist") {
   # Make sure that CWD is in a known sane place
   chomp($cwd = `pwd`);
   $mydir = dirname($cwd . "/" . $0);
   chdir $mydir;

   $files =~ s/\s+/ /g;  # Make the whitepace into spaces

   $cmd = "mpod2html -dir ../www/docs/ -tocname spongtoc -idxname spongindex" 
            . " $files >/tmp/pod.out 2>&1";
   system $cmd;
   exit 0;
}

foreach $file ( @files ) {

   print "$file\n";

   $basename = basename($file,@suffixlist);

   $outfile = $basename . $suffix;
   $outfile = $dstdir . "/" . $outfile if $dstdir;

   if ($doctype eq "html") { 
      $cmd = "pod2html --recurse --podroot=../ --podpath=pod:src " . 
                "--infile=$file --outfile=$outfile 2>/dev/null"; 
   } elsif ($doctype eq "text") {
      $cmd = "pod2text <$file >$outfile 2>/dev/null";
   } elsif ($doctype eq "man") {
      $cmd = "pod2man --center 'Spong 2.7' --release '2.7' $file >$outfile " .
                "2>/dev/null";  
   }
 
   system $cmd;
}

sub usage {

   print <<EOF;
Usage: builddoc [--dir dstdir] --type doctype | doctype

Where:
   --dir   dstdir           Directory in which to create output files
   --type  html|text|man    Type of documentation to create.

EOF

   exit 0;
}
