#!@PERL@
#
# faxq-v.cgi -- show fax queue with details
# 
# Copyright 1998 Simone Demmel simone@greenie.net
#
# CVS: $Id: faxq-v.in,v 1.7 1998/11/20 15:06:35 gert Exp $
#
# $Log: faxq-v.in,v $
# Revision 1.7  1998/11/20 15:06:35  gert
# fix HTML syntax in error messages
#
# Revision 1.6  1998/11/20 10:06:45  gert
# move check for $fax_spool_out to wwwsub.pl
#
# Revision 1.5  1998/10/27 14:17:19  gert
# move error reporting to common subroutine
# check directory settings
# change comments to English
#
# Revision 1.4  1998/10/13 15:53:35  gert
# logo/<h1> header adjusted
#
# Revision 1.3  1998/10/07 13:06:49  gert
# added (preliminary) config file
#
# Revision 1.2  1998/09/04 15:17:50  gert
# scripts are now called .cgi
#
# Revision 1.1  1998/08/28 14:23:23  gert
# initial release
#

print "Content-type: text/html\n\n";

#
# Variablen 
#
$target = "";

# read configuration file
require "@CONFDIR@/wwwgui.conf";

# read common subroutine file
require "@LIBDIR@/wwwsub.pl";

#
# check, if all necessary variables are available
#

# check fax_outgoing
check_outgoing();

# check, if $indexpage is not empty
if ($indexpage eq "")
{
    errormessage( "\$indexpage : not configured - please look at the configuration-Files and configure the URL for my main-index-page (\$indexpage)");
}



#
# starting
#


### GET or POST?
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
  $query_string=$ENV{'QUERY_STRING'};
}
elsif (($ENV{'REQUEST_METHOD'} eq "POST") &&
       ($ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded"))
{
  read(STDIN,$query_string,$ENV{'CONTENT_LENGTH'});
}
else
{
#  &html_out(0,"<B>Submission method not supported!</B>\n");
};

### parse arguments
%args=();
foreach (split(/\&/,$query_string))
{
  if ( /^(\w+)=/ && ($key=$1) && ($value=$') )
  {
    $value =~ s/\+/ /go;
    $value =~ s/\%([0-9a-f]{2})/pack(C,hex($1))/eig;

   $args{"$key"}=$args{"$key"} . " " . $value if ($value!~/^\s*$/);
   $args{"$key"} =~ s/^\s*//o;
  }
}

if (defined $args{name}) { $name = $args{name}; }
else { print "<p> no Directory given!\n"; exit 0; }

#
# $name has our Fax-Directory - sanity check first!!
#

if ( $name !~ /^F\d+$/ || ! chdir "$fax_outgoing/$name" )
{
    print "<html><body><B>can't chdir to '$name': $!</B></body></html>";
    exit 0;
}

unless ( opendir DIRECTORY, "." )
{
    print "<html><body><B>can't read '$name': $!</B></body></html>";
    exit 0;
}

$target='JOB';
foreach $DATEI (readdir DIRECTORY)
{
  # search for our JOB(.*)
  if ($DATEI =~ /^JOB/) {$target = $DATEI;}
}
closedir DIRECTORY;

# now open $target and parse it
$phone='unknown'; $user='unknown'; $mail=''; $input=''; $pages='';
@stati=();

open (IN, "$target") || print "cannot open $target\n";
while (<IN>)
{
  chomp $_;
  @tmp = split / /, $_;
  if (/phone /) {$phone = $tmp[1];}
  if (/user /) {$user = $tmp[1];}
  if (/mail /) {$mail = $tmp[1];}
  if (/input (.*)$/) { $input = $1; }
  if (/pages (.*)$/) { $pages = $1; }
  if (/Status (.*)$/) { push @stati, $1; }
}
close (IN);

print <<EOF;
<html><head><title>Details zu $name</title></head>
<body bgcolor="white">

$www_logo
<h1 align="center">Faxmanager - Details zu Job $name</h1><br clear="all">
<hr>
<h2>Fax-Nummer: $phone</h2>

<table>
<tr><td>User</td><td>$user</td></tr>
<tr><td>EMail</td><td>$mail</td></tr>
<tr><td>Eingabe</td><td>$input</td></tr>
<tr><td>Seiten</td><td>$pages</td></tr>
</table>

<p>

Fehlversuche:
<p>
EOF

for ($i=0; $i<=$#stati; $i++)
{
  print "$stati[$i]<br>\n";
}

print <<EOF;
<p>
<hr>
<a href="faxq.cgi">Übersicht Faxe</a><br>
<a href="$indexpage">^Hauptmenue</a>
</body>
</html>
EOF

exit 0
