DEPTH = .

include $(DEPTH)/Makefile.version
include $(DEPTH)/Makefile.os

ifeq ($(OPERATING_SYSTEM), Linux)
	PROGNAME ?= FreeDoko
endif
ifeq ($(OPERATING_SYSTEM), Windows)
	PROGNAME ?= FreeDoko.exe
endif

export PROGNAME


# Edited for Debian GNU/Linux.
DESTDIR = 


.PHONY: all
all: Version

Version : ChangeLog
ifeq ($(SHELLTYPE), sh)
	@echo -e "Version: "${VERSION}" (alpha)" >Version
	@echo "Date: "`date +%Y-%m-%d` >>Version
endif

.PHONY: delete
delete : clean

.PHONY: clean
clean :
ifeq ($(SHELLTYPE), sh)
	rm -f *~
	rm -rf release_*
	@for d in *; \
	do	if test -d $$d; \
		then	if test -e $$d/Makefile; \
			then	$(MAKE) -C $$d clean; \
			fi \
		fi \
	done
	find . -name "*~" -exec rm \{\} \;
	find . -name "*.bak" -exec rm \{\} \;
	find . -name "core" -exec rm \{\} \;
	find . -name ".#*" -exec rm \{\} \;
	find . -name "#*#" -exec rm \{\} \;
	find . -name "a2psout.ps" -exec rm \{\} \;
	-find . -name ".thumbnails" -exec rm -rf \{\} \;
endif

