#!/usr/bin/perl
# 
# WIDE InternetFAX
#      Postscript to TIFF-F converter
#
# file: ifax_ps2fax.pl
# 
# Written by Masahiko KIMOTO <kimoto@ohnolab.org>.
# Copyright (c) 1998 by Masahiko KIMOTO <kimoto@ohnolab.org>,
#                       Ohno Laboratory(Tokyo Institute of Technology),
#                       WIDE Project.
# ALL RIGHTS RESERVED.
#
# If you need fully defined copyright notice, see COPYRIGHT which is 
# included in this package.
# 
# $Id: ifax_ps2fax,v 1.3 1999/03/06 11:19:37 ninomiya Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# Postscript ΥեTIFF-FѴ롣
# ǤgsӤƤ롣
# HylaFAX°ps2faxHylaFAX˰¸ʬȤΤΡ
# ܺ٤README.ifax_ps2fax.jp򻲾ȤΤ
# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# Converts a file in Postscript format to TIFF-F.
# Invokes gs in the internal part.
# This is the ps2fax attached to HylaFAX, from which the parts depending
# on HylaFAX are eliminated.
# ---------------------------------------------------------------------------

$PS="/usr/local/bin/gs";

$fil="";
$out=ps.fax;		# default output filename
$pagewidth=1728;		# standard fax width
$pagelength=297;		# default to A4 
$vres=98;			# default to low res
$device=tiffg3;		# default to 1D

while ($ARGV = shift) {
    if ($ARGV eq "-o") {
	$out=shift;
	next;
    }
    if ($ARGV eq "-w") {
	$pagewidth = shift;
	next;
    }
    if ($ARGV eq "-l") {
	$pagelength = shift;
	next;
    }
    if ($ARGV eq "-r") {
	$vres = shift;
	next;
    }
    if ($ARGV eq "-m") {
	shift;
	next;
    }
    if ($ARGV eq "-1") {
	$device = "tiffg3";
	next;
    }
    if ($ARGV eq "-2") {
	$device="tiffg32d";
#    -2) ($PS -h | grep tiffg32d >/dev/null 2>&1) \
#	    && { device=tiffg32d; } \
#	    || { device=tiffg3; }
	next;
    }
    if ($ARGV =~ /^-/) {
	shift;
	next;
    }
    $fil .= " $ARGV";
}

$fil="-" if (-z $fil);
if ($fil ne "-" && ! -e $fil) {
    print "$0: $fil : No such file or directory.\n";
    exit 254;
}

if ($pagewidth == 1728 &&
    ($pagelength == 280 || $pagelength == 279)) {
    $paper = "letter";
} elsif ($pagelength == 296 || $pagelength == 297) {
    $paper = "a4";
} elsif ($pagelength == 364) {
    $paper = "b4";
} else {
    print "Unsupported page size: $pagewidth x $pagelength\n";
    exit(254);
}

system("cat $fil | $PS -q -sDEVICE=$device -dNOPAUSE -dSAFER=true -sPAPERSIZE=$paper -r204x$vres -sOutputFile=$out -");
