#!/usr/bin/perl
use lib 'lib';


use rlconfig;
use gentools;

print "Content-type: text/html\n\n";
gentools::inittests;

if ($data{'offset'} || $data{'offset'} eq '0')	{
  if ($data{'offset'} =~ /\D/)	{
    push (@error, '<p class="error">The &quot;offset&quot; argument shall consist of'
    . ' digits only. The list below starts with a randomly chosen site.</p>');
    $data{'offset'} = int (rand (scalar @activesites));
  } elsif ($data{'offset'} > scalar @activesites - 1 || $data{'offset'} < 0)	{
    push (@error, '<p class="error">Incorrect &quot;offset&quot; value. (Should have'
    . ' been a number between 0 and ' . scalar @activesites - 1 . ".)\n"
    . ' The list below starts with a randomly chosen site.</p>');
    $data{'offset'} = int (rand (scalar @activesites));
  }
  &createlist;
}
$data{'offset'} = 0;
for (@activesites)	{
  if ($_ =~ /^$data{'siteid'}\t/)	{
    $activesite = 1;
    last;
  } else	{
    $data{'offset'} ++;
  }
}
if (!$data{'siteid'})	{
  $data{'offset'} = int (rand (scalar @activesites));
  &createlist;
} elsif (!$activesite)	{
  for (@inactivesites)	{
    if ($_ =~ /^$data{'siteid'}\t/)	{
      $inactivesite = 1;
      push (@error, "<p class=\"error\">Site ID &quot;$data{'siteid'}&quot; is not active."
      . ' The list below starts with another, randomly chosen, site.</p>');
      $data{'offset'} = int (rand (scalar @activesites));
      &createlist;
      last;
    }
  }
  if (!$inactivesite)	{
    push (@error, "<p class=\"error\">Site ID &quot;$data{'siteid'}&quot; does not exist"
    . ' in this ring. The list below starts with a randomly chosen site.</p>');
    $data{'offset'} = int (rand (scalar @activesites));
    &createlist;
  }
} else	{
  gentools::addgenhits ($data{'siteid'});
  &createlist;
}


sub createlist	{
  if (scalar @activesites > $sitesperlistpage)	{
    $result = '<h4>' . $sitesperlistpage . ' sites out of ' . scalar @activesites . "</h4>\n";
    $prevoffset = $data{'offset'} - $sitesperlistpage
    + ($sitesperlistpage > $data{'offset'} ? scalar @activesites : 0);
    $nextoffset = $data{'offset'} + $sitesperlistpage
    - ($data{'offset'} + $sitesperlistpage > scalar @activesites - 1 ? scalar @activesites : 0);

    $links = qq~
<p style="text-align: center">
[ <a href="$cgiURL/$action?ringid=$ringid;offset=$prevoffset">Previous $sitesperlistpage sites</a> |
<a href="$cgiURL/$action?ringid=$ringid;offset=$nextoffset">Next $sitesperlistpage sites</a> ]
</p>~;

  } else	{
    $result = '<h4>' . scalar @activesites . ' site'
    . (scalar @activesites > 1 ? 's' : '') . "</h4>\n";
  }
  $result .= "@error\n<dl>\n";
  unshift (@activesites, splice (@activesites, $data{'offset'}));
  splice (@activesites, $sitesperlistpage);
  opendir(DIR, $cgipath) || exit print "Can't open $cgipath<br />\n$!";
  @execfiles = grep { !/^\./ && (/\.pl$/i || /\.cgi$/i) } readdir(DIR);
  closedir DIR;
  for (@execfiles)	{
    $goto = $_ if $_ =~ /^goto\./i;
  }

  for (@activesites)	{
    @sitevalues = split (/\t/, $_);
    for (@sitenames)	{
      $$_ = shift (@sitevalues);
      gentools::htmlize ($$_);
    }

    $result .= qq~
<dt><a href="$cgiURL/$goto?ringid=$ringid;siteid=$siteid">
$sitetitle</a></dt>
<dd>$sitedesc</dd>
~;

  }
  $result .= "\n</dl>";
  $result .= $links;
  gentools::mainhtml;
  exit;
}



