/*
 *	aegis - project change supervisor
 *	Copyright (C) 1997-2003 Peter Miller;
 *	All rights reserved.
 *
 *	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 2 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, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: instructions to cook(1), how to build archive directory
 */

integration-build-targets =
	/* archive/Part.01.gz */
	/* archive/[project_minus].patch.gz */
	archive/change_log.txt
	archive/[project_minus].README
	archive/[project_minus].ae
	archive/[project_minus].faq
	archive/[project_minus].lsm
	archive/[project_minus].spec
	html-targets
	lib/en/release/release.txt
	;
rpm-arch = noarch;
if [find_command tardy] then
{
    integration-build-targets +=
	archive/[project_minus].tar.gz
	;

    if [find_command rpm] then
    if [find_command ppmtoxpm] then /* have to be able to build icon */
    {
	rpm-arch = [collect rpm --showrc | awk "'/^build arch/{print $4}'" ];
	if [matches %1.D%2 [version]] then
	if [in [fromto %1.D%2 %2 [version]] 001 002] then
	if [matches linux%1 [arch]] then
	    integration-build-targets += RPM;
    }
}

if [find_command png2ico] then
{
    integration-build-targets += archive/favicon.ico;

    archive/favicon.ico: archive/[project_short].png
    {
	pngtopnm [resolve archive/[project_short].png] | pnmgamma 2.0
	    | pnmscale -xysize 16 16 > temp1.pnm;
	pnmquant 16 temp1.pnm | pnmtopng > temp16.png;
	pngtopnm [resolve archive/[project_short].png] | pnmgamma 2.0
	    | pnmscale -xysize 32 32 > temp1.pnm;
	pnmquant 16 temp1.pnm | pnmtopng > temp32.png;
	png2ico [target] temp16.png temp32.png;
	rm temp1.pnm temp16.png temp32.png;
    }
}

integration-build-targets: [integration-build-targets];

html-targets: [html-targets];

html-sources = [match_mask etc/%.html [source_files]];

html-targets =
	[fromto etc/%.html archive/%.html
		[html-sources]
	]
	archive/index.html
	;
if [find_command uudecode] then
	html-targets += archive/[project_short].png;

if [count [html-sources]] then
cascade [html-sources] = etc/version.so;

etc/%.html.d: etc/%.html
	set no-cascade
{
	c_incl -nc -ns -eia -r -nrec
		[prepost "-I" /[arch]/etc [search_list]]
		[prepost "-I" "/etc" [search_list]]
		-prefix "'cascade etc/%.html ='"
		-suffix "';'"
		[resolve etc/%.html]
		[prepost "-rlp=" "" [search_list]] /* remove leading path */
		-slp [arch] "'[arch]'" /* substitute leading path */
		-o [target];
}
#include-cooked-nowarn [addsuffix ".d" [match_mask etc/%.html [source_files]]]

archive/%.html: etc/%.html: etc/ssp.awk
	set shallow
{
	roffpp
		[prepost "-I" /[arch]/etc [search_list]]
		[prepost "-I" "/etc" [search_list]]
		[resolve etc/%.html]
	| groff -Tascii -t -P-hubc -man
	| [awk] -f [resolve etc/ssp.awk]
	> [target];
}

archive/%.txt: etc/%.man
{
    roffpp
	[prepost "-I" /[arch]/etc [search_list]]
	[prepost "-I" "/etc" [search_list]]
	[prepost "-I" "/lib/en/readme" [search_list]]
	[resolve etc/%.man]
    | groff -Tascii -t -P-bchu -man
    | [awk] -f [resolve etc/ssp.awk]
    > [target];
}

archive/[project_minus].ug.ps.gz: lib/en/user-guide/user-guide.ps.gz
	set shallow
{
	cp [resolve [need]] [target];
}

archive/[project_minus].rm.ps.gz: lib/en/refman/refman.ps.gz
	set shallow
{
	cp [resolve [need]] [target];
}

archive/[project_minus].faq: lib/en/faq/faq.txt
	set shallow
{
	cp [resolve [need]] [target];
}

archive/[project_minus].lsm: lib/en/lsm/lsm.txt
	set shallow
{
	cp [resolve [need]] [target];
}

archive/[project_minus].README: lib/en/readme/readme.txt
	set shallow
{
	cp [resolve [need]] [target];
}


