# automatically created by config.sh - do not modify
# CC=gcc
# CPP=g++
# MOC=moc
# CFLAGS=-g -Wall
MPDM=./mpdm
MPSL=./mpsl
BUILDMO=build-mo
INSTALLMO=install-mo
UNINSTALLMO=uninstall-mo
DOCS=$(ADD_DOCS)
VERSION=5.1.4
WINDRES=windres
PREFIX=$(DESTDIR)/usr
APPNAME=mp-5
DRV_OBJS=mpv_curses.o 
CCLINK=$(CC) -g -Wall

# -*- Mode: sh

all: $(APPNAME) docs $(BUILDMO)

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 doc/mp_keycodes.html \
	doc/mp_encoding.html $(APPNAME).1
MP_DOCCER_DOCS=doc/mp_api.txt
G_AND_MP_DOCS=doc/mp_api.html

OBJS=mp_core.o $(DRV_OBJS)

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
	$(CCLINK) $(CFLAGS) $(OBJS) `cat config.ldflags` -o $@

mpv_kde4.o: mpv_kde4.cpp mpv_kde4.moc
	$(CPP) $(CFLAGS) -I. `cat config.cflags` -c $<

mpv_kde4.moc: mpv_kde4.cpp
	$(MOC) -o mpv_kde4.moc mpv_kde4.cpp

mpv_qt4.o: mpv_qt4.cpp mpv_qt4.moc
	$(CPP) $(CFLAGS) -I. `cat config.cflags` -c $<

mpv_qt4.moc: mpv_qt4.cpp
	$(MOC) -o mpv_qt4.moc mpv_qt4.cpp

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

mp-5.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 *.moc 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 $(MP_DOCCER_DOCS) 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.txt: *.mpsl
	mp_doccer *.mpsl -o doc/mp_api -f grutatxt \
		-t "Minimum Profit API" \
		-b "This reference documents version $(VERSION) of the MPSL API." \
		-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: mp-5.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: mp-5.exe
	wine ~/.wine/fake_windows/Program\ Files/Inno\ Setup\ 5/ISCC.exe /O. /Fmp-$(VERSION) mp.iss

install: $(INSTALL_MSG) installdoc $(INSTALLMO)
	install $(APPNAME) $(PREFIX)/bin/$(APPNAME)
	./mkinstalldirs $(PREFIX)/share/$(APPNAME)
	./mkinstalldirs $(PREFIX)/share/$(APPNAME)/lang
	install -m 644 mp_*.mpsl $(PREFIX)/share/$(APPNAME)
	install -m 644 lang/*.mpsl $(PREFIX)/share/$(APPNAME)/lang
	./mkinstalldirs $(PREFIX)/share/man/man1
	[ -f $(APPNAME).1 ] && install -m 644 $(APPNAME).1 $(PREFIX)/share/man/man1/$(APPNAME).1 || true
	( cd $(MPSL) ; [ -x mpsl ] && install mpsl $(PREFIX)/bin || 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: $(UNINSTALLMO)
	rm -f $(PREFIX)/bin/$(APPNAME)
	rm -rf $(PREFIX)/share/$(APPNAME)
	rm -rf $(PREFIX)/share/doc/$(APPNAME)
	rm -f $(PREFIX)/share/man/man1/$(APPNAME).1
	rm -f $(PREFIX)/bin/mpsl

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

update-po:
	for a in po/*.po ; do \
		sed -i -e '/^#/d' $$a ; \
		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 -o ! -f $$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

mp_core.o: mp_core.c config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h
mpv_curses.o: mpv_curses.c config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h
mpv_gtk.o: mpv_gtk.c config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h mp.xpm
mpv_win32.o: mpv_win32.c config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h
mpv_kde4.o: mpv_kde4.cpp mpv_qk_common.cpp config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h mp.xpm
mpv_qt4.o: mpv_qt4.cpp mpv_qk_common.cpp config.h $(MPDM)/mpdm.h $(MPSL)/mpsl.h mp.h mp.xpm
