#!/usr/bin/perl
#
# WIDE InternetFAX
#
# file: ifax_make_spool
# 
# Written by Masahiko KIMOTO <kimoto@ohnolab.org>.
# Copyright (c) 1998 by 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_make_spool,v 1.4 1999/03/08 04:22:58 kimoto Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# ƥѥסǥ쥯ȥ⥸塼
# ܺ٤README.ifax_make_spool.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'};

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

$start="10000";

$ps2fax = "/var/spool/fax/bin/ps2fax";

$seqn = $start;

chop($uid = 'id -u');
if ($uid != 0) {
	print "This script must be run by root.\n";
	exit;
}

while (1) {
	while (-d "$spooldir/$seqn") {
		$seqn ++;
	}

	last if (mkdir("$spooldir/$seqn",0755) == 1);
}

$newdir = "$spooldir/$seqn";

umask(022);

foreach (@ARGV) {
print	`cp -f $_ $newdir`;
}

chdir("$newdir");
mkdir ("etc",0755);
print `cp -f /var/spool/fax/etc/setup.cache etc`;

foreach (@ARGV) {
	print 	`$ps2fax $_`;
}

`rm -rf etc *.ps`;
`mv -f ps.fax image.tiff`;

print `chown -R ifax.wifax $newdir`;

print "new slot is $seqn\n";
