#!/usr/bin/perl

#need to set for security reasons (we'll launch _only_ current dir executables)
$ENV{PATH} = ".";

#$book_url = 'http://www.botik.ru/~scp/book/';
$book_url = '../index.html';

$query = $ENV{'QUERY_STRING'};

@examples = ("pal", "Example N1, Palindrom",
 "chpm_1"         , "Example N2, Chpm function (a)",
 "chpm_2"         , "Example N3, Chpm function (b)",
 "fact"           , "Example N4, Factorial",
 "trans"          , "Example N5, Translate a word",
 "trans-line"     , "Example N6, Translate line",
 "translate"      , "Example N7, Translate, the whole example",
 "correct"        , "Example N8, Correct",
 "sub-a-z"        , "Example N9, Sub-a-z",
 "order"          , "Example N10, Order",
 "fact-loop"      , "Example N11, Factorial (tail-recursive)",
 "fab-tr"         , "Example N12, Fab function (tail-recursive)",
 "chpm_3"         , "Example N13, Chpm function (c)",
 "pprout"         , "Example N14, Pretty print-out of expressions",
 "sub-a-z2"       , "Example N15, Sub-a-z (block variant)",
 "translate2"     , "Example N16, Translate (bury-dig variant)",
 "m_n_c"          , "Example N17, Missionaries and Cannibals",
 "sort1"          , "Example N18, Insertion sort",
 "sort2"          , "Example N19, Merge-sort",
 "sort3"          , "Example N20, Quick-sort",
 "path_in_graph"  , "Example N21, Paths in a Graph",
 "trarex"         , "Example N22, Translation of arithmetic expressions"
	     );


@forminputs = split (/&/, $query);
foreach $forminput (@forminputs) {
    ($name, $value) = split (/=/, $forminput);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack ("C", hex($1))/eg;
    $value =~ s/\r\n/\n/g;
    $form{$name} = $value;
}

if (defined ($form{'ex_index'})) {
    $ex_index = $form{'ex_index'};
}else{
    $ex_index = 0;
}

print 
    "Content-type: text/html\n\n" .
    "<HTML>\n<HEAD>\n<TITLE>Examples</TITLE>\n".
    "<META NAME=\"PRAGMA\" CONTENT=\"no-cache\">".
    "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=koi8-r\">\n";

print 
'<script language="JavaScript">'."\n".
'<!--'."\n".
'function open_win (str) {'."\n".
'  w1=window.open("","","scrollbars,dependent,resizable=yes,height=550,width=700");'."\n".
'  w1.document.location = str;'."\n".
'}'."\n".
''."\n".
'function View (sel) { '."\n".
'  var i = sel.selectedIndex;'."\n".
'  if (i == -1) return false;'."\n".
'  if (sel.options[i].value == "") {'."\n".
'    alert ("\'"+sel.options[i].text+"\' is empty yet...");'."\n".
'    return false;'."\n".
'  }'."\n".
'  open_win ("view.cgi?files="+sel.options[i].value);'."\n".
'}'."\n".
''."\n".
'function Edit (sel) { '."\n".
'  var i = sel.selectedIndex;'."\n".
'  if (i == -1) return false;'."\n".
'  open_win ("edit.cgi?files="+sel.options[i].value+"&index="+i);'."\n".
'}'."\n".
''."\n".
'function Compile (sel) { '."\n".
'  var i = sel.selectedIndex;'."\n".
'  if (i == -1) return false;'."\n".
'  open_win ("compile.cgi?files="+sel.options[i].value+".ref");'."\n".
'}'."\n".
''."\n".
'function Execute (sel) { '."\n".
'  var i = sel.selectedIndex;'."\n".
'  if (i == -1) return false;'."\n".
'  open_win ("execute.cgi?state=2&xmode=1&modules="+sel.options[i].value);'."\n".
'}'."\n".
'//-->'."\n".
    '</script>'."\n";

print
'</HEAD>'."\n".
    '<BODY TEXT="#005500" BGCOLOR="#FFFFFF" LINK="#000077" VLINK="#000077" ALINK="#000077">'."\n";

print
'<H3 align=center>'."\n".
'<font color="#007700">'."\n".
'Examples from the book: <br>'."\n".
"<a href=$book_url>"."\n".
'"REFAL-5 programming guide and reference manual"</a>'."\n".
'</font>'."\n".
'</H3>'."\n".
''."\n".
'<HR>'."\n".
'<center>'."\n".
'<table border=1 bgcolor=#e0e0c0>'."\n".
'<form method="post" name="ex_form" action="">'."\n".
'<tr><td>'."\n".
' &nbsp;'."\n".
' <b> Example:</b>'."\n".
' &nbsp;'."\n".
'</td>'."\n".
'<td>'."\n".
' <select name="example">'."\n";

for ($i=0;$i*2<$#examples;$i++) {
    print 
	"<option value=\"@examples[2*$i]\" ";
    if ($i == $ex_index) {
	print "selected";
    }
    print 
	"> @examples[2*$i+1]\n";
}
print
' </select>'."\n".
' &nbsp;'."\n".
'</td>'."\n".
'</tr>'."\n".
'<tr><td>'."\n".
' &nbsp;'."\n".
' <b> Action:</b>'."\n".
' &nbsp;'."\n".
'</td>'."\n".
'<td>'."\n".
' &nbsp;'."\n".
' <INPUT type=button value="View" onClick=\'View(document.ex_form.example)\'>'."\n".
' &nbsp;'."\n".
' ->'."\n".
' &nbsp;'."\n".
' <INPUT type=button value="Edit" onClick=\'Edit(document.ex_form.example)\'>'."\n".
' &nbsp;'."\n".
' ->'."\n".
' &nbsp;'."\n".
' <INPUT type=button value="Compile" onClick=\'Compile(document.ex_form.example)\'>'."\n".
' &nbsp;'."\n".
' ->'."\n".
' &nbsp;'."\n".
' <INPUT type=button value="Execute" onClick=\'Execute(document.ex_form.example)\'>'."\n".
'</td></tr>'."\n".
'</form>'."\n".
'</table>'."\n".
'<center>'."\n".
'<P>'."\n".
'<HR>'."\n".
'</BODY>'."\n".
    '</HTML>'."\n";

