#!/bin/bash
# Display in HTML the account request status. (Pending/Accepted/Rejected)
# Also do some cleanup.

## Config:
#Number of days the Rejected and Accepted records are to be kept
Rdays=6
Adays=6
Pdays=14
#To delete notes.* files:
Ndays=0
wd=/home/www/home/webpage2/acc-req/data
webdir=/home/www/home/webpage2
tmpnotes=notes.$$

## Start:

cd $wd

# First remove older files
find . -name "*.acc" -mtime +${Adays} \
	-exec grep -q -F "N_STATUS='A'" {} \; -exec rm -f {} \;
find . -name "*.acc" -mtime +${Rdays} \
	-exec grep -q -F "N_STATUS='R'" {} \; -exec rm -f {} \;
find . -name "*.acc" -mtime +${Pdays} -exec rm -f {} \;
find . -name "notes.*" -mtime +${Ndays} -exec rm -f {} \;

#Start displaying
cat << EOT1
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional //EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<HEAD>
<title>Email Account Request Status | Department of Physics | University of Pune</title>
<link href="/style/dept.css" rel="stylesheet" type="text/css">
<link rel="icon"          type="image/x-icon" href="/images/favico11.ico">
<link rel="shortcut icon" type="image/x-icon" href="/images/favico11.ico">
</HEAD>
<BODY>
EOT1
cat ${webdir}/intranet/top.fixed.html
echo '<H1>Email account request status</H1>'

table_header() {
cat << EOT2
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5>
<TR>
	<TD BGCOLOR="#C0C0C0"><B>Rcpt.</B></TD>
	<TD BGCOLOR="#C0C0C0"><B>Name</B></TD>
	<TD BGCOLOR="#C0C0C0"><B>Login</B></TD>
	<TD BGCOLOR="#C0C0C0"><B>Status</B></TD>
	<TD BGCOLOR="#C0C0C0"><B>Proc.</B></TD>
	<TD BGCOLOR="#C0C0C0"><B>Comment</B></TD>
</TR>
EOT2
table_header_done="true"
}

notes=""
notescount=0
table_header_done=""
for file in `ls -v *.acc 2>/dev/null` ; do
	[ ! "$table_header_done" ] && table_header
	echo "<TR>"
	S_COMMENT='';
	N_STATUS='P'; # 'P' is not present in the file.
	A_DATE=''
	source $file
	echo "<TD>${N_DATE:--}</TD>"
	echo "<TD>${N_COMMENT}</TD>"
	echo "<TD><CODE>${N_LOGIN}</CODE></TD>"
	case "$N_STATUS" in
	A) echo "<TD BGCOLOR=\"#CCFFCC\">Created" ;;
	P) echo "<TD BGCOLOR=\"#FFFFCC\">Pending" ;;
	R) echo "<TD BGCOLOR=\"#FFCCCC\">Rejected" ;;
	esac
	#if [ "k$S_COMMENT" != "k" ] ; then
		#notescount=`expr $notescount + 1`
		#echo "<FONT SIZE="-2">(${notescount})</FONT>"
		#echo "<LI> ${S_COMMENT}" >> ${tmpnotes}
	#fi
	echo "</TD>"
	echo "<TD>${A_DATE:--}</TD>"
	echo "<TD>${S_COMMENT:--}</TD>"
	echo "</TR>"
done

if [ "$table_header_done" ] ; then
	echo "</TABLE>"
	if [ $notescount -ne 0 ] ; then
		echo "<FONT SIZE=\"-1\">"
		echo "<P>"
		echo "<OL>"; cat ${tmpnotes} ; echo "</OL></P>"
		echo "</FONT>"
		rm -f ${tmpnotes}
		echo "<HR WIDTH="150" ALIGN=left>"
	fi
else
	echo "<HR WIDTH="150" ALIGN=left>"
	echo "No requests in recent past."
	echo "<HR WIDTH=150 ALIGN=left>"
fi
date=`date '+%B %e, %Y'`
time=`date '+%I:%M%p'`
echo "<P>You are reading the status as on ${date} at ${time}. If this is"
echo "not today and now, please reload the page.</P>"
echo "<P>If your request status is pending, then please submit the"
echo "necessary documents to sysadmin within 7-10 days.<P>"
echo "<P>Otherwise your request will be automatically deleted.<P>"

cat ${webdir}/intranet/bottom.fixed.html
cat << EOT3

    <a href="http://validator.w3.org/check?uri=referer"><img
        src="/images/valid-html401.png"
        alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>


</BODY></HTML>

EOT3
