function make_var =
{
	return [prepost "\\$\\(" "\\)" [arg]];
}

function make_line =
{
	echo [arg] >>[target] set silent;
}

function make_action =
{
	[make_line -e "\"\\t\""[arg]];
}



%0%.c.Md: %0%.c etc/makefile.cook 
{
	c_incl -nc -ns -eia
		[prepost -I /include [search_list]]
		[resolve %0%.c]
		[addprefix -rlp\= [search_list]]
		-nsri
		-o [target];
}

%0%.c.M: %0%.c.Md etc/makefile.cook etc/functions.cook etc/sourcelist.cook
{
	rm -f [target];

	[make_line "%0%.o: %0%.c" [collect cat [resolve %0%.c.Md]]];
	[make_action [make_var CC] -c [make_var [get_program %0]cflags] %0%.c -o %0%.o];
	[make_line];
}

destbindir = [make_var DESTDIR][make_var bindir]/;
destman1dir = [make_var DESTDIR][make_var MANDIR]/man1;
configdir = [make_var DESTDIR][make_var DMDIR];
gfxdir = [make_var DESTDIR][make_var GFXDIR];
pamdir = [make_var DESTDIR][make_var PAMDIR];
localedir = [make_var DESTDIR][make_var NLSDIR];

src/%.a.M: etc/makefile.cook etc/functions.cook etc/sourcelist.cook [select_c %]
{
	rm -f [target];

	[make_line "bin/%.a: " [c_to_o [select_c %]]];
	[make_action mkdir -p bin];
	[make_action [make_var ar] cru bin/%.a [c_to_o [select_c %]]];
	[make_action [make_var ranlib] bin/%.a];
	[make_line];
}

