# -*- Mode: sh

all: $(APPNAME) docs build-mo

PROJ=mp

ADD_DOCS=AUTHORS README COPYING RELEASE_NOTES \
	mp_templates.sample doc/mp_function_reference.html
GRUTATXT_DOCS=doc/mp_index.html doc/mp_cookbook.html doc/mp_internals.html \
	doc/mp_configuration.html doc/mp_data_model.html \
	doc/mp_man.html doc/mp_interactive_dialog_boxes.html \
	doc/mp_actions.html $(APPNAME).1
MP_DOCCER_DOCS=doc/mp_api.html

OBJS=mp_core.o mpv_curses.o mpv_gtk.o mpv_win32.o

DIST_TARGET=/tmp/$(PROJ)-$(VERSION)

##################################################################

version:
	@echo $(VERSION)

.c.o:
	$(CC) $(CFLAGS) -I. `cat config.cflags` -c $<

$(MPDM)/libmpdm.a:
	( cd $(MPDM); $(MAKE) )

$(MPSL)/libmpsl.a:
	( cd $(MPSL); $(MAKE) )

dep:
	gcc -MM *.c `cat config.cflags` > makefile.depend

$(APPNAME): $(OBJS) $(MPDM)/libmpdm.a $(MPSL)/libmpsl.a
	$(CC) $(CFLAGS) $(OBJS) `cat config.ldflags` -o $@

mp_res.o: mp_res.rc
	$(WINDRES) mp_res.rc mp_res.o

wmp.exe: $(OBJS) mp_res.o $(MPDM)/libmpdm.a $(MPSL)/libmpsl.a
	$(CC) $(CFLAGS) $(OBJS) mp_res.o `cat config.ldflags` -o $@

clean:
	rm -f $(APPNAME) $(LIB) $(OBJS) *.o tags *.tar.gz *.exe *.zip .installer.c *.1
	(cd $(MPDM) ; make clean)
	(cd $(MPSL) ; make clean)

distclean: clean
	rm -f config.h config.cflags config.ldflags \
		makefile.opts Makefile po/minimum-profit.pot

docsclean:
	rm -f doc/*.html

realclean: distclean
	(cd $(MPDM) ; make realclean)
	(cd $(MPSL) ; make realclean)

realdistclean: realclean

.SUFFIXES: .html .txt .rhtml

.txt.html:
	grutatxt < $< > $@

.rhtml.html:
	cp $< $@

doc/mp_man.html: doc/mp_man.txt
	grutatxt -dl -t "Minimum Profit Man Page" < $< > $@

doc/mp_api.html: *.mpsl
	mp_doccer *.mpsl -o doc/mp_api -f html1 \
		-t "Minimum Profit API ($(VERSION))" \
		-a 'Angel Ortega - angel@triptico.com'

$(APPNAME).1: doc/mp_man.txt
	grutatxt -m man -t "Minimum Profit" < doc/mp_man.txt > $(APPNAME).1

docs: $(DOCS)
	(cd $(MPDM); $(MAKE) docs)
	(cd $(MPSL); $(MAKE) docs)

distcopy: distclean docs
	mkdir -p $(DIST_TARGET) ; \
	tar cf - * | (cd $(DIST_TARGET) ; tar xf -) ; \
	(cd $(MPDM) ; $(MAKE) distcopy DIST_TARGET=$(DIST_TARGET)/mpdm ) ; \
	(cd $(MPSL) ; $(MAKE) distcopy DIST_TARGET=$(DIST_TARGET)/mpsl )

dist: distcopy po/minimum-profit.pot build-mo
	(cd /tmp ; tar czf - $(PROJ)-$(VERSION)/* ) > $(PROJ)-$(VERSION).tar.gz ; \
	rm -rf $(DIST_TARGET)

midnight:
	$(MAKE) distcopy DIST_TARGET=/tmp/mp-midnight
	(cd /tmp ; tar czf - mp-midnight/* ) > mp-midnight.tar.gz ; \
	rm -rf /tmp/mp-midnight

win32dist: wmp.exe
	mkdir -p win32
	mkdir -p win32/doc
	i586-mingw32msvc-strip $(APPNAME)
	install $(APPNAME) win32
	install mp_*.mpsl win32
	(DIR=`pwd` ; cd $(MPDM) ; make installdoc DOCDIR=$$DIR/win32/doc)
	(DIR=`pwd` ; cd $(MPSL) ; make installdoc DOCDIR=$$DIR/win32/doc)
	install $(DOCS) win32/doc
	(cd win32 ; aovwinstall mp-5 "Minimum Profit $(VERSION)" * doc/*)
	i586-mingw32msvc-strip win32/installer.exe
	mv win32/installer.exe mp-$(VERSION).exe
	rm -rf win32

innosetup: wmp.exe
	wine ~/.wine/fake_windows/Program\ Files/Inno\ Setup\ 5/ISCC.exe /O. /Fmp-$(VERSION) mp.iss

install: $(INSTALL_MSG) installdoc install-mo
	install $(APPNAME) $(PREFIX)/bin/$(APPNAME)
	./mkinstalldirs $(PREFIX)/share/$(APPNAME)
	install -m 644 mp_*.mpsl $(PREFIX)/share/$(APPNAME)
	./mkinstalldirs $(PREFIX)/share/man/man1
	[ -f $(APPNAME).1 ] && install -m 644 $(APPNAME).1 $(PREFIX)/share/man/man1/$(APPNAME).1 || true

installdoc:
	./mkinstalldirs $(PREFIX)/share/doc/$(APPNAME)
	( cd $(MPDM); $(MAKE) installdoc )
	( cd $(MPSL); $(MAKE) installdoc )
	install -m 644 doc/* $(PREFIX)/share/doc/$(APPNAME)
	install -m 644 $(ADD_DOCS) $(PREFIX)/share/doc/$(APPNAME)
	[-f doc/mp_index.html ] && install -m 644 doc/mp_index.html $(PREFIX)/share/doc/$(APPNAME)/index.html || true

uninstall: uninstall-mo
	rm -f $(PREFIX)/bin/$(APPNAME)
	rm -rf $(PREFIX)/share/$(APPNAME)
	rm -rf $(PREFIX)/share/doc/$(APPNAME)
	rm -f $(PREFIX)/share/man/man1/$(APPNAME).1

po/minimum-profit.pot:
	xgettext -o $@ --language=C --keyword=L --keyword=LL mp*.c *.mpsl

update-po:
	for a in po/*.po ; do \
		xgettext --omit-header -j -o $$a --language=C \
			--keyword=L --keyword=LL mp*.c *.mpsl ; \
	done

.po.mo:
	msgfmt -o $@ $<

build-mo:
	for i in po/*.po ; do \
		B=`basename $$i .po` ; o=po/$$B.mo ; \
		if [ $$i -nt $$o ] ; then msgfmt -o $$o $$i ; fi ; \
	done

install-mo: build-mo
	for a in po/*.mo ; do \
		B=`basename $$a .mo` ; \
		./mkinstalldirs $(PREFIX)/share/locale/$$B/LC_MESSAGES ; \
		install -m 644 $$a $(PREFIX)/share/locale/$$B/LC_MESSAGES/minimum-profit.mo ; \
	done

uninstall-mo:
	for a in po/*.mo ; do \
		B=`basename $$a .mo` ; \
		rm -f $(PREFIX)/share/locale/$$B/LC_MESSAGES/minimum-profit.mo ; \
	done

deb:
	dpkg-buildpackage -rfakeroot -b -uc -us

