#!/usr/bin/perl
# 
# Copyright (C) Koji Nakamaru
#
# Author: Koji Nakamaru (nakamaru at gmail.com)
# Modified: Jun 5 2005
#   * removed -g option of ps2eps for robust bbox handling. added -O
#     option of ps2eps for landscape pages.
# Modified: Apr 30 2005
#   * changed the contact information.
# Modified: Jan 9 2002
#   * renamed this command ps2epsfiles.pl
# Created: Jan  9 2004
# Keywords: powerpoint, postscript, ghostscript, psutils, psselect, ps2eps
#
# Commentary:

# This script converts PostScript files into multiple, separated EPS
# files. It targets especially PostScript files generated by printing
# a PowerPoint document through the Windows PostScript printer driver.
# It requires the following three other programs:
#   psselect comes with psutils (http://www.ctan.org/tex-archive/support/psutils/)
#   ps2eps (http://www.telematik.informatik.uni-karlsruhe.de/~bless/ps2eps.html)
#   ghostscript (http://www.ghostscript.com/)
#
# Usage: perl ps2epsfiles.pl [options] in.prn
# Options:
#  --help:
#        print usage.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

use Getopt::Long;
use File::Copy;

$usage = <<"EOF";
Usage: perl ps2epsfiles.pl [options] in1.prn in2.prn ...
Options:
  --help:
        print usage.
EOF

sub xwarn;
sub xerror;
sub xinterrupted;

if (! GetOptions('help')) {
    print STDERR $usage;
    exit 1;
}
if ($opt_help || @ARGV == 0) {
    print STDERR $usage;
    exit 1;
}

$SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'xinterrupted';
if ($^O =~ /MSWin32/i or $^O =~ /cygwin/i) {
    $tmpfname = "_ps2epsfiles$$";
} else {
    $tmpfname = "/tmp/_ps2epsfiles$$";
}
$g = 1;
foreach $i (@ARGV) {
    if (! open(IN, "< ${i}")) {
	xwarn "cannot open $i";
	next;
    }
    $_ = <IN>;
    close IN;
    if (! /^%!/) {
	xwarn "$i is a non-postscript file";
	next;
    }
    $p = 1;
    while (`psselect -p$p $i ${tmpfname} 2>&1` =~ /Wrote 1 page/) {
	open IN, "< ${tmpfname}"
	    or xerror "cannot open ${tmpfname}";
	binmode IN;
	open OUT, "> ${tmpfname}.ps"
	    or xerror "cannot open ${tmpfname}.ps";
	binmode OUT;
	$islandscape = 0;
	while (<IN>) {
	    print OUT;
	    if (/^%%Orientation:\s*Landscape/) {
		$islandscape = 1;
	    }
	}
	close OUT;
	close IN;
	$outfname = sprintf "fig%03d", $g;
	if ($islandscape) {
	    $rot = "-R +";
	} else {
	    $rot = "";
	}
	system "ps2eps -q -B -l -s a0 ${rot} -f ${tmpfname}.ps";
	move "${tmpfname}.eps", "${outfname}.eps";
	print STDERR "${outfname}.eps\n";
	$g++;
	$p++;
    }
}
$SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = $SIG{'HUP'} = '';
unlink "${tmpfname}";
unlink "${tmpfname}.ps";
unlink "${tmpfname}.eps";
exit 0;

sub xwarn
{
    my ($msg) = @_;
    print STDERR "pspages2epsfiles.pl: warning - ", $msg, "\n";
}

sub xerror
{
    my ($msg) = @_;
    print STDERR "pspages2epsfiles.pl: ", $msg, "\n";
    unlink "${tmpfname}";
    unlink "${tmpfname}.ps";
    unlink "${tmpfname}.eps";
    exit 1;
}

sub xinterrupted
{
    xerror "interrupted";
}
