/*
 *	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 script_name;
script_name = getenv("SCRIPT_NAME");
if (script_name == "")
	script_name = "http://localhost/cgi-bin/aegis.cgi";
auto pn;
pn = project_name();

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 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\">");
}
print("<title>");

print("Project " ## quote_html(pn) ## ", File Conflicts");
print("</title></head><body><h1 align=center>");

auto href;
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);

if (change_number_set())
{
    href = script_name ## "?file@chan_menu+project@" ## quote_url(pn)
	## "+change@" ## change_number();
    print("Change <a href=\"" ## href ## "\">" ## change_number() ##
	"</a>,<br>");
}
print("File Conflicts");
print("</h1>");
print("<div class=\"information\">");

/*
 * get the project state
 */
auto ps, ps_nobr;
ps_nobr = project[project_name()].state;
ps = ps_nobr.branch;

/*
 * default to all project files
 * if no files named on the command line
 */
auto file;
if (count(arg) == 0)
{
    if (change_number_set())
    {
       	for (file in ps.change[change_number()].src)
	    arg ##= file.file_name;
    }
    else
    {
       	for (file in ps_nobr.src)
	    arg ##= file.file_name;
    }
}

/*
 * check the files in every active change
 */
auto cn, cs, ch;
auto active;
active = {};
for (cn in  keys(ps.change))
{
    cs = ps.change[cn];
    if (cs.state < being_developed || cs.state >= completed)
       	continue;
    for (file in cs.src)
    {
       	if (!(file.file_name in arg))
	    continue;
       	if (file.usage == build && file.action == modify)
	    continue;
       	if (!(file.file_name in keys(active)))
	    active[file.file_name] = [];
       	auto developer, integrator;
       	developer = "";
       	integrator = "";
       	for (ch in cs.history)
       	{
	    if (ch.what == develop_begin)
	     	developer = ch.who;
	    else if (ch.what == integrate_begin)
	     	integrator = ch.who;
	    else if (ch.what == integrate_begin_undo)
	     	integrator = "";
	    else if (ch.what == integrate_fail)
	     	integrator = "";
       	}
       	active[file.file_name] ##=
	    {
	     	usage = file.usage;
	     	action = file.action;
	     	edit = file.edit_origin.revision;
	     	who =
		    (
			cs.state == being_developed
		    ?
			developer
		    :
			(cs.state == being_integrated ? integrator : "")
		    );
	     	change = cn;
	     	state = cs.state;
	     	description = cs.brief_description;
	    };
    }
}

/*
 * list all of the active files
 */
auto fn, num_active;
num_active = 0;
print("<table align=center>");
for (fn in sort(keys(active)))
{
    if (count(active[fn]) < 2)
       	continue;

    print("<tr><td colspan=8><br><p>");
    auto email, long_name;
    href = script_name ## "?file@file_menu+" ## quote_url(fn);
    href ##= "+project@" ## quote_url(project_name());
    print("<a href=\"" ## href ## "\">");
    print("<span class=\"filename\">" ## quote_html(fn) ## "</span></a>");
    print("</td></tr>");

    print("<tr><th></th><th>Action</th><th>Type</th><th>Edit</th><th>Who</th>");
    print("<th>Change</th><th>State</th><th>Description</th></tr>");

    ++num_active;
    auto actions, usages;
    actions = "BS";
    usages = "BS";
    for (file in active[fn])
    {
       	print("<tr><td>&nbsp;&nbsp;</td><td valign=top>");
       	if (file.usage != usages)
	    print(file.usage);
       	print("</td><td valign=top>");
       	if (file.action != actions)
	    print(file.action);
       	print("</td><td valign=top align=right>");
       	print(file.edit);
       	print("</td><td valign=top>");
       	print(file.who);
       	print("</td><td valign=top align=right>");
       	href = script_name ## "?file@chan_menu";
	href ##= "+project@" ## quote_url(project_name());
       	href ##= "+change@" ## file.change;
       	print("<a href=\"" ## href ## "\">");
       	print(file.change ## "</a>");
       	print("</td><td valign=top>");
       	print(file.state);
       	if (file.who != "")
       	{
	    email = file.who;
	    try { email = user[file.who].email_address; }
	    catch (email) { email = ""; }
	    if (email == "")
	     	email = file.who;
	    print("<br><a href=\"mailto:" ## email ## "\"><i>");
	    long_name = file.who;
	    try { long_name = passwd[file.who].pw_comment; }
	    catch (long_name) { long_name = file.who; }
	    print(quote_html(long_name) ## "</i></a>");
       	}
       	print("</td><td valign=top>");
       	auto part;
       	for (part in wrap_html(quote_html(file.description), 80))
	    print(part);
       	print("</td></tr>");
       	actions = file.action;
       	usages = file.usage;
    }
}
print("<tr><td colspan=8><br><p>");
if (num_active == 1)
    print("Listed one conflicted file.");
else if (num_active)
    print("Listed " ## num_active ## " conflicted files.");
else
    print("There are no conflicted files in this project.");
print("</td></tr>");
print("</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>aer file_conflicts -p " ## quote_html(project_name())
	## "</samp></blockquote>");
print("</div>");

print("<hr>");
print("<p align=center class=\"navbar\">[");
print("<a href=\"" ## script_name ## "\">Project List</a> |");
href = script_name ## "?file@proj_menu+project@" ## quote_url(pn);
print("<a href=\"" ## href ## "\">Project Menu</a> |");
href = script_name ## "?file@changes+project@" ## quote_url(pn);
print("<a href=\"" ## href ## "\">Change List</a>");
if (change_number_set())
{
    href = script_name ## "?file@chan_menu+project@" ## quote_url(pn)
	## "+change@" ## change_number();
    print("| <a href=\"" ## href ## "\">Change Menu</a>");
    href = script_name ## "?file@file_list+project@" ## quote_url(pn)
        ## "+change@" ## cn ;
    print("| <a href=\"" ## href ## "\">File List</a>");
}
print("]</p>");

print("<hr>");
print("This page was generated " ## now() ## ".");
print("</body></html>");
