#!/usr/bin/perl

# Perl-Routinen zum Manipulieren von CGI-Eingaben
# S.E.Brenner@bioc.cam.ac.uk
# $Header: /people/seb1005/http/cgi-bin/RCS/cgi-lip.pl,v 1.2 1.10.94 15:5:40 seb1005 Exp $

# Copyright 1993 Seven E. Brenner
# Unverffentlichte Arbeit
# Erlaubnis gewhrt, diese Bibliothek zu nutzen und zu verndern, 
# solange obenstehendes Copyright anerkannt wird, nderungen dokumentiert
# sind und fr jede Benutzung der Bibliothek bezahlt wird.

# ReadParse
# Liest GET- oder POST-Daten ein, konvertiert sie in unescaped Text
# und setzt in jeden Eintrag der Liste "@in" ein "key=value" ein.
# Auerdem erzeugt er Schlssel/Wert-Paare in "%in".
# Dabei wird "\0" zum Separieren von Mehrfachauswahlen verwendet.

# Wenn ein Paramter variable-glob (z.B. *cgi_input) an ReadParse
# weitergeleitet wird, dann ist die Information eher dort, als
# in $in, @in und %in gespeichert.

sub ReadParse {
  if (@_) {
    local (*in) = @_;
  }

  local ($i, $loc, $key, $val);

  # Text einlesen
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
      $in .= getc;
    }
  }

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Wandle Plus- in Leerzeichen um
    $in[$i] =~ s/\+/ /g;

    # Wandle %XX von hexadezimal- in alphanumerische Zahlen um
    $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

    # Splitte in Schlssel und Wert.
    $loc = index($in[$i],"=");
    $key = substr($in[$i],0,$loc);
    $val = substr($in[$i],$loc+1);
    $in{$key} .= '\0' if (defined($in{$key})); # \0 ist der multiple Separator
    $in{$key} .= $val;
        }

        &CreateFile(%in);
}

sub FindFileInTree {
        local ($dir, $filename) = @_;
        local ($file, $newfile, $name);

       if ( $found ){
          return $FoundFile;
       } else {

       $newfile = $dir . '/' . $filename . ".html";
        if (-e $newfile) {
                 $FoundFile = $newfile;
                 $found = 1;
                 return $FoundFile;
        }

        # Trenne Dateien von Verzeichnissen
        chdir $dir;
        opendir ( DIR, '.' ) || die "Kann $dir nicht ffnen\n";
        @filenames = readdir (DIR);
        closedir (DIR);

        # Trenne Dateien von Verzeichnissen
        local (@files);
        local (@subdirs);
        for ( @filenames )
        {
                 if ( -d $_  ) {
                    push (@subdirs, $_);
                 }
                 else {
                         ($fileName, $suffix ) = split (/\./. $_);
                         if ( $suffix ne "html" ) { next };
                         if ( /$filename/ ) {
                             $newfile = $dir . '/' . $_;
                             $FoundFile = $newfile;
                             $found = 1;
                             return $FoundFile;
                           }
                         }
                 }

        # Prfe Unterverzeichnisse auf weitere Dateien
        for (@subdirs ) {
                 next if $_ eq '.';
                 next if $_ eq '..';

                 chdir $_ || die "Kann Verzeichnis nicht wechseln nach $_\n";
                 open (PWDROC, "pwd|");
                 $name = <PWDPROC>;
                 chop $name;

                 # Rekursive Schleife fr weitere Dateien
                 &FindFileInTree ( $name, $filename );
                 chdir '..';
               }
        }
        return $FoundFile;
}

