#!/usr/bin/perl -Iscripts

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# =======================================================================
# Doxygen Pre-Processor for Perl
# Copyright (C) 2002  Bart Schuller
# Copyright (C) 2006  Phinex Informatik AG
# All Rights Reserved
# 
# Doxygen Filter is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
# 
# Larry Wall's 'Artistic License' for perl can be found in
# http://www.perl.com/pub/a/language/misc/Artistic.html
# 
# =======================================================================
# 
# Author: Aeby Thomas, Phinex Informatik AG,
# 	  Based on DoxygenFilter from Bart Schuller
# E-Mail: tom.aeby@phinex.ch
# 
# Phinex Informatik AG
# Thomas Aeby
# Kirchweg 52
# 1735 Giffers
# 
# =======================================================================
# 
# @(#) $Id: doxygenfilter,v 1.3 2006/02/01 12:54:07 aeby Exp $
# 
# Revision History:
# 
# $Log: doxygenfilter,v $
# Revision 1.3  2006/02/01 12:54:07  aeby
# call pas2dox for .pas files, js2doxy for .js files
# add "lib" to the include path
#
# Revision 1.2  2006/01/31 16:53:28  aeby
# parse command line options, understand -h and -v, output a usage message if
# command line syntax is not ok
# treat input as perl if filename is ending with .pl, .pm or .perl, as sql
# if filename ends with .sql, if no file extension is used treat it as perl
# if the first line starts with #!....bin.perl. In all other cases just pass
# the file through as is.
#
#  
# =======================================================================


use warnings;
use strict;

use lib "lib";
use DoxyGen::PerlFilter;
use DoxyGen::SQLFilter;
use Getopt::Long;

$Getopt::Long::ignorecase = 0;  
my $verbose;
my $help;
unless( GetOptions( "verbose" => \$verbose, "v" => \$verbose,
	  "help" => \$help, "h" => \$help ) && $ARGV[0] ) {
    $help = 1;
}

if( $help ) {
    my $prog = $0;
    $prog =~ s#.*/##;
    print STDERR <<END;
Usage: $prog [-v] filename

Pre-processes Perl code in file <filename> and outputs
something doxygen does understand.

END
    exit 1;
}

open( FILE, "<$ARGV[0]" );
my $filehead = "";
for( my $line=0; ($line<3) && ($_ = <FILE>); $line++ ) {
    $filehead .= $_;
}
close FILE;

my $ext = "";
if( $ARGV[0] =~ /\.([a-z]+)$/i ) {
    $ext = lc($1);
}

my $filter;
if( $ext eq "sql" ) {
    print STDERR "treating file as SQL\n" if( $verbose );
    $filter = DoxyGen::SQLFilter->new(\*STDOUT);
} elsif( grep( $_ eq $ext, "pl", "pm", "perl" )
	|| $filehead =~ /^#!.{0,10}bin.perl/ ) {
    print STDERR "treating file as Perl\n" if( $verbose );
    $filter = DoxyGen::PerlFilter->new(\*STDOUT);
}
elsif( $ext eq "js" ) {
    print STDERR "treating file as JavaScript\n" if( $verbose );
    exec( "js2doxy", @ARGV ) or exec( "js2doxypl", @ARGV )
    or print STDERR "js2doxy not installed? - see http://jsunit.berlios.de/internal.html\n";
}
elsif( $ext eq "pas" ) {
    print STDERR "treating file as Pascal\n" if( $verbose );
    exec( "pas2dox", @ARGV )
    or print STDERR "pas2dox not installed? - see http://sourceforge.net/projects/pas2dox/\n";
}

if( $filter ) {
    $filter->filter($ARGV[0]);
}
else {
    print STDERR "passing file through\n" if( $verbose );
    print <>;
}
