#![]{perl}[]
#
# Huuh, we are perl!
#
# Copyright (c) 1994 by Hochschulrechenzentrum der Universit"at Gie"sen
# All rights reserved.
#
# Author: Markus Porto
#         Markus Porto@physik.uni-giessen.de
#
# This software may be freely copied, modified, and redistributed
# provided that this copyright notice is preserved on all copies.
#
# You may not distribute this software, in whole or in part, as part of
# any commercial product without the express consent of the author.
#
# There is no warranty or other guarantee of fitness of this software
# for any purpose. It is provided solely "as is".
#
# Diese Datei wird vom Kochbuch des TeX--Service benutzt.
#
# Dazu wird die "ubergebene Environmentvariable $CONTENT_LENGTH ausgewertet
# und die Eingabe dann von STDIN gelesen.
# Zus"atzlich wird die Environmentvariable $QUERY_STRING ausgewertet.
#
# Dies ist der nicht sicherheitsrelevante Teil des Skripts. Dieser Teil ruft
# "uber das Programm security den sicherheitsrelevanten Teil auf. Dieser
# sicherheitsrelevante Teil l"auft dann unter der UID/GID nobody.nobody.
#
# Parameter:
# input=
#  Der zu "ubersetzende Text.
# output={dvi,dvips,inline}
#  Art der Ausgabe
# zoom={10..1000}
#  Vergr"o"serungfaktor in Prozent
# cropped={on,off}
#  Inline--Grafik auf sichtbares beschr"anken ja/nein
# icon=
#  Welches Icon anzeigen
#
# Revision: 30. September 1994
#

# Behandlung des TERM--Signals
$SIG{'TERM'}= handler;

# das Verzeichnis
$dirname= $ENV{"PATH_TRANSLATED"};

# Neuladen verhindern
if( $ENV{"CONTENT_TYPE"} eq "" )
{
  print `[]{cgibin}[]/tex/reload`;
  exit 0;
}

# der Typ des Textes
print "Content-type: text/html\n\n";

# pstoppm.ps kopieren
`/bin/cp $dirname/.tmplatex/pstoppm.ps /tmp/tmppstoppm$$.ps`;
chmod 0644, "/tmp/tmppstoppm$$.ps";

# latex.insecure sicher starten
print `[]{cgibin}[]/tex/security []{cgibin}[]/tex/latex.insecure`;

# Dateien kopieren
`/bin/cp /tmp/tmplatex$$* $dirname/.tmplatex`;
`/bin/rm /tmp/tmplatex*`;

# pstoppm.ps l"oschen
`/bin/rm /tmp/tmppstoppm$$.ps`;

# alte Dateien l"oschen
&remove_old_files;

sub remove_old_files
{
  # Verzeichnis nach Dateien tmplatex*.* durchsuchen
  opendir( DIR, $dirname . "/.tmplatex");
  @allfiles= grep( /^tmplatex.*\..*$/, readdir( DIR));
  closedir( DIR);
  
  # Datei, die einen Tag oder "alter sind l"oschen
  for( @allfiles)
  {
    chmod 0666, $_;
    unlink if ((-M $_) >= 1);
  }
}

sub handler
{
  exit( 0);
}
