<?

	$file_name = "/home/abu-jeib/temp/homepages2.txt";
	$a = array();
        @ $f = fopen($file_name,'r');
        if (!$f)
        {
		if (!file_exists($file_name))
			echo "File \"$file_name\" does not exist.<br>\n";
		else
        		echo "<h2 align=center>Can't open file
				 \"$file_name\" for reading.</h2><BR>\n";
                exit;
        }
	$b = array();
        $line = trim(fgets($f));
        while (!feof($f))
        {
        	if (!empty($line))
		{
			// Separate the name from the URL
			list($name,$url) = split(';',$line,2);
                        $name = trim($name);
                        $url = trim($url);
			
			 /*
                        Before sorting, make sure the capitalization is correct because
                        that will affect the sorting.
                        */
			$name=ucwords(strtolower($name));

			/* Get the last name and leave the first
			   name and the middle initial as one unit.
			*/
			$name_reversed = strrev($name);
			list($last_reversed,$first_middle_reversed) = split(" ", $name_reversed,2);

			/*
			Store the names in the indices of array b
			and make the URL's the values of b.
			*/
			$last_name = strrev($last_reversed);
			$first_middle = strrev($first_middle_reversed);
			$e = $last_name.$first_middle;
			$b[$e] = array();
			$b[$e][0] = $name;
			$b[$e][1] = $url;
		}
                $line = trim(fgets($f));
        }
        fclose($f);
	ksort($b);
	
	echo "<html><head></head><body>";
        echo "<h1 align=center>Student Homepages</h1><ol>\n";
	foreach ($b as $k => $v)
	{ 
			//echo $v, "<br>";
                        echo "<li><a href='$v[1]'>$v[0]</a>\n";
	}

	echo "</ol></body></html>";

?>
