# Makefile for tuxpaint-config

# Tux Paint Configuration Tool - A GUI-based configurator for
#                              Tux Paint, a simple drawing program for children.

# Copyright 2002-2025 by various contributors; see AUTHORS.txt
# http://www.tuxpaint.org/

# November 12, 2002 - May 26, 2025


# The version number, for release:
# (append "-rc#", e.g. "-rc1", when making release candidates
# for beta testing)

VER_VERSION=0.0.26


ifdef SOURCE_DATE_EPOCH
       VER_DATE=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null || date -u "+%Y-%m-%d")
else
       VER_DATE=$(shell date "+%Y-%m-%d")
endif


SYSNAME=$(shell uname -s)
ifeq ($(SYSNAME),Haiku)
	OS=haiku
endif

# Where to install things:

PREFIX:=/usr/local


# Program:

BIN_PREFIX=$(PREFIX)/bin
EXE_EXT=


# Data:

DATA_PREFIX=$(PREFIX)/share/tuxpaint-config/

# Tux Paint's data install (so we can try to point Pango, via FontConfig,
# to the fonts Tux Paint ships with, for the `uifont` list)

TUXPAINT_DATA_PREFIX=$(PREFIX)/share/tuxpaint


# Docs and man page:

DOC_PREFIX=$(PREFIX)/share/doc/tuxpaint-config/
MAN_PREFIX=$(PREFIX)/share/man/


# Tux Paint's 'System-wide' Config file:

ifeq ($(PREFIX),/usr)
  CONFDIR=/etc/tuxpaint
else ifeq ($(SYSNAME),Haiku)
  CONFDIR=$(shell finddir B_USER_SETTINGS_DIRECTORY)/TuxPaint
else
  CONFDIR=$(PREFIX)/etc/tuxpaint
endif


# Icons and launchers:

ICON_PREFIX=$(PREFIX)/share/pixmaps/
X11_ICON_PREFIX=$(PREFIX)/include/X11/pixmaps/
NEWICON_PREFIX:=$(PREFIX)/share/icons/hicolor


# Locale files

LOCALE_PREFIX=${DESTDIR}$(PREFIX)/share/locale/


# Libraries, paths, and flags:

FLTK_CFLAGS=`fltk-config --cxxflags`
FLTK_LIBS=`fltk-config --ldflags`

PANGO_CFLAGS=`pkg-config pangoft2 --cflags`
PANGO_LIBS=`pkg-config pangoft2 --libs`

PAPER_LIB=-lpaper

UNIBREAK_CFLAGS=`pkg-config --cflags libunibreak`
UNIBREAK_LIB=`pkg-config --libs libunibreak`

# Default CFLAGS:
CFLAGS=-O2 -Wall

# The entire set of CFLAGS:
ALL_CFLAGS=$(CFLAGS) $(FLTK_CFLAGS) $(UNIBREAK_CFLAGS) \
	-DDATA_PREFIX=\"$(DATA_PREFIX)\" \
	-DTUXPAINT_DATA_PREFIX=\"$(TUXPAINT_DATA_PREFIX)\" \
	-DDOC_PREFIX=\"$(DOC_PREFIX)\" \
	-DLOCALEDIR=\"$(LOCALE_PREFIX)\" -DCONFDIR=\"$(CONFDIR)\" \
	-DVER_VERSION=\"$(VER_VERSION)\" -DVER_DATE=\"$(VER_DATE)\"


# "make" with no arguments builds the program and man page from sources:

all:	tuxpaint-config translations
	@echo
	@echo "Done compiling."
	@if [ "x$$(uname -s)" = "xDarwin" ]; then \
		echo "Now run 'make install-macos' to build the macOS App Bundle."; \
	 else \
		echo "Now (probably as 'root' superuser), run 'make install'"; \
		echo "to install Tux Paint Configuration Tool."; \
	 fi
	@echo

releaseclean:
	@echo
	@echo "Cleaning release directory"
	@echo
	@rm -rf "build/tuxpaint-config-$(VER_VERSION)" "build/tuxpaint-config-$(VER_VERSION).tar.gz"
	@-if [ -d build ] ; then rmdir build ; fi

releasedir: build/tuxpaint-config-$(VER_VERSION)

build/tuxpaint-config-$(VER_VERSION):
	@echo
	@echo "Creating release directory"
	@echo
	@mkdir -p build/tuxpaint-config-$(VER_VERSION)
	@find . -follow \
		\( -wholename '*/.git' -o -name .gitignore -o -name .cvsignore -o -name 'build' -o -name '.#*' \) \
		-prune -o -type f -exec cp --parents -vdp \{\} build/tuxpaint-config-$(VER_VERSION)/ \;

