eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' # -*-perl-*-
& eval 'exec perl -S $0 $argv:q'
if 0;
#---- 
use strict;

# Matthieu Moy, November 03, 2005
# Splits a FIG file according to the depth

my $inputfile;
my $outputfile;
my $outbasename;
my @depths = ();
my @adepths;
my $current;
my $mag = 1;
my $scriptfile;
my $action = "pdftex";
my $modulo = 5;

sub usage () {
    print <<"END_OF_USAGE";
Usage:
    figanim [options] FILE [options]
Options:
        -o OUTFILE          Generate animation in OUTFILE
        -m MAG              Use magnification factor of MAG
        -s SCRIPTFILE       Use SCRIPTFILE to describe the animation
        -h                  This help message
        -t                  Generate a template scriptfile to stdout
                              (don\'t generate any PDF file)
        -mod NUM            Group depth by NUM (5 by default)

Splits a FIG file according to the depth. Generates one PDF file for
each slice of 5 depths, and a beamer animation showing the layers
incrementally, starting with the greatest depth. Use "-mod 1" to
disable this feature.

SCRIPTFILE is a file whose lines are either comments (starting with
'#') or containing valid argument for the -D option of fig2dev (a list
of comma-separated numbers or ranges separated by colon (:). For
example, -D +10,40,55:70,80 means keep only layers 10, 40, 55 through
70, and 80.)

Examples:
\$ figanim foo.fig -o bar.pdftex_t

The resulting bar.pdftex_t can be used the following way:

\\documentclass{beamer}
\\usepackage{graphicx}
...
\\begin{document}
...
\\frame{
  \\input bar.pdftex_t
}
...
\\end{document}

\#\# Using script file
\$ cat script.txt
\# slide 1
+10
\# slide 2: show up to depth 20
+10:20
\# slide 3: add an element on depth 21
+10:20,21
\# slide 4: remove element from slide 3, add another
+10:20,22
\# slide 5: and so on
+10:20,23
\$ figanim foo.fig -o foo.pdftex_t -s foo.script



END_OF_USAGE
    exit 0;
}

sub genpdf () {
    my $count = $_[0];
    my $range = $_[1];
    my $pdffile = "$outbasename-$count.pdf";
    my $pstexfile = "$outbasename-$count.pstex";
    my $pdftexfile = "$outbasename-$count.pdftex_t";
    my $cmd1 = "fig2dev -L pdftex_t -D '$range' -p $pdffile $inputfile $pdftexfile";
    my $cmd2 = "fig2dev -L pstex    -D '$range' -p dummy $inputfile $pstexfile";
    my $cmd3 = "epstopdf $pstexfile -o $pdffile";
    print $cmd1, "\n", $cmd2, "\n", $cmd3, "\n\n";
    system ($cmd1);
    system ($cmd2);
    system ($cmd3);
}

if ($#ARGV < 1) {
    usage ();
}

for (my $i = 0; $i <= $#ARGV; ++$i)
{
    if      ( $ARGV[$i] eq "-o" ) {
        $i++;
        $outputfile=$ARGV[$i];
    } elsif ( $ARGV[$i] eq "-m" ) {
        $i++;
        $mag=$ARGV[$i];
    } elsif ( $ARGV[$i] eq "-mod" ) {
        $i++;
        $modulo=$ARGV[$i];
    } elsif ( $ARGV[$i] eq "-s" ) {
        $i++;
        $scriptfile=$ARGV[$i];
    } elsif ( $ARGV[$i] eq "-t" ) {
        $action="template";
    } elsif ( "$ARGV[$i]" eq "-h" ) {
        usage();
    } else {
	$inputfile=$ARGV[$i];
    }
}

if (! $outputfile) {
    $outputfile=$inputfile;
    $outputfile =~ s/\.[^\.]*$/.pdftex_t/
}

$outbasename = $outputfile;
$outbasename =~ s/\.[^\.]*$//;

my $count = 0;

if ($scriptfile) {
    open(SCR, $scriptfile); 
    while(<SCR>) {
        if (! (/^#/ || /^[ \t]*$/)) {
            $count++;
            /^(.*)$/;
            &genpdf($count, "$1");
        }
    }
} else {
    open(FIG, $inputfile);
    $_=<FIG>;
    if (! /\#FIG/) {
        die "$inputfile is not a fig file!";
    }
    
    while (<FIG>) {
        # http://www.xfig.org/userman/fig-format.html
        if (/^[1235] *-?[0-9]+ *-?[0-9]+ *-?[0-9]+ *-?[0-9]+ *-?[0-9]+ *([0-9]+) /) {
            $current = $1 - ($1 % $modulo);
            if (@adepths[$current] != 1) {
                push (@depths, $current);
                @adepths[$current] = 1;
            }
        } elsif (/^[4] *-?[0-9]+ *-?[0-9]+ ([0-9]+)/) {
            $current = $1 - ($1 % $modulo);
            if (@adepths[$current] != 1) {
                push (@depths, $current);
                @adepths[$current] = 1;
            }
        }
    }
    
    @depths = sort {$b <=> $a} @depths;

    if ($action eq "pdftex") {
        foreach (@depths) {
            $count++;
            &genpdf($count, "+$_:999");
        }
    } elsif ($action eq "template") {
        foreach (@depths) {
            print "+$_:999\n";
        }
    }
}

if ($action eq "pdftex") {
    open (TEXFILE, ">$outputfile");
    for (my $i = 1; $i < $count; $i++) {
        print TEXFILE "\\only<$i>{\%\n\\input $outbasename-$i.pdftex_t}\%\n";
    }
    print TEXFILE "\\only<$count\->{\%\n\\input $outbasename-$count.pdftex_t }\%\n";
    close (TEXFILE);
}
