CC = gcc
CFLAGS = $(GLOBAL_CFLAGS) $(APP_CFLAGS) $(LINUX_STATIC_CFLAGS)
CXXFLAGS = $(CFLAGS)
PLATFORM_OBJECTS = t3f/menu.o $(LINUX_STATIC_PLATFORM_OBJECTS)
PLATFORM_LIBS = -lallegro_dialog$(LIB_SUFFIX) $(LINUX_STATIC_PLATFORM_LIBS)
DEPEND_LIBS = `pkg-config --libs --static allegro-static-5` `pkg-config --libs --static allegro_audio-static-5` `pkg-config --libs --static allegro_image-static-5` `pkg-config --libs --static allegro_acodec-static-5` `pkg-config --libs --static allegro_font-static-5` `pkg-config --libs --static allegro_ttf-static-5` `pkg-config gtk+-x11-2.0 --libs` -lpthread -lm -lstdc++ $(LINUX_STATIC_DEPEND_LIBS)
LIB_SUFFIX = -static
DEL_COMMAND = rm -f
DEL_FOLDER_COMMAND = rm -rf
SED_COMMAND = sed -i
COPY_COMMAND = cp
PATH_SEPARATOR = /

include makefile.common

prepare_platform:
	@echo Ready to build...

../debian/control_gen: ../debian/control_gen.o
	$(CC) $(GLOBAL_CFLAGS) ../debian/control_gen.o -o ../debian/control_gen

#generate Debian package
debian_package: $(APP_EXE_NAME) ../debian/control_gen
	echo Generating files...
	echo "!C menu-2\n?package("$(DEBIAN_APP_NAME)"):\n	needs=\"X11\"\n	section=\"$(DEBIAN_MENU_SECTION)\"\n	title=\""$(APP_TITLE)"\"\n	command=\"/usr/$(DEBIAN_DIR)/$(DEBIAN_APP_NAME)\"\n	;" > ../debian/menu
	echo "[Desktop Entry]\nName="$(APP_TITLE)"\nExec=/usr/$(DEBIAN_DIR)/$(DEBIAN_APP_NAME)\nIcon=$(DEBIAN_APP_NAME)\nTerminal=false\nType=Application\nCategories=$(DEBIAN_DESKTOP_CATEGORIES);" > ../debian/app.desktop
	../debian/control_gen ../debian $(DEBIAN_APP_NAME) $(APP_VERSION) $(DEBIAN_SECTION) optional `dpkg-architecture -l -qDEB_BUILD_ARCH` $(DEBIAN_DEPENDS) "`du -s --block-size=1024 ../bin`" $(DEBIAN_MAINTAINER) $(DEBIAN_DESCRIPTION) $(DEBIAN_LONG_DESCRIPTION)
	echo Building directory structure...
	mkdir -p ../temp
	mkdir -p ../temp/debian_temp
	mkdir -p ../temp/DEBIAN
	mkdir -p ../temp/debian_temp/usr
	mkdir -p ../temp/debian_temp/usr/share
	mkdir -p ../temp/debian_temp/usr/share/$(DEBIAN_APP_NAME)
	mkdir -p ../temp/debian_temp/usr/share/doc
	mkdir -p ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)
	mkdir -p ../temp/debian_temp/usr/share/pixmaps
	mkdir -p ../temp/debian_temp/usr/share/applications
	mkdir -p ../temp/debian_temp/usr/share/menu
	mkdir -p ../temp/debian_temp/usr/$(DEBIAN_DIR)
	echo Copying files...
	cp -a ../bin/data ../temp/debian_temp/usr/share/$(DEBIAN_APP_NAME)
	cp ../docs/changelog ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/changelog
	cp ../docs/README ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/README
	cp ../docs/copyright ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/copyright
	cp ../icons/icon.svg ../temp/debian_temp/usr/share/pixmaps/$(DEBIAN_APP_NAME).svg
	cp ../debian/app.desktop ../temp/debian_temp/usr/share/applications/$(DEBIAN_APP_NAME).desktop
	cp ../debian/menu ../temp/debian_temp/usr/share/menu/$(DEBIAN_APP_NAME)
	cp ../bin/$(DEBIAN_APP_NAME) ../temp/debian_temp/usr/$(DEBIAN_DIR)/$(DEBIAN_APP_NAME)
	cp ../debian/control ../temp/DEBIAN
	cp ../debian/postinst ../temp/DEBIAN
	echo Building package...
	strip ../temp/debian_temp/usr/$(DEBIAN_DIR)/$(DEBIAN_APP_NAME)
	chmod 755 ../temp/debian_temp/usr/$(DEBIAN_DIR)/$(DEBIAN_APP_NAME)
	chmod 755 ../temp/DEBIAN/postinst
	find ../temp/debian_temp -type d | xargs chmod 755
	find ../temp/debian_temp/usr/share/$(DEBIAN_APP_NAME) -type f -exec chmod 644 {} \;
	chmod 644 ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/README
	chmod 644 ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/changelog
	chmod 644 ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/copyright
	gzip -9 ../temp/debian_temp/usr/share/doc/$(DEBIAN_APP_NAME)/changelog
	chmod 644 ../temp/debian_temp/usr/share/pixmaps/$(DEBIAN_APP_NAME).svg
	chmod 644 ../temp/debian_temp/usr/share/applications/$(DEBIAN_APP_NAME).desktop
	chmod 644 ../temp/debian_temp/usr/share/menu/$(DEBIAN_APP_NAME)
	cd ../temp/debian_temp; find ./ ! -type d -print0 | xargs -0 md5sum >../DEBIAN/md5sums-pre; sed s-./-- <../DEBIAN/md5sums-pre >../DEBIAN/md5sums; chmod 644 ../DEBIAN/md5sums
	rm -f ../temp/DEBIAN/md5sums-pre
	mv ../temp/DEBIAN ../temp/debian_temp
	fakeroot dpkg-deb --build ../temp/debian_temp
	mv ../temp/debian_temp.deb $(APP_PACKAGE_DIR)/$(DEBIAN_APP_NAME)_$(APP_VERSION)_`dpkg-architecture -l -qDEB_BUILD_ARCH`.deb
	echo Cleaning up...
	rm -rf ../temp
	rm -f ../debian/control
	rm -f ../debian/menu
	rm -f ../debian/app.desktop
	rm -f ../debian/control_gen
	rm -f ../debian/control_gen.o
	echo Testing package...
	lintian $(APP_PACKAGE_DIR)/$(DEBIAN_APP_NAME)_$(APP_VERSION)_`dpkg-architecture -l -qDEB_BUILD_ARCH`.deb