src/%.M: etc/makefile.cook etc/functions.cook etc/sourcelist.cook [select_c %]
{
	rm -f [target];
	
	[make_line "bin/%: " [c_to_o [select_c %]] [lib_deps [get_libs %_]]];
	[make_action mkdir -p bin]
	[make_action [make_var CC] [make_var %_ldflags] [c_to_o [select_c %]] [make_var %_libs] -o bin/%];
	[make_line];
	[make_line [destbindir]%\: bin/%];
	[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
	[make_action [make_var INSTALL_PROGRAM] bin/% \\$\\@];
	[make_line];
}

doc/%.man.M: etc/makefile.cook
{
	rm -f [target];

	if [in doc/%.man [g_manfiles]] then
	{
		[make_line doc/%.man\: doc/%.man.in];
		[make_action CONFIG_FILES\=\\$\\@\:doc/%.man.in \
			CONFIG_HEADERS\= ./config.status];
		[make_line];
	}
	[make_line [destman1dir]/%.1\: doc/%.man];
	[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
	[make_action [make_var INSTALL_DATA] doc/%.man \\$\\@];
	[make_line];
}

po/%.mo.M: etc/makefile.cook
{
	rm -f [target];

	[make_line po/%.mo\: po/%.po];
	[make_action [make_var msgfmt] -o \\$\\@ po/%.po];
	[make_line];
	[make_line [localedir]/%/LC_MESSAGES/wdm.mo\: po/%.mo];
	[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
	[make_action [make_var INSTALL_DATA] po/%.mo \\$\\@];
	[make_line];
}

configs/%.M: etc/makefile.cook etc/functions.cook
{
	rm -f [target];

	if [in configs/% [g_configs]] then
	{
		[make_line configs/%\: configs/%.in];
		[make_action CONFIG_FILES\=\\$\\@\:configs/%.in \
			CONFIG_HEADERS\= ./config.status];
		[make_line];
	}

	perm = 0644;
	if [in configs/% [exe_configs]] then perm = [octal [expr [perm] | 0111]];
	if [in configs/% [sec_configs]] then perm = [octal [expr [perm] & 0700]];

	if [in configs/% [pam_configs]] then
	{
		[make_line [pamdir]/[stripext %]\: configs/%];
		[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
		[make_action [make_var INSTALL] -m [perm] configs/% \\$\\@];
		[make_line];
	}
	else
	{
		[make_line [configdir]/%\: configs/%];
		[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
		[make_action [make_var INSTALL] -m [perm] configs/% \\$\\@];
		[make_line];
	}
	echo >> [target];
}

function is_xpm =
{
	return [matches %.xpm [@1]];
}

pixmaps/%.M: etc/makefile.cook
{
	rm -f [target];

	[make_line [gfxdir]/%\: pixmaps/%];
	[make_action [make_var INSTALL] -d \\$\\(\\@D\\)];
	[make_action [make_var INSTALL_DATA] pixmaps/% \\$\\@];
	[make_line];
}

m_files = 
	[fromto %0% %0%.M [match_mask src/%0%.c [source_files]]]
	[fromto % src/%.M [program_list]]
	[fromto % src/%.a.M [libraries_list]]
	[fromto %0% %0%.M [manfiles]]
	[fromto %0% %0%.M [gfxfiles]]
	[fromto %0% %0%.M [mo_files]]
	[fromto %0% %0%.M [configs] [pam_configs] [i_configs]];

clean_files = [fromto %0%.c %0%.o [match_mask src/%0%.c [source_files]]]
		[g_configs] [mo_files] [g_manfiles]
		[addprefix bin/ [program_list]]
		[fromto % bin/%.a [libraries_list]];

dist_clean_files = [clean_files] [addprefix bin/ [program_list]]
	config.status config.log include/config.h include/gnuLogo.xpm Makefile;

Makefile.in : etc/Howto.conf.in etc/Makefile.head etc/makefile.cook
		etc/Howto.cook [m_files]
{
	rm -f [target];
	sed [resolve etc/Howto.conf.in] \
		-e "'s@\\[prepost.*-I.*\\[.*\\]\\]@-I./include@'" \
		-e "'s@\\[prepost.*-L.*\\[.*\\]\\]@-L./bin@'" \
		-e "'s@\\[\\([^\\[]*\\)\\]@\\$(\\1)@g'" \
		-e "'s@;$@@'" \
		-e "'s@/\\*\\*\\(.*\\)\\*/@\\1@'" \
		-e "'s@/\\*\\(.*\\)\\*/@# \\1@'" \
		>> [target];
	cat [resolve etc/Makefile.head] >> [target];

	[make_line];
	[make_line all\: all-bin all-doc all-config all-nls-[make_var I18N]];
	[make_line];
	[make_line install\: install-bin install-config install-doc install-gfx
		install-nls-[make_var I18N]];

	[make_line];
	[make_line all-bin\: [addprefix bin/ [program_list]]];
	[make_line];
	[make_line install-bin\: [addprefix [destbindir] [stringset [program_list] - [i_program_list]]]];
	[make_line];


	[make_line];
	[make_line all-config\: [g_configs]];
	[make_line];
	[make_line install-config\:
			[fromto %0% [configdir]/% [configs] [i_configs]]
			install-pam-[make_var USE_PAM]
			[configdir]/authdir];
	[make_action install -d -m 0700 [make_var DESTDIR][make_var FAKEHOME]];
	[make_line];
	[make_line install-pam-no\:];
	[make_line];
	[make_line install-pam-yes\: [fromto %0%.pamd [pamdir]/% [pam_configs]]];
	[make_line];
	[make_line [configdir]/authdir\:];
	[make_action [make_var INSTALL] -m 700 -d \\$\\@];
	

	[make_line];
	[make_line all-doc\: [g_manfiles]];


	[make_line];
	[make_line all-nls-yes\: [mo_files]];
	[make_line];
	[make_line all-nls-no\:];


	[make_line];
	[make_line install-doc\: [fromto %0%.man [destman1dir]/%.1 [manfiles]]];

	
	[make_line];
	[make_line install-gfx\: [fromto %0% [gfxdir]/% [gfxfiles]]];


	[make_line];
	[make_line install-nls-yes\: [fromto po/%.mo [localedir]/%/LC_MESSAGES/wdm.mo [mo_files]]];
	
	[make_line];
	[make_line install-nls-no\:];


	[make_line];
	cat [resolve [m_files]] >> [target];


	[make_line];
	[make_line clean\:];
	[make_action rm -f [clean_files]];


	[make_line];
	[make_line distclean\:];
	[make_action rm -f [dist_clean_files]];
	[make_action "'if test -e bin; then rmdir bin; fi'"];

	[make_line];
	[make_line po/wdm.pot\: [match_mask src/%0%.c [source_files]]];
	[make_action [make_var xgettext] --keyword\=_ --keyword\=N_
		--default-domain\=wdm --output\=\\$\\@ [match_mask src/%0%.c [source_files]]];
}