ifeq ($(SHELLTYPE), sh)
.PHONY: chmod
chmod :
	find -type d -exec chmod +x \{\} \;
	chmod -R go-w .
	chmod -R a+rX .
	find . ! -type d ! -type l -exec chmod -x \{\} \;
	find -name "c" -exec chmod +x \{\} \;
	find -name "FreeDoko" -exec chmod +x \{\} \;
	find -name "*.bat" -exec chmod +x \{\} \;
	find -name "*.exe" -exec chmod +x \{\} \;
	chmod a+x data/setup/debian/*/rules
	for f in data/bin/*; do \
	  (head -n 1 $$f | grep -q '^#!') && chmod +x $$f; \
	done
	@#find . -name "*.cpp" -exec chmod -x \{\} \;
	@#find . -name "*.h" -exec chmod -x \{\} \;
endif

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY: release
release : clean
	rm -rf ../FreeDoko_$(VERSION).release/
	mkdir -p ../FreeDoko_$(VERSION).release/
	$(MAKE) release_directory
	$(MAKE) release_data
	@# Program file
	$(MAKE) release_linux_binary
	@# package
	#@ - Linux
	tar czf ../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).Linux.tgz -C release FreeDoko_$(VERSION)
	rm release/FreeDoko_$(VERSION)/FreeDoko
	@# - Windows
	$(MAKE) release_windows_binary
	@cd release \
	  && zip -q9r ../../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).Windows.zip FreeDoko_$(VERSION)
	@cp -a data/gtkmm.dll release/FreeDoko_$(VERSION)/
	@$(MAKE) -C data/setup/Windows setup
	@mv data/setup/Windows/*.Setup.exe ../FreeDoko_$(VERSION).release/
	@rm -r release/FreeDoko_$(VERSION)/gtkmm.dll
	@rm release/FreeDoko_$(VERSION)/FreeDoko.exe
	@# Data files
	@#cd data/;\
	@#for d in graphics translations; do \
	@#  zip -q9r ../../FreeDoko_$(VERSION).release/$${d}_$(VERSION).zip $$d;\
	@#done;
	@# Source
	$(MAKE) release_source
	@# packages
	$(MAKE) packages
	cd ../FreeDoko_$(VERSION).release/ \
	  && cp -a /home/install/mirrors/FreeDoko/ debian \
	  && find debian/ -name "*.deb" ! -name "*$(VERSION)*.deb" -exec rm \{\} \;
	cd ../FreeDoko_$(VERSION).release/ \
	  && cp debian/pool/non-free/FreeDoko/freedoko-altenburg_$(VERSION)*.deb . \
	  && fakeroot alien --to-rpm freedoko-altenburg_$(VERSION)*.deb
	rm -rf ../FreeDoko_$(VERSION)
	mv release/FreeDoko_$(VERSION) ../
	@# copy to zip
	#-mount /media/zip
	#cp -av ../FreeDoko_$(VERSION).release/ /media/zip/

.PHONY: release_directory
release_directory :
	rm -rf release/
	mkdir -p release/FreeDoko_$(VERSION)

.PHONY: release_data
release_data :
	cp AUTHORS COPYING ChangeLog README LIESMICH Version release/FreeDoko_$(VERSION)/
	$(MAKE) -C data release
	$(MAKE) -C doc release

.PHONY: release_linux_binary
release_linux_binary :
	cp src/constants.h src/constants.h.backup
	mv src/Makefile.local src/Makefile.local.backup
	touch src/Makefile.local
	cat src/constants.h.backup \
	  | sed "s/^\/\/#define RELEASE/#define RELEASE/" \
	  > src/constants.h
	$(MAKE) -C src $(PROGNAME)
	mv src/constants.h.backup src/constants.h
	mv src/Makefile.local.backup src/Makefile.local
	touch src/constants.h
	cp src/$(PROGNAME) release/FreeDoko_$(VERSION)/

.PHONY: release_windows_binary
release_windows_binary :
	-cp /mnt/Daten/FreeDoko/src/FreeDoko.ico release/FreeDoko_$(VERSION)/ && \
	chmod -x release/FreeDoko_$(VERSION)/FreeDoko.ico && \
	cp /mnt/Daten/FreeDoko/src/FreeDoko.exe release/FreeDoko_$(VERSION)/ && \
	cp data/bin/FreeDoko.bat release/FreeDoko_$(VERSION)/

release_source :
	$(MAKE) release_directory
	$(MAKE) release_data
	$(MAKE) -C src clean
	#for d in AUTHORS COPYING ChangeLog Makefile* README LIESMICH Version debian src; do
	for d in Makefile* debian src; do\
		cp -a $$d release/FreeDoko_$(VERSION)/;\
	done
	# source
	rm release/FreeDoko_$(VERSION)/src/Makefile.local
	echo "override CPPFLAGS += -DPUBLIC_DATA_DIRECTORY_VALUE=\"\\\"../data\\\"\"" \
		> release/FreeDoko_$(VERSION)/src/Makefile.local
	# documentation
	cp doc/Makefile release/FreeDoko_$(VERSION)/doc/
	cp doc/Makefile.latex release/FreeDoko_$(VERSION)/doc/
	for d in de en; do \
		$(MAKE) -C doc/$$d clean; \
		cp -a doc/$$d release/FreeDoko_$(VERSION)/doc/; \
	done
	# data
	mkdir -p release/FreeDoko_$(VERSION)/data
	for d in translations rules ai cardsets backgrounds; do \
		mv release/FreeDoko_$(VERSION)/$$d release/FreeDoko_$(VERSION)/data/; \
	done
	cd data/;\
	  for f in Makefile bin setup; do\
		cp -a $$f ../release/FreeDoko_$(VERSION)/data/;\
	  done; \
	for d in translations rules ai cardsets backgrounds; do \
		if test -f $$d/Makefile; then \
			cp -a $$d/Makefile ../release/FreeDoko_$(VERSION)/data/$$d/;\
		fi; \
	done;
	#chmod u+w release/FreeDoko_$(VERSION) -R
	cd release/; \
	  zip -q9r ../../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).src.zip FreeDoko_$(VERSION)
endif

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY: install
install :
	$(MAKE) release_directory
	$(MAKE) release_data
	$(MAKE) -C src FreeDoko
	# Copying the program
	mkdir -p $(DESTDIR)/usr/games
	cp release/FreeDoko_$(VERSION)/FreeDoko $(DESTDIR)/usr/games/
	cd $(DESTDIR)/usr/games/; ln -s FreeDoko freedoko
	# Copying the program files
	mkdir -p $(DESTDIR)/usr/share/games/FreeDoko
	for d in translations rules ai cardsets backgrounds; do \
	  cp -a release/FreeDoko_$(VERSION)/$$d $(DESTDIR)/usr/share/games/FreeDoko/; \
	done
	#cp -a release_$(VERSION)/doc $(DESTDIR)/usr/share/games/FreeDoko/
endif
	# Copying the documentation
	mkdir -p $(DESTDIR)/usr/share/doc/freedoko
	cp -a release/FreeDoko_$(VERSION)/doc/* release/FreeDoko_$(VERSION)/AUTHORS release/FreeDoko_$(VERSION)/COPYING release/FreeDoko_$(VERSION)/ChangeLog $(DESTDIR)/usr/share/doc/FreeDoko/
	# update the manual page
	cd debian; \
	  docbook-to-man freedoko.sgml > freedoko.6
	mkdir -p $(DESTDIR)/usr/share/man/man6/
	cp debian/freedoko.6 $(DESTDIR)/usr/share/man/man6/
	cp -a debian/FreeDoko.6 $(DESTDIR)/usr/share/man/man6/
	# copy the icon
	mkdir -p $(DESTDIR)/usr/share/pixmaps/
	cp src/FreeDoko.png $(DESTDIR)/usr/share/pixmaps/

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY: packages
packages :
	$(MAKE) -C data/setup/debian packages
endif

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY: CD
CD :
	-rm -r /tmp/FreeDoko_$(VERSION).CD
	-mkdir /tmp/FreeDoko_$(VERSION).CD
	# copy the installation
	mkdir /tmp/FreeDoko_$(VERSION).CD/Installation
	cp -a ../FreeDoko_$(VERSION).release/* /tmp/FreeDoko_$(VERSION).CD/Installation/
	rm -rf /tmp/FreeDoko_$(VERSION).CD/Installation/debian
	cp -a /home/install/mirrors/FreeDoko /tmp/FreeDoko_$(VERSION).CD/debian
	#   cardsets
	mkdir /tmp/FreeDoko_$(VERSION).CD/Installation/cardsets
	for c in xskat kdecarddecks pysol; do \
	  pushd .; \
	  cd ~/Daten/FreeDoko/cardsets; \
	  zip -r /tmp/$$c.zip $$c; \
	  popd; \
	  mv /tmp/$$c.zip /tmp/FreeDoko_$(VERSION).CD/Installation/cardsets/; \
	done
	# program files
	DIR=`pwd`; \
	  cd /tmp/FreeDoko_$(VERSION).CD/; \
	  unzip $${DIR}/../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).Windows.zip; \
	  tar xzf $${DIR}/../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).hpux.tgz \
	  	FreeDoko_$(VERSION)/FreeDoko; \
	  mv FreeDoko_$(VERSION)/FreeDoko FreeDoko_$(VERSION)/FreeDoko.hpux; \
	  tar xzf $${DIR}/../FreeDoko_$(VERSION).release/FreeDoko_$(VERSION).Linux.tgz \
	  	FreeDoko_$(VERSION)/FreeDoko; \
	  ln -s FreeDoko_$(VERSION) FreeDoko;
	#   more cardsets
	cd /tmp/FreeDoko_$(VERSION).CD/FreeDoko/cardsets/; \
	  for c in origin xskat kdecarddecks pysol; do \
	    cp -a ~/Daten/FreeDoko/cardsets/$$c $$c; \
	  done
	#   more backgrounds
	cp -va data/backgrounds/* /tmp/FreeDoko_$(VERSION).CD/FreeDoko/backgrounds/
	rm /tmp/FreeDoko_$(VERSION).CD/FreeDoko/backgrounds/Makefile
	# documentation
	cd /tmp/FreeDoko_$(VERSION).CD/; \
	  cp -a FreeDoko/doc doc/;
	#   homepage
	cp -a homepage \
	  /tmp/FreeDoko_$(VERSION).CD/
	for f in /tmp/FreeDoko_$(VERSION).CD/homepage/*/download*.html; do \
	  sed "s|http://prdownloads.sourceforge.net/free-doko/|../../Installation/|g" $$f > t.out \
	  && mv t.out $$f; \
	done
	# autostarter
	for f in Autorun.inf FreeDoko.ico \
		FreeDoko_autostart.exe; do \
	  cp data/CD/cdstarter/$$f /tmp/FreeDoko_$(VERSION).CD/; \
	done
	cat data/CD/cdstarter/freedoko_autostart.config \
	  | sed "s/VERSION/$(VERSION)/" \
	  > /tmp/FreeDoko_$(VERSION).CD/freedoko_autostart.config
	# remainder
	cp /tmp/FreeDoko_$(VERSION).CD/FreeDoko/Version \
		/tmp/FreeDoko_$(VERSION).CD/
	cp data/CD/root/* /tmp/FreeDoko_$(VERSION).CD/
	# create image
	mkisofs -q -J -joliet-long -r -f \
	  -V "FreeDoko $(VERSION)" /tmp/FreeDoko_$(VERSION).CD \
	  > ../FreeDoko_$(VERSION).CD.iso
endif

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY: zip_copy
zip_copy : tgz
	-mount /media/zip
	-rm -f /media/zip/FreeDoko_*.dk.tgz
	@echo cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz /media/zip/
	@cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz /media/zip/
	@chmod a+wrX /media/zip/FreeDoko_$(VERSION)_*.dk.tgz

.PHONY: src.zip_copy
src.zip_copy : src.tgz
	-mount /media/zip
	-rm -f /media/zip/FreeDoko_*.dk.src.tgz /media/zip/FreeDoko-Bugreports_*.dk.tgz
	@echo cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz /media/zip/
	@cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz /media/zip/
	@chmod a+wrX /media/zip/FreeDoko_$(VERSION)_*.dk.src.tgz
	-tar czf /media/zip/FreeDoko-Bugreports_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz Bugreports

.PHONY: src.usb_copy
usb_copy : tgz
	-mount /media/usbstick
	-rm -f /media/usbstick/FreeDoko_*.dk.tgz
	@echo cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz /media/usbstick/
	@cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz /media/usbstick/
	@chmod a+wrX /media/usbstick/FreeDoko_$(VERSION)_*.dk.tgz

.PHONY: src.usb_copy
src.usb_copy : src.tbz
	-mount /media/usbstick
	-rm -f /media/usbstick/FreeDoko_*.dk.src.tbz /media/usbstick/FreeDoko-Bugreports_*.dk.tbz
	@echo cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tbz /media/usbstick/
	@cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tbz /media/usbstick/
	@chmod a+wrX /media/usbstick/FreeDoko_$(VERSION)_*.dk.src.tbz
	-tar cjf /media/usbstick/FreeDoko-Bugreports_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tbz Bugreports

.PHONY: src.floppy_copy
src.floppy_copy : src.tgz
	-mount /media/floppy
	-rm -f /media/floppy/FreeDoko_*.dk.src.tgz /media/floppy/FreeDoko-Bugreports_*.dk.tgz
	@echo cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz /media/floppy/
	@cp /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz /media/floppy/
	@chmod a+wrX /media/floppy/FreeDoko_$(VERSION)_*.dk.src.tgz
	-tar czf /media/floppy/FreeDoko-Bugreports_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz Bugreports

.PHONY: Windows_copy
Windows_copy : clean
	-rm -rf /mnt/Daten/FreeDoko.bak
	-mv /mnt/Daten/FreeDoko /mnt/Daten/FreeDoko.bak
	-cd -P ..;\
	cp -rL FreeDoko /mnt/Daten/
	find /mnt/Daten/FreeDoko -exec touch -r /mnt/Daten/FreeDoko.bak/src/ \{\} \;

.PHONY: backup
backup : tgz

.PHONY: tgz
tgz : clean chmod
	@echo tar czf  /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz FreeDoko
	@cd -P ..;\
	mkdir -p /Sicherung/FreeDoko/$(VERSION);\
	tar czf /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz FreeDoko \
		--exclude "FreeDoko/src/Makefile.local"
	@chmod a+r-w /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.tgz

.PHONY: src.tgz
src.tgz : clean chmod
	@echo tar czf /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz src ChangeLog Makefile*
	-@cd -P ..;\
	mkdir -p /Sicherung/FreeDoko/$(VERSION); \
	tar czf /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz \
		FreeDoko/Makefile* \
		FreeDoko/src/ \
		FreeDoko/ChangeLog \
	     --exclude "FreeDoko/src/Makefile.local" \
	     --exclude "FreeDoko/src/player/ai.reference"
	@chmod a+r-w /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tgz

.PHONY: src.tbz
src.tbz : clean chmod
	@echo tar cjf /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tbz src ChangeLog Makefile*
	-@cd -P ..;\
	mkdir -p /Sicherung/FreeDoko/$(VERSION); \
	tar cjf /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tbz \
		FreeDoko/Makefile* \
		FreeDoko/src/ \
		FreeDoko/ChangeLog \
	     --exclude "FreeDoko/src/Makefile.local" \
	     --exclude "FreeDoko/src/player/ai.reference"
	@chmod a+r-w /Sicherung/FreeDoko/$(VERSION)/FreeDoko_$(VERSION)_`date +%Y-%m-%d_%0k`.dk.src.tbz

.PHONY: flip
flip :
	flip -mb AUTHORS COPYING ChangeLog README* Version
	$(MAKE) -C src/ flip
endif
