sub padZeros
# Pad $number with zeros up to a length of $length
{
	local ($number, $length, @args) = @_;
	my $num_length = length ($number);
	my $num_zeros = $length - $num_length;
	my $zeros = "";
	for ($i=1;$i<=$num_zeros;$i++) {
		$zeros = $zeros . "0";
	}
	$number = $zeros . $number;
	return $number;
}

sub WriteADTpage
# Convert an eprint with ID $ID to HTML and store it as file $FName
{
	local ($FName, $ID, @args) = @_;

# --------------------------- GET THE EPRINTS METADATA -----------------------------
	# These values would be dragged out from the database, not assigned
	my $dbh = DBI->connect("DBI:mysql:$DB_NAME:$DB_HOST", $DB_USER, $DB_PASS);
	my $query = $dbh->prepare("SELECT * FROM archive WHERE eprintid ='$ID'");
	$query->execute;
	my $numrows = $query->rows;

	if ($numrows > 0) {
		$row = $query->fetchrow_hashref;
                $title = $row->{title};
                $abstract = $row->{abstract};
                $publisheddate = $row->{date_effective};
                $keywords = $row->{keywords};
                $dept = $row->{department};
                $thesis_type = $row->{thesis_type};
		$depositID = $row->{userid};
		$depositedon = $row->{datestamp};
        }
	else {
#		die "No records found in table 'archive' for eprint id $ID\n";
		return 0;
	}

	$query->finish;

	my $query = $dbh->prepare("SELECT * FROM archive_creators WHERE eprintid ='$ID'");
	$query->execute;
	my $numrows = $query->rows;

	if ($numrows > 0) {
		$row = $query->fetchrow_hashref;
                $lastname = $row->{creators_family};
                $firstname = $row->{creators_given};
                $salutation = $row->{creators_honourific};
        }
	else {
#		die "No records found in table 'archive_creators' for eprint id $ID\n";
		return 0;
	}

	$query->finish;

	my $query = $dbh->prepare("SELECT * FROM users WHERE userid ='$depositID'");
	$query->execute;
	my $numrows = $query->rows;

	if ($numrows > 0) {
		$row = $query->fetchrow_hashref;
                $depositedby = $row->{name_family} . ", " . $row->{name_given};
        }
	else {
#		die "No records found in table 'users' for user id $depositID\n";
		return 0;
	}

	$query->finish;

	my $query = $dbh->prepare("SELECT * FROM document WHERE eprintid ='$ID'");
	$query->execute;
	my $numrows = $query->rows;

	if ($numrows > 0) {
		$i = 0;
		undef @documentIDs;
		undef @documentFORMATs;
		undef @documentFORMATDESCs;
		undef @documentMAINs;
		while ($row = $query->fetchrow_hashref) {
                	$documentIDs[$i] = $row->{docid};
                	$documentFORMATs[$i] = $row->{format};
                	$documentFORMATDESCs[$i] = $row->{formatdesc};
                	$documentMAINs[$i] = $row->{main};
			$i++;
		}
        }
	else {
#		die "No records found in table 'document' for eprint id $ID\n";
		return 0;
	}

	$query->finish;

	my $query = $dbh->prepare("SELECT * FROM archive_subjects WHERE eprintid ='$ID'");
	$query->execute;
	my $numrows = $query->rows;

	if ($numrows > 0) {
		$i = 0;
		undef @subjectIDs;
		while ($row = $query->fetchrow_hashref) {
                	$subjectIDs[$i] = $row->{subjects};
			$i++;
		}
        }
	else {
#		die "No records found in table 'archive_subjects' for eprint id $ID\n";
		return 0;
	}

	$query->finish;

	$dbh->disconnect;

	# OK, so now generate derived variables, first authors

	$author = $firstname . ' ' . $lastname;
	$authorrev = $lastname . ', ' . $firstname;
	$authorallrev = $lastname . ', ' . $salutation . ' ' . $firstname;

	# Now the URI

	$URI = $DOCS_URL . padZeros ($ID, 8) . "/";

	# Convert thesis_type to actual text

	if ($thesis_type eq "phd") {
		$thesis_type = "PhD thesis";
	}
	elsif ($thesis_type eq "honours") {
		$thesis_type = "Honours thesis";
	}
	elsif ($thesis_type eq "cmaster") {
		$thesis_type = "Coursework Master thesis";
	}
	elsif ($thesis_type eq "rmaster") {
		$thesis_type = "Research Master thesis";
	}
	elsif ($thesis_type eq "other") {
		$thesis_type = "Other Degree thesis";
	}

	# Convert abstract so that blank lines become <BR>

	$converted_Abstract = $abstract;
#	$converted_Abstract =~ s/(\n)/<BR>/g; # Perhaps leave that up to ADT to format.
	$converted_Abstract =~ s/"/&quot;/g;
	$abstract = $converted_Abstract;

# ------------------------ START WRITING THE FILE --------------------------

	my $filename = "> " . $FName;
	open(FILEHANDLE, $filename) or die "Failed to open/create file: $FName\n";
	# So write the file
print FILEHANDLE 
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">
<html><head>";
			
# --------------------------- WRITE THE TITLE -----------------------------

print FILEHANDLE "<title>$title</title>\n";

# --------------------------- WRITE THE ADT METADATA -----------------------------

print FILEHANDLE "<meta name=\"DC.Title\" content=\"$title\"/>\n";
	
print FILEHANDLE "<meta name=\"DC.Creator\" content=\"$authorrev\"/>\n";
print FILEHANDLE "<meta name=\"DC.Creator.personalName\" content=\"$authorrev\"/>\n";
	
	# generate sequence of keywords
	if ($keywords =~ ",") {
		@keywordslist = split (/,/,$keywords);
	}
	else {
		@keywordslist = split (/\ /,$keywords);
	}
	my $nokeywordslist = @keywordslist;
	for ($i = 0; $i < $nokeywordslist; $i++) {
		$keywordslist[$i] =~ s/^( +)//; # remove all spaces at start
		print FILEHANDLE "<meta name=\"DC.subject\" content=\"$keywordslist[$i]\"/>\n";
	}
	
print FILEHANDLE "<meta name=\"DC.Description.abstract\" content=\"$abstract\"/>\n";
print FILEHANDLE "<meta name=\"DC.Date\" scheme=\"W3CDTF\" content=\"$publisheddate\"/>\n";
print FILEHANDLE "<meta name=\"DC.Date.valid\" scheme=\"W3CDTF\" content=\"$publisheddate\"/>\n";
print FILEHANDLE "<meta name=\"DC.Language\" scheme=\"RFC1766\" content=\"en\"/>\n";
print FILEHANDLE "<meta name=\"DC.Publisher\" content=\"$UNI_TEXT, $dept\"/>\n";
print FILEHANDLE "<meta name=\"DC.Rights\" content=\"$COPY_URL\"/>\n";
print FILEHANDLE "<meta name=\"DC.Rights\" content=\"(c) Copyright $publisheddate $author\"/>\n";
print FILEHANDLE "<meta name=\"DC.Identifier\" scheme=\"URI\" content=\"$URI\"/>\n";

#// --------------------------- WRITE THE PAGE -----------------------------
print FILEHANDLE
"</head>

 <body bgcolor=\"#ffffff\" text=\"#000000\">
    
        <!-- Thesis title -->
		<h1 class=\"pagetitle\">$title</h1>
        <!-- Citation -->
		<p>
		$authorallrev ($publisheddate) <i>$title</i>. $thesis_type, $dept, $UNI_TEXT.</p>
		<!-- ID code -->
		<p>Eprints ID Code: $ID</p>
  
  </body>
</html>\n";
#// END OF FUNCTION
}

1;
