#! /usr/local/bin/perl
# 2ascii -- Version 1.00b 98.01.02 12:16
# (c) 1997, 1998 Hellenic Resources Institute, Inc.
#
# This script converts URL's from Elot928 Greek to ASCII Greek.
# Usage: GET 2ascii?URL

$Banner = "/iis/www/htdocs/includes/banner.inc";
$Footer = "/iis/www/htdocs/includes/map.inc";
$webcopy = "/usr/local/bin/webcopy";
$opendouble = "\140\140";
$closedouble = "\047\047";
$copyright = "(c)";
$registered = "(R)";
$trademark = "(TM)";
$banner = `cat /hrnet/iis/www/htdocs/includes/banner.inc`;
$footer = `cat /hrnet/iis/www/htdocs/includes/map.inc`;

$myname = $ENV{SCRIPT_NAME};
$myname = "http://www.hri.org" . $myname;

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
  $url = $ENV{'QUERY_STRING'};

  if($url =~ /\w/) {
    $url =~ tr/+/ /;
    $url =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $url =~ s/^q=//;
  } else {
    &return_form("Elot928 to ASCII Greek Converter");
    exit;
  }
} else {
  &return_form("Elot928 to ASCII Greek Converter");
  exit;
}

print "Content-type: text/html\n\n";
chdir("/tmp");
AGAIN:
@message = `$webcopy -apfoc -b2ascii$$ -t0 "$url"`;
if(open(LOG, "2ascii$$.log")){
  @log = <LOG>;
  close(LOG);
  unlink("2ascii$$.log");
  @temp = grep(/(reject\-remote|301 Moved Permanently)/i, @log);
  if(@temp) {
    $temp[0] =~ s/^getting\s*//gi;
    $temp[0] =~ s/^reference\s*//gi;
    $temp[0] =~ s/\s*reject\-remote\n$//gi;
    $temp[0] =~ s/\s*301\s*moved\s*permanently\n$/\//gi;
    $temp[0] =~ s/\:80\//\//gi;
    $url = $temp[0];
    goto AGAIN;
  }
  @temp = grep(/(reference null reject-invalid)/i, @log);
  if(@temp) {
    $url .= "/";
    goto AGAIN;
  }
  @temp = grep(/(404)/i, @log);
  if(@temp) {
    $notfound = 1;
    goto OUT;
  }
  @temp = grep(/^getting/, @log);
  $base = $temp[$#temp];
  $base =~ s/^getting\s*//gi;
  $base =~ s/\s*\n$//gi;
  $base =~ s/\s.*$//gi;
  $base =~ s/\:80\//\//gi;
  $base =~ s/$myname\?//;
  $base =~ s/\/[^\/]*$/\//;
} else {
  $base = $url;
  $base =~ s/$myname\?//;
  $base =~ s/\/[^\/]*$/\//;
}
$system = $base;
$system =~ s/^(http\:\/\/[^\/]*).*$/$1\//gi;
print "<BASE HREF=\"$base\">\n";
# print "\[<FONT SIZE=-1><A HREF=\"$base\">Returned: $base</A></FONT>\]\n";
if(@message) {
  $i = 0;
  while($i <= $#message) {
    $_ = @message[$i];
    tr [\213\221\222\233\242\264\266-\376] [\`\`\'\'A\'A\267EHI\273O\275YWiABGDEZHQIKLMNJOPR\322STYFXCWIYaehiyabgdezhqiklmnjoprsstyfxcwiyoyw] ;
    s/\253/$opendouble/ge;
    s/\273/$closedouble/ge;
    s/\251/$copyright/ge;
    s/\256/$registered/ge;
    s/\231/$trademark/ge;
    s/(href\s*=\s*\")([^\#\/ ])/$1$myname?$base\/$2/gi;
    s/(href\s*=\s*\"\s*\+\s*\"\\\"\"\s*\+\s*\")([^\#\/ ])/$1$myname?$base\/$2/gi;
    s/href\s*=\s*\"#/href=\"$myname\?$url\#/gi;
    s/href\s*=\s*\"\//href=\"$myname\?$system\//gi;
    s/(frame.*src\s*=\s*\")([^\#\/])/$1$myname?$base\/$2/gi;
    s/frame.*src\s*=\s*\"#/frame src=\"$myname\?$url\#/gi;
    s/frame.*src\s*=\s*\"\//frame src=\"$myname\?$system\//gi;
    s/($myname\?)($base\/)(http\:\/\/)/$1$3/gi;
    s/([^:])\/{2,}/$1\//gi;
    s/\/\w*\/\.\.//gi;
    s/\/\w*\/\.\.//gi;
    s/\/\.\//\//gi;
    s/($myname\?)($base\/*)(mailto\:)/$3/gi;
    s/($myname\?)($base\/*)(ftp\:)/$3/gi;
    s/($myname\?)($base\/*)(telnet\:)/$3/gi;
    s/($myname\?){2,}/$myname\?/gi;
    print;
    $i++;
  }
  print "<PRE>\n";
  print "\[<FONT SIZE=-1>Converted from: <A HREF=\"$url\">$url</A> by <A HREF=\"$myname\">$myname</A></FONT>\]\n";
  print "</PRE>\n";
} else {
OUT:
  print <<EOT;
<HTML>
<HEAD>
<TITLE>URL Unobtainable</TITLE>
</HEAD>
<BODY>
$banner
<H2>URL Unobtainable</H2>
EOT
  if($notfound == 1) {
    print "The specified URL <A HREF=\"$url\">$url</A> does not exist (code 404 returned).";
  } else {
    print "The specified URL <A HREF=\"$url\">$url</A> seems to be unobtainable.";
    print "Please check your reference, or retry later.";
  }
  print <<EOT2;
$footer
</BODY>
</HTML>
EOT2
}
exit;

sub return_form {
  $document_title = $_[0];
  print "Content-type: text/html\n\n";
  print "<HTML>\n";
  print "<HEAD>\n";
  print "<TITLE>$document_title</TITLE>\n";
  print "</HEAD>\n";
  print "<BODY>\n";
  if(open(BANNER, $Banner)){
    while(<BANNER>) {
      print $_;
    }
    close(BANNER);
  }
  print "<H2>$document_title</H2>\n";

  print "<P>This page allows you to enter a URL which is in Elot928 Greek and to\n";
  print "view it using ASCII Greek.  The conversion is done automatically on HR-Net.\n";

  print "<P>Enter the URL you wish to view in ASCII Greek, and click the \"Convert\" button:\n";
  print "<FORM ACTION=\"$myname\">\n";
  print "<STRONG>URL:</STRONG>\n";
  print "<INPUT TYPE=text NAME=\"q\">\n";
  print "<INPUT TYPE=submit VALUE=\"Convert\">\n";
  print "<P><B>Tip:</B>\n";
  print "<FONT SIZE=-1>Please remember that installing Greek with your WWW\n";
  print "browser has never been easier.  Full instructions are available at <A\n";
  print "HREF=\"http://www.hri.org/fonts\">http://www.hri.org/fonts</A>.  If you have\n";
  print "been unsuccessful with a prior installation, or run into trouble with our\n";
  print "instructions, you can always write to <A\n";
  print "HREF=\"mailto:fonts\@hri.org\">fonts\@hri.org</A> for assistance.</FONT>\n";
  if(open(FOOTER, $Footer)){
    while(<FOOTER>) {
      print $_;
    }
    close(FOOTER);
  }
  print "</BODY>\n";
  print "</HTML>\n";
}
