#!/usr/bin/perl
#
# WIDE InternetFAX
#
# file: ifax_mail_send
#
# Written by Kei MOCHIDA <mochida@ohnolab.org>,
#            Masahiko KIMOTO <kimoto@ohnolab.org> and
#            Hiroyuki OHNO <hohno@ohnolab.org> 
# Copyright (c) 1998 by Kei Mochida <mochida@ohnolab.org>,
#                       Masahiko KIMOTO <kimoto@ohnolab.org>,
#                       Hiroyuki OHNO <hohno@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_mail_send,v 1.4 1999/03/08 04:22:58 kimoto Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# e-mail֥⥸塼
# ܺ٤README.ifax_mail_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
#-d mail@domain
#-s seqf
#-sub subject
#######################

#ޥ
$mail  = "/usr/bin/mail"; #ȤǤޤ
$ps2fax = "/var/spool/fax/bin/ps2fax";
$pspr =  "/usr/local/bin/pspr";
$file_c = "/usr/bin/file";
$fax2ps = "/usr/local/bin/fax2ps";
$convert = "/usr/X11R6/bin/convert";
$mpack = "/usr/local/bin/mpack";
$sendmail = "/usr/sbin/sendmail";
$nkf = "/usr/local/bin/nkf-1.6";
#ե

$queue = $spooldir;
$threddir = "";
$wifax_etc = "/var/spool/fax/etc/";
$setup = "setup\.cache";
$header = "header";
$text_tmp = "mpack_tmp2";


#ץѿ
$dist = "";
#ѿ
$data = "";
@data = "";
$text_file = "";
$picture = "";
@pictures = "";
@remove = "";
$remove = "";
@texts = "";
$text_data = "";
#################################

#ץط
while(@ARGV){
    if($ARGV[0] eq "-sub"){
	shift(@ARGV);
	$subject = @ARGV[0];
    }
    elsif($ARGV[0] eq "-d"){
	shift(@ARGV);
	$dist = $dist." ".$ARGV[0];
    }
    elsif($ARGV[0] eq "-s"){
	shift(@ARGV);
	$num = $ARGV[0];
    }else{
	shift(@ARGV);
    }
}

if($subject eq ""){
    $subject = "FROM_PICKLES";
}
     

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

if($dist eq ""){
    die "please specify a destination mail address\.\n";
}


#PS˥С

#print "num = $num\n";

chdir("$queue/$num");
open(FILES,"/bin/ls -A $queue/$num/ |")|| die "cant open $queue/$num\n";
while(<FILES>){
    chop;
    $file = $_;
    open(DATA,"$file_c $file|")||die "cant open $file\n";
    $data = <DATA>;
    @data = split(/:/,$data);
    
    if($data[1] =~ /TIFF|tiff/){
	
	@pictures = ("@pictures","$file.ps");
	@remove =  ("@remove","$file.ps");
	open(CONTENT,"$queue/$num/$file") || die "cannot open CONTENT";
	open(LPR_TIFF,"| $fax2ps  > $queue/$num/$file.ps") 
		|| die "cannot open for LPR_TIFF";
	while(<CONTENT>){
	    print LPR_TIFF;
	}
	
	close(CONTENT);
	close(LPR_TIFF);

    }elsif($data[1] =~ /GIF|gif/){
	@pictures = (@pictures,"$file.ps");
	@remove =  ("@remove","$file.ps");
	system("$convert $file $file.ps");
	
    }elsif($data[1] =~ /JPG|JPEG|jpeg|jpg/){
	@pictures = (@pictures,"$file.ps");
	@remove =  ("@remove","$file.ps");
	system("$convert $file $file.ps");
	
    }elsif($data[1] =~ /PostScript|PS|postscript/){	
	@pictures = (@pictures,"$file");
    }elsif($data[1] =~ /text|ascii/){
	if($_  eq $header){}else{
	    
	    @texts = (@texts,$_);}
    }
}




###ޤƥȥեԽ򤹤



#for($i = 0;$i < scalar(@texts);$i ++){
#    $letter = system("/bin/echo @texts[$i] |$nkf -j");
#    @base64 = (@base64,$letter);
#}

print @texts;

if ($#texts != 0) {
    foreach $i (@texts) { 
	open(TEXT,"$queue/$num/$i")||die "cant open $i\n";
	while(<TEXT>){
	    
	    #@text_data = (@text_data,$_);
	    $text_data = join("",$text_data,$_);
	    
	}
	$text_data = join("",$text_data,"\n\n");
    }
}


#open(NKF,"|$nkf -j  > $queue/$num/$text_tmp");
#$text_data = join("",@text_data);
#print NKF $text_data;
#close(NKF);


####ǡԽ

$pictures = join(" ",@pictures);
#print "hgohgoehoehg $pictures\n";


unless(($pictures eq  "")||($pictures eq "\ ")){

    mkdir("etc",0755);
    system("/bin/cp -f $wifax_etc/$setup $queue/$num/etc/$setup");

    system("$ps2fax $pictures");
    #

     if($text_data eq ""){
	system("$mpack -s $subject -c image/tiff ps.fax $dist");
	@remove = (@remove,"ps.fax");
    }else{
        $tmp = "tempfile.$$";
	open(MPACK_TMP,"|$nkf -j > $queue/$num/$tmp");
	print MPACK_TMP $text_data;
#	print MPACK_TMP $decode;
	close(MPACK_TMP);
	system("$mpack -s $subject -d $queue/$num/$tmp -c image/tiff ps.fax $dist");
	@remove = (@remove,$tmp,"ps.fax");
    }
}else{

    if($text_data eq ""){
    }else{
#	$text_data = join("",@text_data);

	open(SENDMAIL,"|$nkf -j|$sendmail $dist");
 	foreach $addr  (split(/ /, $dist)) {
	    print SENDMAIL "To: $addr\n";
 	}
	print SENDMAIL "From: $IFAX\n";
	if($subject eq ""){
	    
	    print SENDMAIL"Subject: $subject\n";}
	    else{
		print SENDMAIL "Subject: INTERNET FAX\n";
	    }
	print "\n";
	print SENDMAIL "$text_data";

    }
}


#Ȥ

$remove  = join("\ ",@remove);

chdir("$queue/$num");
unless(scalar(@pictures) eq 0){
    if(scalar(@texts) eq 0){
	system("/bin/rm -f $tmp");
    }
    system("/bin/rm -f  $remove");
    system("/bin/rm -fr $queue/$num/etc");

}
	
    