release: releasedir
	@echo
	@echo "Creating release tarball"
	@echo
	@cd build ; \
		tar -czvf tuxpaint-config-$(VER_VERSION).tar.gz \
		tuxpaint-config-$(VER_VERSION)

.PHONY: macos install-macos

macos:
	make \
		PREFIX=./TuxPaint-Config.app/Contents/Resources \
		CFLAGS="-mmacosx-version-min=10.8 -isystem /opt/local/include -DHAVE_STRCASESTR -w -headerpad_max_install_names -Wall -O2" \
		ARCH_LINKS="-Wl,-framework,Security -lintl" \
		ARCH_LIBS="src/macos.m" \
		DESTDIR=./

install-macos:
	@echo
	@echo "...Installing App Bundle Support Files..."
	@install -d -m 755 ./TuxPaint-Config.app/Contents/MacOS
	@install -d -m 755 ./TuxPaint-Config.app/Contents/Resources/share
	@install -d -m 755 ./TuxPaint-Config.app/Contents/Resources/doc
	@install -d -m 755 ./TuxPaint-Config.app/Contents/Resources/man
	@install -d -m 755 ./TuxPaint-Config.app/Contents/Resources/locale
	@make install \
		BIN_PREFIX=./TuxPaint-Config.app/Contents/MacOS \
		PREFIX=./TuxPaint-Config.app/Contents/Resources \
		DATA_PREFIX=./TuxPaint-Config.app/Contents/Resources/share/ \
		DOC_PREFIX=./TuxPaint-Config.app/Contents/Resources/doc/ \
		MAN_PREFIX=./TuxPaint-Config.app/Contents/Resources/man/ \
		LOCALE_PREFIX=./TuxPaint-Config.app/Contents/Resources/locale \
		CONFDIR="/Library/Application Support/TuxPaint" \
		ARCH_LINKS=-lintl DESTDIR=./
	@VER_VERSION=$(VER_VERSION) macos/template.sh macos/Info.plist.shdoc > macos/Info.plist
	@install -m 644 macos/PkgInfo ./TuxPaint-Config.app/Contents
	@install -m 644 macos/Info.plist ./TuxPaint-Config.app/Contents
	@install -m 644 macos/tuxpaint.icns ./TuxPaint-Config.app/Contents/Resources
	@macos/build-app.sh
	@echo
	@echo "The App Bundle has been created as TuxPaint-Config.app!  Now you can:"
	@echo
	@echo "  * Double click TuxPaint-Config.app to run the application,"
	@echo "  * build the universal App Bundle,"
	@echo "  * and/or run 'make TuxPaint-Config.dmg' to create the DMG file for distribution."
	@echo
	@echo "For more information, see:"
	@echo "  macos/README.txt"
	@echo

TuxPaint-Config.dmg:
	@echo
	@echo "...Creating DMG Distribution File..."
	@macos/build-dmg.sh

win32:
	make \
		EXE_EXT=.exe \
		ICON_PREFIX=./ \
		X11_ICON_PREFIX=./ \
		CONFDIR=$(PREFIX)/etc/tuxpaint \
		ARCH_LINKS="-lintl -mwindows" \
		ARCH_LIBS="obj/resource.o obj/win32utils.o" \
		PAPER_LIB= 

haiku:
	make ARCH_LINKS="-lintl"

install-win32:
	@strip -s tuxpaint-config.exe
	make install \
		EXE_EXT=.exe \
		ICON_PREFIX=./ \
		X11_ICON_PREFIX=./ \
		CONFDIR=$(PREFIX)/etc/tuxpaint \
		ARCH_LINKS="-lintl -mwindows" \
		ARCH_LIBS="obj/resource.o obj/win32utils.o"

# "make install" installs all of the various parts
# (depending on the *PREFIX variables at the top, you probably need
# to do this as superuser ("root"))

install:	install-bin install-data install-man install-doc \
		install-icon install-gettext
	@echo
	@echo "All done! Now (preferably NOT as 'root' superuser),"
	@echo "you can type the command 'tuxpaint-config' to run the"
	@echo "configuration tool!"
	@echo
	@echo "Enjoy!"
	@echo


# "make clean" deletes the program, the compiled objects and the
# built man page (returns to factory archive, pretty much...)

