#!/usr/bin/perl
#
# WIDE InternetFAX
#
# file: ifax_spooler.pl
# 
# Written by Takakazu YAMAUCHI <yamauchi@ohnolab.org>.
# Modified by Masahiko KIMOTO <kimoto@ohnolab.org>.
# Copyright (c) 1998 by Takakazu YAMAUCHI <yamauchi@ohnolab.org>,
#                       Masahiko KIMOTO <kimoto@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_fax_recv,v 1.7 1999/03/09 04:59:50 kimoto Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# FAX֥⥸塼
# ܺ٤README.ifax_fax_recv.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'};

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

$queue1 = "/var/spool/fax/recvq/";
$seqfile = "$spooldir/seqf";
$queue2 = $spooldir;
$seqf2 = "/var/spool/fax/recvq/seqf";

chdir("/var/spool/fax"); ## XXX: This is just quick hack.

CHECK: { 
  open(SEQF, "$seqf2");
  $number = <SEQF>;         #seqfκǸ˲Ԥʤ
  $oldnum = $number;
  close(SEQF);

  while() { 
    open(SEQF, "$seqf2");
    $number = <SEQF>;
    flock(SEQF,2);
    if ($oldnum != $number) {
      goto RECEIVE;
    }
    else {
      flock(SEQF,8);
      close(SEQF);
      sleep 3; 
      $oldnum = $number;
    }
  }  
}    


RECEIVE: {                    #FAXǡTIFF/Fޤ
  flock(SEQF,8);
  close(SEQF);
  while() {
    $flag = system "faxstat -sa | grep 'Running and idle'"; #
    if($flag == 0) {
      &get_work_dir;
      goto CHECKDIR; 
    }
    sleep 3; 
  }
}

CHECKDIR: {
  opendir(DIR, "$queue1");               #ȥե뤬Ƥ뤫
  @file = readdir(DIR);                  #ǧ
  closedir(DIR);                         #ʤмԡgoto CHECK

  open(SEQF, "$seqf2");
  $number = <SEQF>;
  opendir(DIR, "$queue1"); 
  @file = readdir(DIR);
  closedir(DIR);    
  
  while(@file) {          #եǧ롣ʤС
    if ($file[0] =~ /fax0+$number.tif/) {
      $objfile = $file[0];
      open(FILE, "|/usr/local/bin/fax2ps $queue1$objfile > $queue2$num/$num.ps");           #եѴ

      close(FILE);

      open(FILE, "|/var/spool/fax/bin/ps2fax -o $queue2$num/$num.tif  $queue2$num/$num.ps"); #եѴ

      close(FILE);

      open(FILE, "|/bin/rm $queue2$num/$num.ps"); #ps

      close(FILE);

      open(FILE, "|echo X-FromFax: modemport@hostname > $queue2$num/header");
                                       
      close(FILE);                     
           
      system "chown wifax $queue2$num/*";

      system "chown wifax $queue2$num";

      chmod (0755, "$queue2/$num"); #ѡߥåѹ
  
      goto CHECK;         #̵λ
    }
    shift(@file);
  }
  goto CHECK;             # goto CHECK
}

sub get_work_dir{
    open(SEQF,"$seqfile");
    flock(SEQF,2);
    $num = <SEQF>;
   
    chop $num;

    close(SEQF);

    open(SEQF,">$seqfile");

    $num = $num + 1;
    print SEQF "$num\n";
    flock(SEQF,8);
    close(SEQF);

    mkdir("$queue2/$num",0300)||die "can\'t  make thread dir\.\n";
}
