#!/usr/bin/perl

# rat2html.pl
# Copyright 1996, Andrew Daviel, Vancouver-Webpages.com
# you may freely use this program
# Converts a very limited subset of PICS ratings files to an HTML form
# Usage: rat2html.pl < service.rat > service.gen.html
# 
# define CGI script to handle the generated form
$action = "/cgi-bin/mk-PICS.pl" ;
#$log = 1 ;
$| = 1 ;
#$debug = 1 ;
while (<STDIN>) {
  chop ; $line = $_ ;
  if ($debug) { print "$_\n"; }
  if (/\(\s*PICS-version/i) {
    s/.*PICS-version\s+//i ; s/\).*// ; $PICS_version = $_ ;
    if ($log) { print STDERR "Got PICS version $PICS_version\n"; }
  }
  if (/\(\s*rating-system\s+\"/i) {
    s/.*rating-system\s+\"//i ; s/\".*// ; $rating_system = $_ ; 
    if ($log) { print STDERR  "Got rating-system \"$rating_system\"\n"; }
  }
  if (/\(\s*rating-service\s+\"/i) {
    s/.*rating-service\s+\"//i ; s/\".*// ; $rating_service = $_ ;
    if ($log) { print STDERR  "Got rating-service \"$rating_service\"\n"; }
  }
  if (/\(\s*category/) { 
    &do_category ;
  }
  if (!$category) {
    if (/\(\s*name\s+\"/) {
      s/.*\(\s*name\s+\"// ; s/\".*// ; $rating_name = $_ ;
      if ($log) { print STDERR  "Got rating name \"$rating_name\"\n"; }
    }
    if (/\(\s*description\s+\"/) {
      s/.*\(\s*description\s+\"// ; s/\".*// ; $rating_description = $_ ;
      if ($log) { print STDERR  "Got rating description \"$rating_description\"\n"; }
    }
    if (/\(\s*icon\s+\"/) {
      s/.*\(\s*icon\s+\"// ; s/\".*// ; $rating_icon = $_ ;
      if ($log) { print STDERR  "Got rating icon \"$rating_icon\"\n"; }
    }
  } else { # category
    $_ = $line ; if (/\(\s*label\s+/) {
      $label++ ;
    }
    if ($label) {
      $_ = $line ; if (/\(\s*name\s+\"/) {
        s/.*\(\s*name\s+\"// ; s/\".*// ; $name[$label]=$_ ;
        if ($log) { print STDERR  "Got label name \"$_\"\n"; }
      }
      $_ = $line ; if (/\(\s*value\s+/) {
        s/.*\(\s*value\s+// ; s/\).*// ; $value[$label]=$_ ;
        if ($log) { print STDERR  "Got label value \"$_\"\n"; }
      }
      $_ = $line ; if (/\(\s*description\s+\"/) {
        s/.*\(\s*description\s+\"// ; s/\".*// ; $description[$label]=$_ ;
        if ($log) { print STDERR  "Got label  description \"$_\"\n"; }
      }
    } else { # if label
      $_ = $line ; if (/\(\s*name\s+\"/) {
        s/.*\(\s*name\s+\"// ; s/\".*// ; $name = $_ ;
        if ($log) { print STDERR  "Got category name \"$name\"\n"; }
      }
      $_ = $line ; if (/\(\s*description\s+\"/) {
        s/.*\(\s*description\s+\"// ; s/\".*// ; $description = $_ ;
        if ($log) { print STDERR  "Got category description \"$description\"\n"; }
      }
      $_ = $line ; if (/\(\s*transmit-as\s+\"/) {
        s/.*\(\s*transmit-as\s+\"// ; s/\".*// ; $transmit_as= $_ ;
        if ($log) { print STDERR  "Got transmit-as \"$transmit_as\"\n"; }
      }
      $_ = $line ; if (/\(\s*min\s+/) {
        s/.*\(\s*min\s+// ; s/\).*// ; $min = $_ ;
        if ($log) { print STDERR  "Got min \"$min\"\n"; }
      }
      $_ = $line ; if (/\(\s*max\s+/) {
        s/.*\(\s*max\s+// ; s/\).*// ; $max = $_ ;
        if ($log) { print STDERR  "Got max \"$max\"\n"; }
      }
    } # !label
  } # category
}
& do_category ;
print <<EOT;
<p>
<INPUT TYPE="submit" VALUE="Generate Rating Tags">
<INPUT TYPE="reset" VALUE="Reset to Defaults">
</FORM>
</BODY></HTML>
EOT

exit ;

sub do_category {
  if ($log) { print "Category\n"; }
  if ($category) {
    print <<EOT;
<h2>Category: $name</h2>
<!-- Transmitted as $transmit_as,
maximum value $max, minimum value $min<p> -->
EOT
if (!$name) { print STDERR "ERROR - No category name\n"; }
if (!$transmit_as) { print STDERR "ERROR - No transmit-as value\n"; }
if ($description) { print "Description:</b><br>\n $description\n" ; }

    $minv = $value[1] ; $maxv = $value[1] ;
    print "<ul>\n";
    for ($i =1 ; $i<=$label ; $i++) {
      #print "Name $name[$i] value $value[$i]\nDescription $description[$i]\n" ;
      #print "<li>$value[$i] <b>$name[$i]:</b> $description[$i]\n" ;
      print "<li><b>$name[$i]:</b> $description[$i]\n" ;
      if ($value[$i] > $max) { 
         print STDERR "Category $name value $value[$i] exceeds declared maximum $max\n";
      }
      if ($value[$i] < $min) {
         print STDERR "Category $name value $value[$i] exceeds declared minimum $min\n";
      }
      if ($value[$i] > $maxv) { $maxv = $value[$i] ; }
      if ($value[$i] < $minv) { $minv = $value[$i] ; }
    }
    if ($maxv != $max) { 
      print STDERR "Category $name maximum $maxv != declared max. $max\n"; }
    if ($minv != $min) { 
      print STDERR "Category $name minimum $minv != declared min. $min\n"; } 
    print "</UL>\n" ;
    print "<b>Select Limit:</b> <SELECT NAME=\"$name\">\n" ;
    for ($i =1 ; $i<=$label ; $i++) {
      if ($value[$i] == 0) {
        print "<OPTION VALUE=\"$transmit_as $value[$i]\" SELECTED>$name[$i]\n" ;
      } else {
        print "<OPTION VALUE=\"$transmit_as $value[$i]\">$name[$i]\n" ;
      }
    }
    print "</SELECT>\n";
    $label = 0 ; undef(@name) ; undef(@description) ;
    undef(@value) ; $description = "" ; $transmit_as = "" ; $name = "" ;
  } else { # category not set, do header
    print <<EOT;
<HTML><HEAD><TITLE>PICS Rating Generator: $rating_name</TITLE></HEAD>
<BODY>
<h1>PICS Rating Generator</h1>
For more information on the PICS system of content rating, see
<ul>
<li><a href="http://www.w3.org/PICS/">PICS at W3.org</a> (Master Site)
<li><a href="http://vancouver-webpages.com/PICS/">PICS</a> at
<a href="http://vancouver-webpages.com/">Vancouver Webpages</a> (HOWTO)
</ul>
This form was generated from a PICS rating file, which should be
accessible under the
<a href="$rating_service">Rating Service</a> given below.
Users must typically download this rating file and install it
in their browser in order to use this service.
<H2>Rating Name: $rating_name</H2>
PICS Version: $PICS_version<p>
<b>Description:</b>
$rating_description
<p>
<b>System:</b>
<a href="$rating_system">$rating_system</a>
<p>
<b>Service:</b>
<a href="$rating_service">$rating_service</a>
EOT
    if ($rating_icon) { 
      $_ = $rating_service ; s%/$%% ;
      print "<p>
<b>Icon:</b>
<a href=\"$_/$rating_icon\">$_/$rating_icon</a>\n";
    }
    print <<EOT;
<FORM METHOD="POST" ACTION="$action">
<b>For URL:</b>
<INPUT TYPE="text" NAME="URL" SIZE="50">
<SELECT NAME="SCOPE">
<OPTION>Site
<OPTION>Directory
<OPTION>File
</SELECT><br>
You may specify an entire site, a directory tree, or a single file.
The rating tag must be applied to the correct URL or site, otherwise
the pages will be inaccessible to all ratings users.<br>
For a site, this should be the classification of the majority of pages.
Pages which have a different rating must be given an explicit rating
as a file.

<INPUT TYPE="HIDDEN" NAME="VERSION" VALUE="$PICS_version">
<INPUT TYPE="HIDDEN" NAME="SERVICE" VALUE="$rating_service">
<INPUT TYPE="HIDDEN" NAME="SYSTEM" VALUE="$rating_system">
<INPUT TYPE="HIDDEN" NAME="ICON" VALUE="$rating_icon">
<INPUT TYPE="HIDDEN" NAME="NAME" VALUE="$rating_name">
<INPUT TYPE="HIDDEN" NAME="DESC" VALUE="$rating_description">

<p>
<b>By:</b>
<INPUT TYPE="text" NAME="BY" SIZE="50"> (email or contact URL)<br>
The person responsible for the rating, who may be contacted if
the ratings are challenged.
<h1>Categories</h1>
<p>
Category values are presented most-desireable-first.
Browsers will typically pass documents with one of the first
N possible values, where the number N is set by a parent or
password holder.
<p>
EOT

  }

  $category++ ; 
}