clean:
	@echo "Cleaning up the build directory ($(PWD))"
	@-rm -f tuxpaint-config tuxpaint-config.exe
	@-rm -f obj/*.o
	@#if [ -d obj ]; then rmdir obj; fi
	@-rm -f trans/*.mo
	@if [ -d trans ]; then rmdir trans; fi
	@-rm -rf TuxPaint-Config.app TuxPaint-Config.dmg macos/Info.plist


# "make uninstall" should remove the various parts from their
# installation locations.  BE SURE the *PREFIX variables at the top
# are the same as they were when you installed, of course!!!

uninstall:
	-rm ${DESTDIR}$(ICON_PREFIX)tuxpaint-config.png
	-rm ${DESTDIR}$(X11_ICON_PREFIX)tuxpaint-config.xpm
	-rm ${DESTDIR}$(NEWICON_PREFIX)/*/apps/tuxpaint-config.png
	-rm ${DESTDIR}$(BIN_PREFIX)/tuxpaint-config
	-rm -r ${DESTDIR}$(DATA_PREFIX)
	-rm -r ${DESTDIR}$(DOC_PREFIX)
	-rm ${DESTDIR}$(MAN_PREFIX)/man1/tuxpaint-config.1.gz
	-rm $(LOCALE_PREFIX)/*/LC_MESSAGES/tuxpaint-config.mo


# Install the PNG icon (for GNOME, KDE, etc.)
# and the 24-color 32x32 XPM (for other Window managers):

install-icon:
	@echo
	@echo "...Installing launcher icon graphics..."
	@install -d ${DESTDIR}$(ICON_PREFIX)
	@install -m 644 data/images/icon.png ${DESTDIR}$(ICON_PREFIX)tuxpaint-config.png
	@install -d ${DESTDIR}$(X11_ICON_PREFIX)
	@install -m 644 data/images/icon32x32.xpm ${DESTDIR}$(X11_ICON_PREFIX)tuxpaint-config.xpm
	@install -d ${DESTDIR}$(NEWICON_PREFIX)/16x16/apps
	@install -d ${DESTDIR}$(NEWICON_PREFIX)/32x32/apps
	@install -d ${DESTDIR}$(NEWICON_PREFIX)/48x48/apps
	@cp data/images/icon16x16.png ${DESTDIR}$(NEWICON_PREFIX)/16x16/apps/tuxpaint-config.png
	@cp data/images/icon32x32.png ${DESTDIR}$(NEWICON_PREFIX)/32x32/apps/tuxpaint-config.png
	@cp data/images/icon48x48.png ${DESTDIR}$(NEWICON_PREFIX)/48x48/apps/tuxpaint-config.png
	@echo
	@echo "...Installing launcher..."
	@install -d ${DESTDIR}$(PREFIX)/share/applications/
	@install -m 644 src/tuxpaint-config.desktop ${DESTDIR}$(PREFIX)/share/applications/


# Install the program:

install-bin:
	@echo
	@echo "...Installing program itself..."
	@install -d ${DESTDIR}${BIN_PREFIX}
	@cp tuxpaint-config ${DESTDIR}$(BIN_PREFIX)
	@chmod a+rx,g-w,o-w ${DESTDIR}$(BIN_PREFIX)/tuxpaint-config$(EXE_EXT)


# Install the data (sound, graphics, fonts):

install-data:
	@echo
	@echo "...Installing data files..."
	@install -d ${DESTDIR}$(DATA_PREFIX)
	@cp -R data/* ${DESTDIR}$(DATA_PREFIX)
	@chmod -R a+rX,g-w,o-w ${DESTDIR}$(DATA_PREFIX)


# Install the text documentation:

install-doc:
	@echo
	@echo "...Installing documentation..."
	@echo "(English)"
	@install -d ${DESTDIR}$(DOC_PREFIX)
	@install -d ${DESTDIR}$(DOC_PREFIX)/html
	@cp docs/AUTHORS.txt ${DESTDIR}$(DOC_PREFIX)
	@cp docs/CHANGES.txt ${DESTDIR}$(DOC_PREFIX)
	@cp docs/COPYING.txt ${DESTDIR}$(DOC_PREFIX)
	@cp docs/README.txt ${DESTDIR}$(DOC_PREFIX)
	@cp docs/html/README.html ${DESTDIR}$(DOC_PREFIX)/html
	@cp docs/TODO.txt ${DESTDIR}$(DOC_PREFIX)
	@echo "(...Setting permissions)"
	@chmod -R a+rx,g-w,o-w ${DESTDIR}$(DOC_PREFIX)


# Install the man page:

install-man:
	@echo "...Installing man pages..."
	@install -d ${DESTDIR}$(MAN_PREFIX)/man1/
	@cp src/tuxpaint-config.1 ${DESTDIR}$(MAN_PREFIX)/man1/
	@gzip -9f ${DESTDIR}$(MAN_PREFIX)/man1/tuxpaint-config.1
	@chmod a+rx,g-w,o-w ${DESTDIR}$(MAN_PREFIX)/man1/tuxpaint-config.1.gz


# Build the program!

tuxpaint-config:	obj/tuxpaint-config.o obj/tuxpaint-config2.o	\
			obj/defaults.o $(ARCH_LIBS)
	@echo Linking
	$(CXX) $(ALL_CFLAGS) -o tuxpaint-config obj/tuxpaint-config.o 	\
			obj/tuxpaint-config2.o obj/defaults.o $(ARCH_LIBS) \
			$(FLTK_LIBS) $(ARCH_LINKS) $(PAPER_LIB) $(UNIBREAK_LIB) $(PANGO_LIBS)


# Build the object for the program!

obj/tuxpaint-config.o:	src/tuxpaint-config.cxx src/tuxpaint-config2.h src/defaults.h src/version.h src/about.h src/debug.h
	@echo Compiling tuxpaint-config.cxx
	$(CXX) $(ALL_CFLAGS) -c src/tuxpaint-config.cxx -o obj/tuxpaint-config.o

obj/tuxpaint-config2.o:	src/tuxpaint-config2.cxx src/tuxpaint-config2.h src/defaults.h src/version.h src/about.h
	@echo Compiling tuxpaint-config2.cxx
	$(CXX) $(ALL_CFLAGS) $(PANGO_CFLAGS) -c src/tuxpaint-config2.cxx -o obj/tuxpaint-config2.o

obj/defaults.o:	src/defaults.cxx src/defaults.h
	@echo Compiling defaults.cxx
	$(CXX) $(ALL_CFLAGS) -c src/defaults.cxx -o obj/defaults.o

obj/win32utils.o: src/win32utils.cxx src/win32utils.h
	@echo Compiling win32utils.cxx
	$(CXX) $(ALL_CFLAGS) -c src/win32utils.cxx -o obj/win32utils.o

obj/resource.o:	visualc/resource.rc visualc/resource.h
	@echo
	@echo "...Compiling win32 resources..."
	@windres -i visualc/resource.rc -o obj/resource.o


# Translation stuff:

POFILES:=$(wildcard src/po/*.po)
MOFILES:=$(patsubst src/po/%.po,trans/%.mo,$(POFILES))
INSTALLED_MOFILES:=$(patsubst trans/%.mo,$(LOCALE_PREFIX)/%/LC_MESSAGES/tuxpaint-config.mo,$(MOFILES))
INSTALLED_MODIRS:=$(patsubst trans/%.mo,$(LOCALE_PREFIX)/%/LC_MESSAGES,$(MOFILES))

$(INSTALLED_MODIRS): $(LOCALE_PREFIX)/%/LC_MESSAGES: trans/%.mo
	install -d -m 755 $@

$(INSTALLED_MOFILES): $(LOCALE_PREFIX)/%/LC_MESSAGES/tuxpaint-config.mo: trans/%.mo
	install -m 644 $< $@

.PHONY: uninstall-i18n
uninstall-i18n:
	-rm $(LOCALE_PREFIX)/*/LC_MESSAGES/tuxpaint-config.mo