source_file_order =
	README
	[project_short].spec /* so RPM can build without separate spec file */
	[project_short].png
	[project_short].xpm
	[sort
		[stringset
			[source_files]
			install-sh
			Makefile.in
			BUILDING
			LICENSE
			MANIFEST
			README
			etc/version.so
			common/config.h.in
			common/patchlevel.h
			configure
			/* etc/CHANGES */
			[fromto
				lib/%1/faq/main.%2 lib/%1/faq/toc.so
				[match_mask lib/%1/faq/main.%2 [source_files]]
			]
			[fromto
				lib/%/refman/main.%2 lib/%/refman/index.so
				[match_mask lib/%/refman/main.%2 [source_files]]
			]
			[fromto
				lib/%/refman/main.%2 lib/%/refman/parts.so
				[match_mask lib/%/refman/main.%2 [source_files]]
			]
			[fromto
				lib/icon/%.uue lib/icon/%
				[match_mask lib/icon/%.uue [source_files]]
			]
		-
			README
		]
	]
	;

if [find_command msgcat] then
    source_file_order +=
	/*
	 * This next file is for the benefit of the Translation Project
	 * robot.  (Yes, that's right, a bloody robot.)
	 *
	 * See http://www.iro.umontreal.ca/contrib/po/HTML/maintainers.html
	 * for more information.
	 *
	 * I particularly dislike this approach, because it is incredibly
	 * short sighted.  This approach fails to take into account that
	 * you may want to have translations of manual pages and other
	 * documentation, AS WELL AS translations of error messages.
	 *
	 * Would it make sense to ask authors to catenate all of
	 * ther man pages together, and have a po/PACKAGE.man file?
	 * No, of course not.  Would it make sense to ask authors to
	 * catenate all of their other documentation together, and
	 * have a po/PACKAGE.latex file?  No, of course not.  Well then,
	 * why on Earth does it make sense to catenate together all of
	 * the messages into a po/PACKAGE.pot file?  Betch.
	 *
	 * And then, to add insult to injury, it wastes space in the tarball.
	 *
	 * My project uses a directory tree
	 * 	lib/
	 *	    <LL>/
	 *		LC_MESSAGES/
	 *		    all of the various .po files go here
	 *		man<N>/
	 *		    all of the man<N> pages go here
	 *		user-guide/
	 *		    all of the files which make the
	 *		    user guide go here.
	 *		<other-docs>/
	 *		    and so on...
	 *
	 * As you can see, this can accomodate translated .po files,
	 * AND ALSO any other documentation, once it is translated.
	 */
	po/[project_short].pot
	;

MANIFEST: etc/MANIFEST.sh [stringset [source_file_order] - MANIFEST]
{
	sh [resolve etc/MANIFEST.sh [source_file_order]] > [target];
}

archive/Part.01.gz: [source_file_order]
	set shallow
{
	parts = [glob "archive/Part*"];
	if [parts] then
		rm [parts]
			set clearstat;
	/* assumes shar.3.49 available */
	shar -n[project_short] -a -c -oarchive/Part -L200
		-s "'Peter Miller <millerp@canb.auug.org.au>'"
		[resolve [source_file_order]];
	gzip -9 -v archive/Part*;
}

archive/[project_minus].tar.gz: [source_file_order]
	set shallow
{
	if [exists archive/[project_minus].tar] then
		rm archive/[project_minus].tar
			set clearstat;
	tar --create --file - --dereference [resolve [source_file_order]]
		| tardy -unu 0 -gnu 0 -una Peter -gna Miller
			-p [project_minus] -ms 0644 -mc 07022 -now
			[prepost "-rp=" "" [search_list]]
		> archive/[project_minus].tar;
	gzip -9 -v archive/[project_minus].tar;
}

patch_files = [fromto %0% %0%,P [source_files] etc/CHANGES.[version_short]];
patch_files = [vs_file],P [stringset [patch_files] - [vs_file],P];

archive/[project_minus].patch.gz: [patch_files]
	set shallow
{
	if [exists archive/[project_short].patch] then
		rm archive/[project_short].patch
			set clearstat;
	sh [resolve etc/patches.sh] [resolve [patch_files]]
		> archive/[project_short].patch;
	gzip -9 -v archive/[project_short].patch;
}

[project_short].spec: etc/spec.sh [source_files]
{
	version\=[version_short]
	sh [resolve etc/spec.sh] [source_files] > [target];
}

archive/[project_minus].spec: [project_short].spec
	set shallow
{
	cp [resolve [need]] [target];
}