sub CreateFile {
        local ( %in ) = @_;

                 # ffne die Ausgabedatei
                 $key = 'Zeichnung Nummer:';
                 $drawing = $in{$key};
                 $key = 'Projekt:';
                 $project = $in{$key};
                 $key = 'Standort:';
                 $location = $in{$key};
                 $key = 'Name:';
                 $origin = $in{$key};
        $fname = '/instra/FTP/projects/' . $project . '/DRAWINGS/' . $location;
  $fname = $fname . '/' . $drawing . ' . html';
  open ( OUT, ">$fname" );
  chmod 0775, $fname;

  # Gib die Header-Information aus
  $key = "Titel:";
  print "Content-type: text/html\n\n";
  print "<HTML>\n";
  print OUT "<HTML>\n";
  print "<HEAD><TITLE>Test</TITLE></HEAD>\n";
  print OUT "<HEAD><TITLE>Test</TITLE></HEAD>\n";
  $ref = '<BODY><H1>ber diese Zeichnung: ' . $in{$key} . "</H1>\n";
  print $ref;
  print OUT $ref;

  # Gib den Kommentar aus
  $key = 'Kommentar:';
  $ref = '<P>' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  # Gib Info zur Quelle der Zeichnung aus
  print "<HR>\n";
  print OUT "<HR>\n";
  print "<H1>Informationen zur Zeichnung</H1>\n";
  print OUT "<H1>Informationen zur Zeichnung</H1>\n";

  $key = "Titel:";
  $ref = '<P>Titel: ' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  $ref = '<P>Zeichnung Nummer: ' . $drawing . "</P>\n";
  print $ref;
  print OUT $ref;

  $ref = '<P>Einsehbare Datei: <A HREF="http://instra.gsfc.nasa.gov/projects/' . $project . '/DRAWINGS/' . $location . '/' . $drawing . '.gif">' . $drawing . ".gif</A></P>\n";
  print $ref;
  print OUT $ref;

  $ref = '<P>Quelldatei: <A HREF="http://instra.gsfc.nasa.gov/projects/' . $project . '/DRAWINGS/' . $location . '/' . $drawing . '.dwg">' . $drawing . ".dwg</A></P>\n";
  print $ref;
  print OUT $ref;

  $ref = '<P>Plott-Datei: <A HREF="http://instra.gsfc.nasa.gov/projects/' . $project . '/DRAWINGS/' . $location . '/' . $drawing . '.plt">' . $drawing . ".plt</A></P>\n";
  print $ref;
  print OUT $ref;

  # Gib Info ber Zeichnung aus
  $key = 'Urheber:';
  $ref = '<P>Urheber: ' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  $key = 'Gre:';
  $ref = '<P>Gre: ' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  $key = 'Anzahl der Seiten:';
  $ref = '<P>Anzahl der Seiten: ' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  $key = 'Freigabe-Datum:';
  $ref = '<P>Freigabe-Datum: ' . $in{$key} . "</P>\n";
  print $ref;
  print OUT $ref;

  # Gib Verweise aus
  print "<HR>\n";
  print OUT "<HR>\n";
  print "<H1>Verweis</H1>\n";
  print OUT "<H1>Verweis</H1>\n";

  # Gib EO Info aus, wenn irgendeine eingegeben wurde
  $ref = '<P><A HREF="http://instra.gsfc.nasa.gov/cgi-bin/createEOs.pl/var=' . "$drawing,$project" . '">EOs verweist' . "</A></P>\n";
  print $ref;
  print OUT $ref;

  # Gib nchsthhere Montage aus
  $key = 'Nchsthhere Montage:';
  local(@assys) = split(/,/,$in{$key});

  if ( $#assys == -1 ) {
     $ref = "<P>Nchsthhere Montage : keine</P>\n";
     print $ref;
     print OUT $ref;
     }
     else {
       $dir = $project . '/DRAWINGS/';

       foreach $assy (@assys) {

         $FoundFile = "";
         $found = 0;
         $assy =~ s/ //;
         $top = "/instra/FTP/projects/" . $project . "/DRAWINGS/";
         $assyFound = &FindFileInTree ($top, $assy );

         if ( $assyFound eq "" ) {
           $ref = "<P>Nchsthhere Montage : $assy</P>\n";
           print $ref;
           print OUT $ref;
         } else {
           $top = "/instra/FTP";
             ($rest, $newFile) = split ( /$top/, $assyFound);
           $ref = '<P>Nchsthherer Montageverweis: <A HREF="http://instra.gsfc.nasa.gov/' . $newFile . '">' . $newFile . "</A></P>\n";
           print $ref;
           print OUT $ref;
         }
       }
     }

     # Gib Referenz-Zeichnungen aus
     $key = 'Referenz-Zeichnungen:';
     $dir = '/instra/FTP/projects/' . $project . '/DRAWINGS/' . $in{$key};
     if ( -e $dir ) {
       $ref = '<P>Referenz-Zeichnung: <A HREF="http://instra.gsfc.nasa.gov/projects/' . $project . '/DRAWINGS/' . $dir . '">' . $in{$key} . "</A></P>\n";
     }
     else {
       $ref = '<P>Referenz-Zeichnungen: ' . $in{$key} . "</P>\n";
     }
     print $ref;
     print OUT $ref;

     # Gib Referenz-Dokumente aus
     $key = 'Referenz-Dokumente:';
     $dir = '/instra/FTP/projects/' . $project . '/DOCUMENTS/' . $in{$key};
     if ( -e $dir ) {
           $ref = '<P>Referenz-Dokumente: <A HREF="http://instra.gsfc.nasa.gov/projects/' . $project . '/DOCUMENTS/' . $dir . '">' . $in{$key} . "</A></P>\n";
     }
     else {
       $ref = '<P>Referenz-Dokumente: ' . $in{$key} . "</P>\n";
     }
   print $ref;
   print OUT $ref;

   # Gib das andere Zeug aus
   print "<HR>\n";
   print OUT "<HR>\n";
   print "<H1>Vermischtes</H1>\n";
   print OUT "<H1>Vermischtes</H1>\n";
   $key = 'Schlsselwrter:';
   $ref = '<P>Schlsselwrter:' . $in{$key} . "</P>\n";
   print $ref;
   print OUT $ref;

   # Gib den HTML-Nachspann aus
   print "</BODY>";
   print OUT "</BODY>";
   print "</HTML>\n";
   print OUT "</HTML>\n";
   close ( OUT );

   # Die Quelldatei berkopieren
   $dest = '/instra/FTP/projects/' . $project . '/DRAWINGS/' . $location;
   $cmd = "cp /home/$project/$origin.dwg $dest/$drawing.dwg";
   print "<P>cmd: $cmd</P>\n";
#     system ($cmd);
   $cmd = "cp /home/$project/$origin.plt $dest/$drawing.plt";
   print "<P>cmd: $cmd</P>\n";
#     system ($cmd);
   $cmd = "cp /home/$project/$origin.gif $dest/$drawing.gif";
   print "<P>cmd: $cmd</P>\n";
#     system ($cmd);
}

# Gib den HTML-Header aus
  &ReadParse();

