-- © 2007 David Given.
-- WordGrinder is licensed under the BSD open source license. See the COPYING
-- file in this distribution for the full text.
--
-- $Id: pmfile 43 2008-01-13 06:35:34Z dtrg $
-- $URL: https://wordgrinder.svn.sourceforge.net/svnroot/wordgrinder/wordgrinder/pmfile $

include "c.pm"
HOME = os.getenv("HOME")
DATE = "13 January 2008"
VERSION = "0.2"
FILEFORMAT = 2

-----------------------------------------------------------------------------
-- User configurable settings start here!

-- Where do you want WordGrinder installed? By default, it goes into your
-- home directory.

PREFIX = HOME
-- PREFIX = "/usr/local"

-- What build flags do you want to use? (Not including -g or -Os, which are
-- added later.)

CBUILDFLAGS = {"-Wall"}

-- Any other build options go in these three sections. Note the lack of -l, -D
-- or -I. We need the XOPEN stuff to make the wide-character curses library
-- work. 

CLIBRARIES = {
	'ncursesw',
	'm',
	'lua5.1',
}

CDEFINES = {
	'_XOPEN_SOURCE_EXTENDED',
	'_XOPEN_SOURCE',
	'VERSION="%VERSION%"',
	'FILEFORMAT=%FILEFORMAT%',
	'PREFIX="%PREFIX%"'
}
	
CINCLUDES = {
	"/usr/include/ncursesw",
	"/usr/include/lua5.1",
}

-- There is nothing editable beyond this point.
-----------------------------------------------------------------------------

-- The staging area for the installation.

BINDIR = ".staging"

-- Build the man page.

wordgrinder_man = simple {
	command = {
		"sed -e 's/@@@DATE@@@/%DATE%/g; s/@@@VERSION@@@/%VERSION%/g' %in% > %out%"
	},
	outputs = {"%U%-%I%.man"},
	file "wordgrinder.man"
}

-- Build the main program.

wordgrinder_exe = cprogram {
	cfile "src/c/utils.c",
	cfile "src/c/main.c",
	cfile "src/c/lua.c",
	cfile "src/c/screen.c",
	cfile "src/c/word.c",
	cfile "src/c/bit.c",
}

wordgrinder_release_exe = group {
	wordgrinder_exe,
	wordgrinder_man,
	CBUILDFLAGS = {PARENT, '-Os'},
	CDEFINES = {PARENT, 'NDEBUG'},
	
	install = {
		pm.install("%{return self.out[1]}%",     "%BINDIR%/bin/wordgrinder"),
		pm.install("%{return self.out[2]}%",     "%BINDIR%/man/man1/wordgrinder.1"),
		pm.install("src/lua/browser.lua",        "%BINDIR%/share/wordgrinder/browser.lua"),
		pm.install("src/lua/document.lua",       "%BINDIR%/share/wordgrinder/document.lua"),
		pm.install("src/lua/export.lua",         "%BINDIR%/share/wordgrinder/export.lua"),
		pm.install("src/lua/fileio.lua",         "%BINDIR%/share/wordgrinder/fileio.lua"),
		pm.install("src/lua/forms.lua",          "%BINDIR%/share/wordgrinder/forms.lua"),
		pm.install("src/lua/html.lua",           "%BINDIR%/share/wordgrinder/html.lua"),
		pm.install("src/lua/events.lua",         "%BINDIR%/share/wordgrinder/events.lua"),
		pm.install("src/lua/margin.lua",         "%BINDIR%/share/wordgrinder/margin.lua"),
		pm.install("src/lua/import.lua",         "%BINDIR%/share/wordgrinder/import.lua"),
		pm.install("src/lua/main.lua",           "%BINDIR%/share/wordgrinder/main.lua"),
		pm.install("src/lua/menu.lua",           "%BINDIR%/share/wordgrinder/menu.lua"),
		pm.install("src/lua/navigate.lua",       "%BINDIR%/share/wordgrinder/navigate.lua"),
		pm.install("src/lua/redraw.lua",         "%BINDIR%/share/wordgrinder/redraw.lua"),
		pm.install("src/lua/ui.lua",             "%BINDIR%/share/wordgrinder/ui.lua"),
		pm.install("src/lua/utils.lua",          "%BINDIR%/share/wordgrinder/utils.lua"),
	}
}

wordgrinder_debug_exe = group {
	wordgrinder_exe,
	CBUILDFLAGS = {PARENT, '-g'},
	
	install = pm.install("wordgrinder-debug")
}

wordgrinder_both_exe = group {
	wordgrinder_release_exe,
	wordgrinder_debug_exe
}

default = wordgrinder_both_exe

-- Once built, do the installation, rather crudely. FIXME.

install = simple {
	outputs = {"dummy"},
	command = "",
	__dobuild = function(self, inputs, outputs)
		os.execute("mkdir -p "..PREFIX)
		os.execute("(cd "..BINDIR.." && tar chf - .) | (cd "..PREFIX.." && tar xvf -)")
	end,
	
	wordgrinder_both_exe
}
