debug?=0

name:=landed
appname:=LANDED.app
version:=1.0.2
prefix?=$(HOME)
universal?=0
debug?=0

ifdef SystemRoot
system:=Windows
endif

ifeq "$(system)" "Windows"
c:=gcc
dc:=gcc
cc:=g++
dcc=g++
link:=$(cc)
moc=moc
rm=del
else

rm=rm -f

system:=$(shell uname -s)
QTDIR?=$(shell (which qmake-qt4 2>/dev/null || which qmake) | sed 's/\/bin\/qmake.*//g')
moc?=$(shell which moc-qt4 >/dev/null 2>&1 && echo moc-qt4 || echo moc)

opt:=-O3
ifeq "$(debug)" "1"
opt=-g
endif

cc:=g++
dcc:=$(cc)
c:=gcc
dc:=$(c)

link:=g++ $(opt)
cc+= $(opt) 
c+=$(opt)
link:=g++ $(opt)

endif

cc+=-DVERSION=\"$(version)\"

.PHONY: clean all install dist

target:=$(name)
ifeq "$(system)" "Windows"
target:=$(name).exe
endif
all: $(target)

ifeq "$(system)" "Windows"
-include makefile.qt.win
else
cc+=-I/opt/local/include
dcc+=-isystem/opt/local/include
link+=-L/opt/local/lib
ifeq "$(debug)" "1"	
$(shell perl findqt.pl debug)
else 
ifeq "$(universal)" "1"
$(shell perl findqt.pl universal)
else
$(shell perl findqt.pl)
endif
endif
-include makefile.qt
endif
ifeq "$(system)" "Linux"
	QT_LIBS+= -lGLU
endif

cc+=$(QT_INCPATH) $(QT_CXXFLAGS) -Wno-sign-compare
ifeq "$(system)" "Darwin"
#cc+=-Wno-unused-private-field 
ifeq "$(universal)" "1"
arches=-arch i386 -arch x86_64
cc+=$(arches)
link+=$(arches)
c+=$(arches)
endif
endif

dcc+=$(QT_INCDEP) $(QT_CXXFLAGS)
link+=$(QT_LFLAGS)

#intermediate code required by qt
mobjs:=gui.hh viewer3d.hh projection.hh dialogs.hh movie.hh landed.hh helpers.hh

$(mobjs:.hh=-moc.cc): %-moc.cc: %.hh
	$(moc) -o $@ $<

#objects
objs= $(mobjs:.hh=.o) viewerbase.o $(mobjs:.hh=-moc.o) functions.o pdgtest.o pdgmap.o
$(objs:.o=.d): %.d :%.cc
	$(dcc) -o $@ -MM -MT '$@ $(@:.d=.o)' $<
$(objs):
	$(cc) -o $@ -c $<
-include $(objs:.o=.d)

cobjs=listfont.o
$(cobjs:.o=.d): %.d :%.c
	$(dc) -o $@ -MM -MT '$@ $(@:.d=.o)' $<
$(cobjs):
	$(c) -o $@ -c $<
-include $(cobjs:.o=.d)

ifeq "$(system)" "Windows"
$(name).ico: $(name).png
	convert -scale 256x256 $(name).png $(name).ico
eddb.ico: eddb.png
	convert -scale 256x256 eddb.png eddb.ico
objs+=resource.o
resource.o: $(name).rc $(name).ico eddb.ico
	windres -Ocoff -o $@ $<
endif

$(target): $(filter-out pdgtest.o,$(objs)) $(cobjs)
	$(link) -o $@ $^ -lz $(QT_LIBS)
ifeq "$(system)" "Linux"
	cp $(target)-head.sh $(target)-installer.sh
	base64 < $(target) >> $(target)-installer.sh
	cat $(target)-tail.sh >> $(target)-installer.sh
	chmod +x $(target)-installer.sh
endif

pdgtest: helpers.o helpers-moc.o pdgtest.o listfont.o
	$(link) -o $@ $^ $(QT_LIBS)

clean:
	$(rm) $(name) *.o *-moc.cc *.d $(name)*.exe *.tar.gz *.dmg *.ico *.icns pdgtest $(name)-installer.sh *~
ifneq "$(system)" "Windows"
	rm -rf landed.iconset eddb.iconset
endif

install: all
ifeq "$(system)" "Darwin"
	rm -rf $(appname)
	mkdir -p $(appname)/Contents/MacOS
	cp $(name) $(appname)/Contents/MacOS/
	mkdir $(appname)/Contents/Resources
	cp Info.plist $(appname)/Contents/Info.plist
	[ -d $(name).iconset ] || mkdir $(name).iconset
	for size in 512 256 128 32 16; do \
	  convert -scale "$$size"x"$$size" $(name).png $(name).iconset/icon_"$$size"x"$$size".png; \
	done
	png2icns $(name).icns $(name).iconset/*.png
	[ -d eddb.iconset ] || mkdir eddb.iconset
	for size in 512 256 128 32 16; do \
	  convert -scale "$$size"x"$$size" eddb.png eddb.iconset/icon_"$$size"x"$$size".png; \
	done
	png2icns eddb.icns eddb.iconset/*.png
	cp $(name).icns eddb.icns $(appname)/Contents/Resources/ 
	cp ffmpeg-mac $(appname)/Contents/MacOS/ffmpeg
	macdeployqt $(appname) -executable=$(appname)/Contents/MacOS/ffmpeg -dmg
	osascript -e "tell Application \"Finder\" to move POSIX file \"`pwd`/$(appname)\" to the Desktop replacing true"
else
	[ -d $(prefix)/bin ] || mkdir $(prefix)/bin
	strip -o $(prefix)/bin/$(name) $(name)
endif

dist: $(name)-$(version).tar.gz

$(name)-$(version).tar.gz:
	make clean
	- [ -e /tmp/$(name)-$(version) ] && rm -rf /tmp/$(name)-$(version)
	mkdir /tmp/$(name)-$(version)
	cp -R * /tmp/$(name)-$(version)/
	tar -C /tmp -czf $(name)-$(version).tar.gz $(name)-$(version) 
	rm -rf /tmp/$(name)-$(version)

