#!/usr/bin/perl
use CGI ':standard';

  $get = param("get");
  $page = param("page");
  $addr = param("addr");
  $rate = param("rate");
  
  if ($rate)
  {
    if (!$page || !$addr)
  	{
    	print "Content-type: text/html\n\n";
        print "<html><head></head><body>
    	<H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>
	ERROR:page, address or get unspecified</body></html>";
    	exit;
	}
    if ($rate < 10) {
	
    	system "lockfile -0 -l 30 -s 0 /home/brejc8/public_html/mirror/list.lock";
    	system "sed s@\"${page}==${addr}.*\"@@ /home/brejc8/public_html/mirror/list >/home/brejc8/public_html/mirror/list.new";
    	system "cp /home/brejc8/public_html/mirror/list.new /home/brejc8/public_html/mirror/list";
    	system "rm -f /home/brejc8/public_html/mirror/list.lock";
    	print "Content-type: text/html\n\n";
    	print "<html><head></head><body>
    	<H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>
    	Thank you for your rating of the ${addr} mirror of ${page}.<br>
    	Due to it's low rating the mirror has been removed.</body></html>";
	exit;
	}
    
    system "lockfile -0 -l 30 -s 0 /home/brejc8/public_html/mirror/list.lock";
    system "sed s@\"${page}==${addr}==.*\"@\"${page}==${addr}==${rate}\"@ /home/brejc8/public_html/mirror/list >/home/brejc8/public_html/mirror/list.new";
    system "cp /home/brejc8/public_html/mirror/list.new /home/brejc8/public_html/mirror/list";
    system "rm -f /home/brejc8/public_html/mirror/list.lock";

    print "Content-type: text/html\n\n";
    print "<html><head></head><body>
    <H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>
    Thank you for your rating of the ${addr} mirror of ${page}</body></html>";
    exit;
  }
  
  
  if (!$get)
  {
    if (!$page || !$addr)
  	{
    print "Content-type: text/html\n\n";
        print "<html><head></head><body>
	<H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>
	ERROR:page, address or get unspecified</body></html>";
    exit;
	}
    $page =~ s@http:\/\/@@;
    $addr =~ s@http:\/\/@@;
    
    system "lockfile -0 -l 30 -s 0 /home/brejc8/public_html/mirror/list.lock";
    system "sed s@\"${page}==${addr}==.*\"@@ /home/brejc8/public_html/mirror/list >/home/brejc8/public_html/mirror/list.new";
    system "cp /home/brejc8/public_html/mirror/list.new /home/brejc8/public_html/mirror/list";
    open (FH, ">> /home/brejc8/public_html/mirror/list");
    print FH "$page==$addr==50\n";
    close (FH);
    system "rm -f /home/brejc8/public_html/mirror/list.lock";
    print "Content-type: text/html\n\n";
    print "<html><head></head><body>
    <H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>
    Thank you for your sumbition of <A href=\"http://${addr}\">${page}</A></body></html>";
    exit;
  }
  $get =~ s@http:\/\/@@;
  open (FH, "grep \"${get}==\" /home/brejc8/public_html/mirror/list |");
  $address = <FH>;
  
  if ($address) {
      print "Content-type: text/html\n\n";
      print "<html><head>";
      system "cat /home/brejc8/public_html/original/make_head";
      system "cat /home/brejc8/public_html/original/make_menu";
      print "<H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>Mirror list:<br><table>";
      while ($address) {
      $rating = $address;
      $rating =~ s/.*==//;
      $address =~ s/==$rating//;
      $address =~ s/.*==//;
      if (eval($rating < 1))  { $rating = 1;  }
      if (eval($rating > 100)){ $rating = 100;}
      $bad  = eval($rating * 0.9 - 1);
      $good = eval(100 -((100 - $rating) * 0.9 - 1));
      if (eval($bad < 0))  { $bad = 0;  }
      if (eval($good > 100)){ $good = 100;}
      $rating2 = eval(int($rating * 2));
      $rating = eval(int($rating));
      $address2 = substr($address,0,60);
      if (!($address eq $address2)) {$address2= "${address2}...";}
      
      print "<tr><td><A href=\"http://$address\">$address2</A></td>
      	     <td><IMG SRC=\"http://www.cs.man.ac.uk/~brejc8/mirror/bar.gif\" WIDTH=${rating2} HEIGHT=10 ></td>
      	     <td>${rating}%</td>
      	     <td><form action=\"http://solem.cs.man.ac.uk:8006/cgi-bin/mirror.pl\">
    	     <input type=\"hidden\" name=\"page\" value=\"${get}\">
             <input type=\"hidden\" name=\"addr\" value=\"${address}\">
             <input type=\"radio\"  name=\"rate\" value=\"$good\">Good, 
             <input type=\"radio\"  name=\"rate\" value=\"$bad\">Bad 
             <input type=\"submit\" value=\"Rate\">
	     </form></td></tr>";
      $address = <FH>;
      }
    close(FH);
    print "</table><br><br>You could add your own mirror for ${get}<br><br>
    <form action=\"http://solem.cs.man.ac.uk:8006/cgi-bin/mirror.pl\">
    <input type=\"hidden\" name=\"page\" value=\"${get}\">
    <input type=\"text\" name=\"addr\" value=\"Your Mirror\">
    <input type=\"submit\" value=\"Submit\">
    </form>";
      system "cat /home/brejc8/public_html/original/make_foot";
      print "</html>";
      exit;
      }
  else {
    print "Content-type: text/html\n\n";
    print "<html><head>";
    system "cat /home/brejc8/public_html/original/make_head";
    system "cat /home/brejc8/public_html/original/make_menu";
    print "<H2><A href=\"http://www.cs.man.ac.uk/~brejc8/mirror/index.html\">Distributed Mirror Project</A></H2>No mirrors found!<br><br>
    You could add your own mirror for ${get}<br><br>
    <form action=\"http://solem.cs.man.ac.uk:8006/cgi-bin/mirror.pl\">
    <input type=\"hidden\" name=\"page\" value=\"${get}\">
    <input type=\"text\" name=\"addr\" value=\"Your Mirror\">
    <input type=\"submit\" value=\"Submit\">
    </form>
    ";
    
    system "cat /home/brejc8/public_html/original/make_foot";
    print "</html>";
    close (FH);
    exit;
    }


  