[vs_file],P: [vs_file]
{
	echo "Index:" [vs_file] > [target];
	prev =
		[collect
			aegis -list version -p [project] -c [change]
			| [awk] -F"'\"'" "'/previous/{print $2}'"
		];
	echo "'Prereq: \""[prev]"\"'" >> [target];
	echo "'#define PATCHLEVEL \""[prev]"\"'" |
	diff -c - [vs_file] | sed '1,2d' >> [target];
}

%0%,P: %0%
	if [exists [resolve %0%]]
{
	aegis -cp %0% -delta 1 -output patch.orig -p [project] -c [change]
		|| cp /dev/null patch.orig;
	sh [resolve etc/patch.fil.sh] patch.orig [resolve %0%] %0% > [target];
	rm patch.orig;
}

lib/%0%: lib/%0%.uue
{
	uudecode -o [target] [resolve [need]];
}

archive/[project_short].png: lib/icon2/64x64.png
	set shallow
{
	cp [resolve [need]] [target];
}

[project_short].png: archive/[project_short].png
{
	cp [resolve [need]] [target];
}

%0%.xpm: %0%.png
{
	pngtopnm [resolve %0%.png] | ppmquant 91 | ppmtoxpm > [target];
}

/*
 * This is not in the integration-build-targets because it takes far
 * too long to do.
 */

RPM: archive/[project_minus]-1.[rpm-arch].rpm;

archive/[project_minus]-1.[rpm-arch].rpm archive/[project_minus]-1.src.rpm:
		archive/[project_minus].tar.gz
		archive/[project_short].xpm
		etc/rpm-build.sh
	set shallow
{
	/*
	 * The book says that the icon files need to be in the SOURCES
	 * directory.  Actually, they need to be in the same directory
	 * as the tarball.
	 *
	 * The icon must be GIF or XPM.
	 */
	sh [resolve etc/rpm-build.sh]
		RPM-temporary
		[resolve archive/[project_minus].tar.gz]
		[resolve archive/[project_short].xpm]
		;
	mv RPM-temporary/RPMS/[rpm-arch]/[project_minus]-1.[rpm-arch].rpm
		archive/[project_minus]-1.[rpm-arch].rpm
		;
	mv RPM-temporary/SRPMS/[project_minus]-1.src.rpm
		archive/[project_minus]-1.src.rpm
		;
	rm -r RPM-temporary;
}

LICENSE: lib/en/cat1/aelic.1
{
	cat [resolve [need]] > [target];
}

archive/[project_minus].ae: [source_files]
{
	if [find_command aedist] then
		/* must be *system* aedist, not just built aedist,
		 * since it uses the existing project, which may
		 * not even be readable by the newly built aedist
		 */
		aedist --send --entire-source -ndh -naa
			-p\=[project] -c\=[change]
			-o [target];
	else
		[print Skipping [target], since no aedist(1) found];

}


example-cook-fhist-version = 1.0;
example-cook-fhist-baseline =
     [collect aegis -cd -bl -p example-cook-fhist.[example-cook-fhist-version]
	'2>/dev/null' || true];

if [example-cook-fhist-baseline] then
{
    html-targets +=
	archive/example-cook-fhist.html
	archive/example-cook-fhist.ae
	;

    archive/example-cook-fhist.html: [example-cook-fhist-baseline]/README.html
	set shallow
    {
	cp [need] [target];
    }

    archive/example-cook-fhist.ae:
	[example-cook-fhist-baseline
			]/example-cook-fhist.[example-cook-fhist-version].ae
	set shallow
    {
	cp [need] [target];
    }
}


example-make-rcs-version = 1.0;
example-make-rcs-baseline =
	[collect aegis -cd -bl -p example-make-rcs.[example-make-rcs-version]
		'2>/dev/null' || true];

if [example-make-rcs-baseline] then
{
    html-targets +=
	archive/example-make-rcs.html
	archive/example-make-rcs.ae
	;

    archive/example-make-rcs.html: [example-make-rcs-baseline]/README.html
	set shallow
    {
	cp [need] [target];
    }

    archive/example-make-rcs.ae:
	[example-make-rcs-baseline
			]/example-make-rcs.[example-make-rcs-version].ae
	set shallow
    {
	cp [need] [target];
    }
}

archive/index.html: archive/[project_short].html
    set shallow
{
    cp [resolve [need]] [target];
}

archive/change_log.txt: lib/en/change_log/change_log.txt
    set shallow
{
    cp [resolve [need]] [target];
}
