#!/usr/bin/perl
#
# Main Rcgi script... see documentation for licence
# Copyright (C) 2001 MJ Ray and University of East Anglia
# Release 4 (beta)

# Paths
my ($url,$logo,$doc,$allowedhost);
# Our script URL
$url = "/Rcgi/go";
# R logo URL
$logo = "/Rdoc/doc/html/logo.jpg";
# R documentation URL
$doc = "/Rdoc/doc/html/";
# what websites can send jobs
$allowedhost = 'mth\.uea\.ac\.uk';

# No user serviceable parts below here... see Rcgi/SystemPrograms.pm
use strict;
use Rcgi::Session;
use CGI;

my $q = new CGI;

sub header {
  # Print the header
  return $q->header.'
<html><head><title>Rcgi</title>
</head><body bgcolor="#ffffff" text="#000000" link="#0000c0" vlink="#0000c0">
<h1><img src="'.$logo.'">Rcgi</h1>
<h4>Release 4 - "Weston"</h4>';
}

sub footer {
  return "<hr><a href=\"$doc\">R language help</a></body></html>";
}

# Basic security checks
if (($q->referer()) && ($q->referer() !~ /$allowedhost\//)) {
  print header.'<b>Sorry, the website which sent you here is not allowed
to run scripts here.  Email the author of the previous page and ask them
to look into it.</b>'.footer;
}
# Are we running a script?
elsif ($q->param('INPUT')) {
  my ($in);
  $in=join("\n",$q->param('INPUT'));
  my $rsession = new Rcgi::Session;
  $rsession->execute($in);
  my $resultsfile = $rsession->text();
  my $results = join(" ",<$resultsfile>);
  $resultsfile->close;
  $results =~ s/&/&amp;/g;
  $results =~ s/</&lt;/g;
  $results =~ s/>/&gt;/g;
  my $id = $rsession->get_session_id();
  print header.'
<hr /><b>Program Output</b><pre>*** Rcgi reference '.$id
.$results.'</pre>

<hr /><b>Graphical Output</b>:
    <a href="'.$url.'?ps=1&amp;id='.$id.'">High Quality PostScript</a> (fast)
    or
    <a href="'.$url.'?gif=1&amp;id='.$id.'" target="_blank">Low Quality GIF</A> (slow)
    <br>
    PostScript may need an application or plugin to be installed to view.
    GIF should be viewable by most browsers.

<hr /><b>Program Input</b>:
<form method="post" action="'.$url.'">
<textarea name="INPUT" rows="5" cols="64">
'.$in.'</textarea><br><input type="SUBMIT" value="Go!">'
.footer;

}
elsif ($q->param("ps")) {
  print $q->header(-type=>"application/postscript");
  my $rsession = new Rcgi::Session($q->param("id"));
  my $psfh = $rsession->postscript();
  print <$psfh>;
  $psfh->close;
}
elsif($q->param("gif")) {
  my $rsession = new Rcgi::Session($q->param("id"));
  my $pages = $rsession->render();
  $| = 1;
  print header."
<H4>Converting Postscript to GIF...</H4>

<P>This may take a while - but please allow the process to finish now
that you've started it.  Also, please be considerate to other users and
try to avoid creating more pages of output than you need to.  Thanks.</P>

<P>Once loaded, Netscape users can print one page at a time by
right-clicking on the chart they wish to print, selecting \"View
Image\" and then pressing the print button.</P>

<P>There are $pages pages in this output.</P>
";
  STDOUT->flush();

  my $i;
  for $i (1..$pages) {
    print "<br /><img src=\"".$url."?gifpg=".$i."&amp;id=".$q->param("id")."\" /><br />";
  }
  print footer;
}
elsif ($q->param("gifpg")) {
  print $q->header(-type=>"image/gif");
  my $rsession = new Rcgi::Session($q->param("id"));
  my $psfh = $rsession->gif($q->param("gifpg"));
  print <$psfh>;
  $psfh->close;
}
else{
  print header.'
<hr>
<form method="post" action="'.$url.'">
<textarea name="INPUT" rows="5" cols="64">
</textarea><br><input type="SUBMIT" value="Go!">'
.footer;
}
