# Makefile -- Tux Commander build system
#
#    Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net>
#    Check for updates on tuxcmd.sourceforge.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty or
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


#  Specify paths
DEFAULT_DESTDIR=/usr

# following paths are optional; needed only for Kylix compilation
KYLIXPREFIX=/usr/local/kylix3
GTK2FORPASCAL_LIBDIR=./gtk2

###  End of user writeable section



DESTDIR=$(DEFAULT_DESTDIR)
INSTALL=install
CLEAN_OBJS=*.o *.a *.d *.s *.ppu *.dcu *.rst *~ *.~* core tuxcmd fpcver.inc
LIB_SUFFIX=`if test \`uname -m\` = x86_64 -o \`uname -m\` = ppc64; then echo 64; fi`


# -Ct must be disabled here, causes unneeded crashes
# -CR and -Cr must be disabled here, causes range check errors with gnome-power-manager
# -Cg generates internal compiler errors on i386
# -XX prevents linking errors of unresolved and unused symbols
# "-k-z noexecstack" (with quotes as a single parameter) avoids marking stack as executable, allowing to work correctly with SELinux in Enforcing mode
CFLAGS= -vweh -Un -Ci -Co -XX "-k-z noexecstack"
EXTRA_CFLAGS= -O3


tuxcmd::
	echo "const" > fpcver.inc
	echo "  ConstFPCVersionString = '`fpc -iW`';" >> fpcver.inc
	echo "  ConstFPCDateString = '`fpc -iD`';" >> fpcver.inc
	echo "  ConstFPCCompilerOSString = '`fpc -iSO`';" >> fpcver.inc
	echo "  ConstFPCCompilerHostProcessorString = '`fpc -iSP`';" >> fpcver.inc
	echo "  ConstFPCTargetOSString = '`fpc -iTO`';" >> fpcver.inc
	echo "  ConstFPCTargetProcessorString = '`fpc -iTP`';" >> fpcver.inc
	fpc -Mdelphi $(CFLAGS) $(EXTRA_CFLAGS) \
	    -Fu./libgtk_kylix -Fu./translations -Fu./vfs \
	    tuxcmd.dpr


debug: EXTRA_CFLAGS = -g -gl -gv -O-
debug: tuxcmd

final_debug: EXTRA_CFLAGS = -g -gl -gv -O3
final_debug: tuxcmd

kylix::
	$(KYLIXPREFIX)/bin/dcc -DKYLIX -V -H -W -$$X+ -$$I+ -$$D+ -$$I+ -$$L+ -$$M+ -$$Q+ -$$R+ -$$W+ -$$Y+ \
	-U$(GTK2FORPASCAL_LIBDIR)/glib -U$(GTK2FORPASCAL_LIBDIR)/pango \
	-U$(GTK2FORPASCAL_LIBDIR)/atk -U$(GTK2FORPASCAL_LIBDIR)/gtk+/gtk \
	-U$(GTK2FORPASCAL_LIBDIR)/gtk+/gdk -U$(GTK2FORPASCAL_LIBDIR)/gtk+/gdk-pixbuf \
	-U$(KYLIXPREFIX)/lib \
	-U./libgtk_kylix -U./translations -U./vfs -U./compat ./tuxcmd.dpr


modules::
	(cd "vfs" && $(MAKE) `echo $@ | sed s/-recursive//` ) || exit 1;

modules_install:
	(cd "vfs" && $(MAKE) install "$(DESTDIR)") || exit 1;



clean cleandir:
	rm -f $(CLEAN_OBJS)
	( cd vfs && rm -f $(CLEAN_OBJS) )
	( cd libgtk_kylix && rm -f $(CLEAN_OBJS) )
	( cd translations && rm -f $(CLEAN_OBJS) )
	( cd compat && rm -f $(CLEAN_OBJS) )


gtk_update_icon_cache = gtk-update-icon-cache -f -t $(DESTDIR)/share/icons/hicolor

tuxcmd_install:
	$(INSTALL) -d $(DESTDIR)/bin
	$(INSTALL) -m 755 tuxcmd $(DESTDIR)/bin
	$(INSTALL) -d $(DESTDIR)/lib$(LIB_SUFFIX)/tuxcmd
	$(INSTALL) -d $(DESTDIR)/share/doc/tuxcmd
	$(INSTALL) -m 644 COPYING $(DESTDIR)/share/doc/tuxcmd
	$(INSTALL) -m 644 README $(DESTDIR)/share/doc/tuxcmd
	
	desktop-file-install -m 644 --dir $(DESTDIR)/share/applications data/tuxcmd.desktop
	
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/16x16/apps
	$(INSTALL) -m 644 data/icons/16x16/tuxcmd.png $(DESTDIR)/share/icons/hicolor/16x16/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/24x24/apps
	$(INSTALL) -m 644 data/icons/24x24/tuxcmd.png $(DESTDIR)/share/icons/hicolor/24x24/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/32x32/apps
	$(INSTALL) -m 644 data/icons/32x32/tuxcmd.png $(DESTDIR)/share/icons/hicolor/32x32/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/48x48/apps
	$(INSTALL) -m 644 data/icons/48x48/tuxcmd.png $(DESTDIR)/share/icons/hicolor/48x48/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/64x64/apps
	$(INSTALL) -m 644 data/icons/64x64/tuxcmd.png $(DESTDIR)/share/icons/hicolor/64x64/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/128x128/apps
	$(INSTALL) -m 644 data/icons/128x128/tuxcmd.png $(DESTDIR)/share/icons/hicolor/128x128/apps
	$(INSTALL) -d $(DESTDIR)/share/icons/hicolor/scalable/apps
	$(INSTALL) -m 644 data/icons/scalable/tuxcmd.svg $(DESTDIR)/share/icons/hicolor/scalable/apps
	
	@-if test "$(DESTDIR)" = "$(DEFAULT_DESTDIR)"; then \
		echo "Updating Gtk icon cache."; \
		$(gtk_update_icon_cache); \
	else \
		echo "*** Icon cache not updated.  After install, run this:"; \
		echo "***   $(gtk_update_icon_cache)"; \
	fi


uninstall:
	if [ -f $(DESTDIR)/bin/tuxcmd ]; then rm -f $(DESTDIR)/bin/tuxcmd; fi;
	if [ -d $(DESTDIR)/share/doc/tuxcmd ]; then rm -Rf $(DESTDIR)/share/doc/tuxcmd; fi;
	if [ -d $(DESTDIR)/lib$(LIB_SUFFIX)/tuxcmd ]; then rm -Rf $(DESTDIR)/lib$(LIB_SUFFIX)/tuxcmd; fi;

help::
	@echo "Tux Commander build system"
	@echo
	@echo "Following targets are available:"
	@echo
	@echo "  tuxcmd              Build tuxcmd with FreePascal [default]"
	@echo "  kylix               Build tuxcmd with Kylix (needs paths setup in Makefile)"
	@echo "  install             Install tuxcmd to the system (DESTDIR overrides default path)"
	@echo "  uninstall           Remove tuxcmd from the system (DESTDIR overrides default path)"
	@echo "  clean               Cleanup the build structure"
	@echo "  modules             Build modules (in vfs/)"
	@echo "  modules_install     Install modules to the system"
	@echo
	@exit 0

install: tuxcmd_install

all: tuxcmd
