#!/usr/bin/perl
#
# WIDE InternetFAX
#
# file: ifax_prt_send
#
# Written by Kei MOCHIDA <mochida@ohnolab.org>.
# Copyright (c) 1998 by Kei Mochida <mochida@ohnolab.org>,
#                       Ohno Laboratory(Tokyo Institute of Technology) and
#                       WIDE Project.
# ALL RIGHTS RESERVED.
#
# If you need fully defined copyright notice, see COPYRIGHT which is 
# included in this package.
# 
# $Id: ifax_prt_send,v 1.4 1999/03/08 04:22:58 kimoto Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# ץ()֥⥸塼
# ܺ٤README.ifax_prt_send.jp򻲾ȤΤ
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
#  եɤ߹ࡣ
# ---------------------------------------------------------------------------
# ɸեpath/etc/wifax.confĶѿ
# WIFAXCONFPATHͤǾ񤭤Ǥ롣
# եƤ KEY = VAL ηǤФȤꤢʤǤ
# ŪKEYιܤϷƤ
# ޤKEYʸʸ̵뤹롣
# rulefile: dispatcherѤ롼ǡ١Υե̾
# bindir:   ٤ƤΥ⥸塼μ¹Է֤Ƥǥ쥯ȥ̾
# spooldir: סΥǥ쥯ȥ̾
#
$wifax_conf = "/etc/wifax.conf";
$wifax_conf = $ENV{'WIFAXCONFPATH'} if ($ENV{'WIFAXCONFPATH'} ne "");
undef %CONFIG;
if (-e $wifax_conf) {
    open(CONF,$wifax_conf) || die "cannot open $wifax_conf\n";
    while (<CONF>) {
	chop;
	/\S/ || next;
	/\s*\#/ && next;
	/\s*([^= ]+)\s*=\s*(.*)$/ || next;
	$1 =~ tr/A-Z/a-z/;
	$CONFIG{$1}=$2;
    }
    close(CONF);
}

# ---------------------------------------------------------------------------
# ѿDEFAULT
# ---------------------------------------------------------------------------
$default_rulefile = "/var/wifax/etc/wifax.rule";
$default_spooldir = "/var/wifax/spool/";
$default_bindir   = "/usr/wide/wifax/bin";

# ---------------------------------------------------------------------------
# PATH
# ---------------------------------------------------------------------------

$rulefile = ($CONFIG{'rulefile'} eq "") ? 
    $default_rulefile : $CONFIG{'rulefile'};
$spooldir = ($CONFIG{'spooldir'} eq "") ? 
    $default_spooldir : $CONFIG{'spooldir'};
$bindir = ($CONFIG{'bindir'} eq "") ? 
    $default_bindir : $CONFIG{'bindir'};

# ---------------------------------------------------------------------------

##### option ######
#-P$printer
#-h               #إåե
#-s $num
###################

#ޥ

$a2ps = "/usr/local/bin/a2ps";
$fax2ps = "/usr/local/bin/fax2ps";
$lpr = "/usr/bin/lpr";

$LABELSTRING = "MESSAGE_INFORMATION";

#ե
$queue = $spooldir;
$seqfile = "seqf";
$header = "header";
$rm = "/bin/rm";
$convert = "/usr/X11R6/bin/convert";


$exist_textfile = 0;

#ץطѿ
$printer = "pringles";
$head_option = "no";
$num = "";


#ѿ


$file = "";
@pictures = "";
$pictures = "";
@remove = "";

#######################################
#ץȽ

    while(@ARGV){
	if($ARGV[0] eq "-H"){
	    shift(@ARGV);
	    $head_option = "yes";
	}
	elsif($ARGV[0] =~ /^\-P/){
	    @rev = split(//,$ARGV[0]);
	    shift(@rev);
	    shift(@rev);	    
	    $printer = join("",@rev);
	    shift(@ARGV);
       }
	elsif($ARGV[0] eq "-s"){
	    shift(@ARGV);
	    $num = $ARGV[0];
	}else{
	    shift(@ARGV);
	}
    }

if($num eq ""){
    die "please specify queue No\.\n";
}

#եPS˥С

chdir("$queue/$num")||die "cannt chdir $queue  $num\n";

$textfilename = "$queue/$num/textfile.$$.ps";
open(LPR_TXT,"|$a2ps -l $LABELSTRING -p > $textfilename");

$linec = 0;

open(FILES,"/bin/ls -A $queue/$num/|");
while(<FILES>){
    chop;
    $file = $_;

    next if ($file =~ /$textfilename/) ;

    open(DATA,"/usr/bin/file $file|");
    $data = <DATA>;
    @data = split(/:/,$data);
    close(DATA);
    
    #ե°Ƚ
    if($data[1] =~ /TIFF|tiff/){ #ȽǤޤå
	@pictures = (@pictures,"$file.ps");
	@remove = (@remove,"$file.ps");
	open(CONTENT,"$queue/$num/$file");
	open(LPR_TIFF,"| $fax2ps  > $file.ps");
	while(<CONTENT>){
		  print LPR_TIFF;
	}
	close(CONTENT);
	close(LPR_TIFF);
	
    }elsif($data[1] =~ /PostScript|PS|postscript/){
	@pictures = (@pictures,"$file");
    }elsif($data[1] =~ /text|ascii/){
	
	if(($head_option eq "no")&&($file eq $header)){

	} else{

	  if ($file eq $header) {
		print LPR_TXT "MESSAGE HEADER:\n\n";
	} else {
		print LPR_TXT "MESSAGE DESCRIPTIONS\n\n";
	}
	    
	    # @pictures = (@pictures,"$file.ps");
	    @remove = (@remove,"$file.ps");
	    open(CONTENT,"$queue/$num/$file");
	    while(<CONTENT>){
		print LPR_TXT  if ($linec++ < 50);
	    }
	    close(CONTENT);
	    print LPR_TXT "------------------------------------------\n"; 
	   $exist_textfile = 1;
	}
    }elsif(($data[1] =~ /JPG|JPEG|jpeg|jpg/)||($data[1] =~ /GIF|gif/)){
	@pictures  = (@pictures,"$file.ps");
	@remove = (@remove,"$file.ps");
	system("$convert $file $file.ps");
    }else{
    }
}
close(FILES);
close(LPR_TXT);

if ($exist_textfile) {
	push(@pictures,$textfilename);
} else {
	unlink ($textfilename);
}

chdir("$queue/$num/");

$pictures = join(" ",@pictures);
system("$lpr -P$printer $pictures");
	
$remove = join(" ",@remove);

#system("$rm $remove");