# Install the translated text:
# We can install *.mo files if they were already generated, or if it can be
# generated from the *.po files.  The *.mo files can be generated from the
# *.po files if we have the converter program, msgfmt, installed in the
# system.  So we test for both and install them if either case is found
# to be true.  If neither case is found to be true, we'll just install
# Tux Paint without the translation files.
.PHONY: install-gettext
ifeq "$(wildcard trans/*.mo)$(shell msgfmt -h)" ""
install-gettext:
	@echo
	@echo "--------------------------------------------------------------"
	@echo "Cannot install translation files because no translation files"
	@echo "were found (trans/*.mo) and the 'msgfmt' program is not installed."
	@echo "You will not be able to run Tux Paint in non-U.S. English modes."
	@echo "--------------------------------------------------------------"
else
install-gettextdirs: $(INSTALLED_MODIRS)
install-gettext: install-gettextdirs $(INSTALLED_MOFILES)
endif


# Build the translation files for gettext

$(MOFILES): trans/%.mo: src/po/%.po trans
	msgfmt -o $@ $<

.PHONY: translations
ifeq "$(shell msgfmt -h)" ""
translations: trans
	@echo "--------------------------------------------------------------"
	@echo "Cannot find program 'msgfmt'!"
	@echo "No translation files will be prepared."
	@echo "Install gettext to run Tux Paint in non-U.S. English modes."
	@echo "--------------------------------------------------------------"
else
translations: trans $(MOFILES)
endif

trans:
	@echo
	@echo "...Preparing translation files..."
	@mkdir trans


# Make the "obj" directory to throw the object(s) into:

obj:
	@mkdir obj


# vim:noet
