/*
 *	aegis - project change supervisor
 *	Copyright (C) 1994, 1997, 2001, 2006-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/>.
 */

if (change_number_set())
{
	title
	(
		sprintf
		(
			"Project \"%s\", Change %d",
			project_name(),
			change_number()
		),
		"File Activity Report"
	);
}
else
{
	title
	(
		"The File Activity Report",
		sprintf("Project \"%s\"", project_name())
	);
}

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

/*
 * default to all project files
 * if not 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.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
 */
columns
(
	{ name = "File Name\n-----------"; right = 0; },
	{ name = ""; left = 2; width = 8; }, /* action */
	{ name = ""; width = 7; }, /* usage */
	{ name = "Edit\n------"; width = 6; },
	{ name = "Who\n-------"; width = 8; },
	{ name = "Change\n------"; width = 6; },
	{ name = "State\n-------"; width = 10; },
	{ name = "Description\n-------------"; right = 0; }
);
auto fn, num_active;
num_active = 0;
for (fn in sort(keys(active)))
{
	if (count(active[fn]) < 1)
		continue;
	++num_active;
	need(4);
	print(fn);
	auto actions, usages;
	actions = "BS";
	usages = "BS";
	for (file in active[fn])
	{
		print
		(
			"",
			(file.usage != usages ? file.usage : ""),
			(file.action != actions ? file.action : ""),
			sprintf("%4s", file.edit),
			file.who,
			sprintf("%4d", file.change),
			file.state,
			file.description
		);
		actions = file.action;
		usages = file.usage;
	}
}
if (num_active == 0)
	print("There are no active files in this project.");
