/*
 *	aegis - project change supervisor
 *	Copyright (C) 1997, 1998, 2000-2003, 2005-2008 Peter Miller
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program. If not, see
 *	<http://www.gnu.org/licenses/>.
 */
auto href;

auto script_name;
script_name = getenv("SCRIPT_NAME");
if (script_name == "")
    script_name = "http://localhost/cgi-bin/aegis.cgi";

columns({width = 1000; });
print("Content-Type: text/html");
print("");
print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\""
        ## "\"http://www.w3.org/TR/REC-html40/loose.dtd\">");
print("<html><head>");
print("<meta name=\"ROBOTS\" content=\"NOINDEX, NOFOLLOW\">");
print("<meta name=\"GENERATOR\" content=\"" ## script_name ## "\">");
print("<meta http-equiv=\"Content-Type\" "
        ## "content=\"text/html; charset=ISO-8859-1\">");
print("<style type=\"text/css\"> "
        ## "tr.even-group { background-color: #CCCCCC; }"
        ## "body { background-color: white; }"
        ## "</style>");
/*
 * Netscape 4.x has numerous CSS bugs, two of which need mentioning.
 * 1. If a style sheet is not present Netscape says 404 Not found, when
 * it should silently ignore it.  2. Style sheets who's media is not
 * "screen" will be ignored.  Fortunately we can use (2) to get around (1).
 */
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/aedefault.css\" "
    ## "media=\"all\">");
auto pn;
pn = project_name();
auto stack, depth, n;
depth=0;
stack[depth] = project[pn].state.name;
while ("" != ( n = project[stack[depth]].state.parent_name )) {
  stack[++depth] = n;
}
while ( depth >= 0) {
    print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/" ##
      stack[depth--] ## ".css\" " ## "media=\"all\">");
}
/* get change type filter */
auto c_type, c_not, a;
c_type = "undefined";
c_not = false;
for (a in arg)
{
    if (a ~~ "state:")
        c_type = substr(a, 6, 9999);
}
if (substr(c_type, 0, 1) == "-")
{
    c_not = true;
    c_type = substr(c_type, 1, 9999);
}
auto c_desc,  sample_cmdln;
c_desc = capitalize(subst("_", " ", c_type));
sample_cmdln = "aegis -l chan_" ## c_type;
if (c_type == "undefined")
{
    c_not = true;
    c_desc = "";
    sample_cmdln = "ael c -p " ## quote_html(pn);
}
if (c_type == "being_developed")
{
    sample_cmdln = "aede -l -p " ## quote_html(pn);
}
else if (c_type == "awaiting_review")
{
    sample_cmdln = "aerb -l -p " ## quote_html(pn);
}
else if (c_type == "being_reviewed")
{
    sample_cmdln = "aerfail -l -p " ## quote_html(pn);
}
else if (c_type == "awaiting_integration")
{
    sample_cmdln = "aeib -l -p " ## quote_html(pn);
}
else if (c_type == "being_integrated")
{
    sample_cmdln = "aeifail -l -p " ## quote_html(pn);
}
else if (c_type == "completed")
{
    if (c_not)
	sample_cmdln = "ael oc " ## quote_html(pn);
    else
	sample_cmdln = "ael phi " ## quote_html(pn);
}

print("<title>");
print("Project " ## quote_html(pn) ## " Changes");
if (c_desc != "")
	print((c_not ? "not " : "") ## c_desc);
print("</title></head><body><h1 align=center>");

print("<a href=\"" ## script_name ## "\">Project</a>");
auto long_project_name;
auto prj_name_parts;
prj_name_parts = split(pn, '.');
href = script_name ## "?file@proj_menu+project@"
	## quote_url(prj_name_parts[0]);
long_project_name = "<a href=\"" ## href ## "\" >"
	## quote_html(prj_name_parts[0]) ## "</a>";
auto j;
for (j = 1; j < count(prj_name_parts); j++)
{
    href ##= '.' ## prj_name_parts[j];
    long_project_name ##= ".<a href=\"" ## href ## "\" >"
    	## quote_html(prj_name_parts[j]) ## "</a>";
};
long_project_name = "&ldquo;" ## long_project_name ## "&rdquo;,<br>";
/* HTTP limits lines to 510 characters */
for (j in wrap(long_project_name, 510))
    print(j);

print("List of Changes");
if (c_desc != "")
{
    print("<br>");
    print((c_not ? "not " : "") ## c_desc);
}
print("</h1>");
print("<div class=\"information\">");
print("<table align=\"center\">");
print("<tr class=\"even-group\">");
print("<th>Change</th><th>State</th><th>Description</th><th>Download</th>");
print("</tr>");

auto ps;
ps = project[pn].state.branch;
auto cn, rownum; rownum = 0;
for (cn in sort(keys(ps.change)))
{
    auto cs;
    cs = ps.change[cn];
    if (c_not)
    {
	if (cs.state == c_type)
	    continue;
    }
    else
    {
	if (cs.state != c_type)
	    continue;
    }
    if (rownum % 6 < 3)
        print("<tr class=\"odd-group\">");
    else
        print("<tr class=\"even-group\">");
    rownum++;
    print("<td valign=\"top\" align=\"right\">");
    auto junk;
    href = "file@chan_menu+project@" ## quote_url(pn) ## "+change@" ## cn;
    try
    {
       	if (count(keys(cs.branch)))
	    href = "file@proj_menu+project@" ## quote_url(pn) ## "." ## cn;
    }
    catch (junk)
       	;
    href = script_name ## "?" ## href;
    print("<a href=\"" ## href ## "\">" ## cn ## "</a>");
    print("</td><td valign=\"top\">");
    print(cs.state);
    if
    (
	(cs.state == being_developed || cs.state == being_integrated)
    &&
	!count(keys(cs.branch))
    )
    {
	auto who, hi, h, email, long_name;
	for (hi = count(cs.history) - 1; hi >= 0; --hi)
	{
	    h = cs.history[hi];
	    if
	    (
		h.what == develop_begin
	    ||
		h.what == develop_end
	    ||
		h.what == integrate_begin
	    ||
		h.what == integrate_pass
	    )
	    {
		who = h.who;
		email = h.who;
		try { email = user[h.who].email_address; }
		catch (email) { email = ""; }
		if (email == "")
		    email = h.who;
		href = "mailto:" ## email;
		long_name = h.who;
		try { long_name = passwd[who].pw_comment; }
		catch (long_name) { long_name = h.who; }
		print("<br><a href=\"" ## href ## "\"><i>" ##
		    quote_html(long_name) ## "</i></a>");
		break;
	    }
	}
    }
    print("</td><td valign=\"top\">");
    auto part;
    for (part in wrap_html(quote_html(cs.brief_description), 80))
       	print(part);
    print("</td>");
    print("<td valign=top>");
    if (cs.state != awaiting_development)
    {
	href = script_name ## "/" ## quote_url(pn) ## ".C" ## cn
	    ## ".patch?file@aepatch+project@" ## quote_url(pn)
	    ## "+change@" ## cn;
	print("<a href=\"" ## href ## "\"><code>patch</code></a>,");

	href = script_name ## "/" ## quote_url(pn) ## ".C" ## cn
	    ## ".tar.gz?file@aetar+project@" ## quote_url(pn)
	    ## "+change@" ## cn;
	print("<a href=\"" ## href ## "\"><code>tar.gz</code></a>,");

	href = script_name ## "/" ## quote_url(pn) ## ".C" ## cn
	    ## ".ae?file@aedist+project@" ## quote_url(pn)
	    ## "+change@" ## cn;
	print("<a href=\"" ## href ## "\"><code>aedist</code></a>");
    }
    print("</td>");
    print("</tr>");
}
print("<tr><td colspan=\"2\">");
print("Listed " ## rownum ## " changes.");
print("</td></tr></table>");
print("</div>");
print("<hr>");
print("<div class=\"report-cmd\">");
print("A similar report may be obtained from the command line, with");
print("<blockquote><samp>");
print(sample_cmdln);
print("</samp></blockquote>");
print("</div>");

print("<hr>");
print("<p align=\"center\" class=\"navbar\">[");
print("<a href=\"" ## script_name ## "\">Project List</a>");
print("|");
href = script_name ## "?file@proj_menu+project@" ## quote_url(pn);
print("<a href=\"" ## href ## "\">Project Menu</a>");
print("]</p>");
print("<hr>");
print("This page was generated " ## now() ## ".");
print("</body></html>");
