# $Id$

# This file is part of OpenTTD.
# OpenTTD 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, version 2.
# OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.

#
# Creation of bundles
#

# The revision is needed for the bundle name and creating an OSX application bundle.
ifdef REVISION
REV := $(REVISION)
else
# Detect the revision
VERSIONS := $(shell AWK="$(AWK)" "$(ROOT_DIR)/findversion.sh")
REV      := $(shell echo "$(VERSIONS)" | cut -f 1 -d'	')
endif

# Make sure we have something in REV
ifeq ($(REV),)
REV := norev000
endif

ifndef BUNDLE_NAME
BUNDLE_NAME = openttd-custom-$(REV)-$(OS)
endif

# An OSX application bundle needs the data files, lang files and openttd executable in a different location.
ifdef OSXAPP
AI_DIR   = $(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources/ai
DATA_DIR = $(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources/data
LANG_DIR = $(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources/lang
GM_DIR   = $(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources/gm
TTD_DIR  = $(BUNDLE_DIR)/$(OSXAPP)/Contents/MacOS
else
AI_DIR   = $(BUNDLE_DIR)/ai
DATA_DIR = $(BUNDLE_DIR)/data
LANG_DIR = $(BUNDLE_DIR)/lang
GM_DIR   = $(BUNDLE_DIR)/gm
TTD_DIR  = $(BUNDLE_DIR)
endif

bundle: all
	@echo '[BUNDLE] Constructing bundle'
	$(Q)rm -rf   "$(BUNDLE_DIR)"
	$(Q)mkdir -p "$(BUNDLE_DIR)"
	$(Q)mkdir -p "$(BUNDLE_DIR)/docs"
	$(Q)mkdir -p "$(BUNDLE_DIR)/media"
	$(Q)mkdir -p "$(BUNDLE_DIR)/scripts"
	$(Q)mkdir -p "$(TTD_DIR)"
	$(Q)mkdir -p "$(AI_DIR)"
	$(Q)mkdir -p "$(DATA_DIR)"
	$(Q)mkdir -p "$(LANG_DIR)"
	$(Q)mkdir -p "$(GM_DIR)"
ifdef OSXAPP
	$(Q)mkdir -p "$(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources"
	$(Q)echo "APPL????" >                                          "$(BUNDLE_DIR)/$(OSXAPP)/Contents/PkgInfo"
	$(Q)cp    "$(ROOT_DIR)/os/macosx/openttd.icns"                 "$(BUNDLE_DIR)/$(OSXAPP)/Contents/Resources/openttd.icns"
	$(Q)$(ROOT_DIR)/os/macosx/plistgen.sh                          "$(BUNDLE_DIR)/$(OSXAPP)" "$(REV)"
	$(Q)cp    "$(ROOT_DIR)/os/macosx/splash.png"                   "$(DATA_DIR)"
endif
	$(Q)cp "$(BIN_DIR)/$(TTD)"                "$(TTD_DIR)/"
	$(Q)cp "$(BIN_DIR)/ai/"compat_*.nut       "$(AI_DIR)/"
	$(Q)cp "$(BIN_DIR)/data/"*.grf            "$(DATA_DIR)/"
	$(Q)cp "$(BIN_DIR)/data/"*.obg            "$(DATA_DIR)/"
	$(Q)cp "$(BIN_DIR)/data/"*.obs            "$(DATA_DIR)/"
	$(Q)cp "$(BIN_DIR)/data/opntitle.dat"     "$(DATA_DIR)/"
	$(Q)cp "$(BIN_DIR)/lang/"*.lng            "$(LANG_DIR)/"
	$(Q)cp "$(BIN_DIR)/gm/"*.obm              "$(GM_DIR)/"
	$(Q)cp "$(ROOT_DIR)/readme.txt"           "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/COPYING"              "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/known-bugs.txt"       "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/docs/obg_format.txt"  "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/docs/obm_format.txt"  "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/docs/obs_format.txt"  "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/docs/multiplayer.txt" "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/docs/32bpp.txt"       "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/changelog.txt"        "$(BUNDLE_DIR)/"
ifdef MAN_DIR
	$(Q)mkdir -p "$(BUNDLE_DIR)/man/"
	$(Q)cp "$(ROOT_DIR)/docs/openttd.6"       "$(BUNDLE_DIR)/man/"
	$(Q)gzip -9 "$(BUNDLE_DIR)/man/openttd.6"
endif
	$(Q)cp "$(ROOT_DIR)/media/openttd.32.xpm" "$(BUNDLE_DIR)/media/"
	$(Q)cp "$(ROOT_DIR)/media/openttd."*.png  "$(BUNDLE_DIR)/media/"
	$(Q)cp "$(BIN_DIR)/scripts/"*             "$(BUNDLE_DIR)/scripts/"
ifdef MENU_DIR
	$(Q)cp "$(ROOT_DIR)/media/openttd.desktop" "$(BUNDLE_DIR)/media/"
	$(Q)cat "$(ROOT_DIR)/media/openttd.desktop" | sed s/=openttd/=$(BINARY_NAME)/g > "$(ROOT_DIR)/media/openttd.desktop.install"
endif
ifeq ($(TTD), openttd.exe)
	$(Q)unix2dos "$(BUNDLE_DIR)/docs/"* "$(BUNDLE_DIR)/readme.txt" "$(BUNDLE_DIR)/COPYING" "$(BUNDLE_DIR)/changelog.txt" "$(BUNDLE_DIR)/known-bugs.txt"
ifeq ($(OS), DOS)
	$(Q)cp "$(ROOT_DIR)/os/dos/cwsdpmi.txt"   "$(BUNDLE_DIR)/docs/"
ifndef STRIP
	$(Q)cp "$(ROOT_DIR)/os/dos/cwsdpmi.exe"   "$(TTD_DIR)/"
endif
endif
endif

### Packing the current bundle into several compressed file formats ###
#
# Zips & dmgs do not contain a root folder, i.e. they have files in the root of the zip/dmg.
# gzip, bzip2 and lha archives have a root folder, with the same name as the bundle.
#
# One can supply a custom name by adding BUNDLE_NAME:=<name> to the make command.
#
bundle_zip: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).zip'
	$(Q)mkdir -p "$(BUNDLES_DIR)"
	$(Q)cd "$(BUNDLE_DIR)" && zip -r $(shell if test -z "$(VERBOSE)"; then echo '-q'; fi) "$(BUNDLES_DIR)/$(BUNDLE_NAME).zip" .

bundle_7z: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).7z'
	$(Q)mkdir -p "$(BUNDLES_DIR)"
	$(Q)cd "$(BUNDLE_DIR)" && 7z a "$(BUNDLES_DIR)/$(BUNDLE_NAME).7z" .

bundle_gzip: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).tar.gz'
	$(Q)mkdir -p "$(BUNDLES_DIR)/.gzip/$(BUNDLE_NAME)"
	$(Q)cp -R    "$(BUNDLE_DIR)/"* "$(BUNDLES_DIR)/.gzip/$(BUNDLE_NAME)/"
	$(Q)cd "$(BUNDLES_DIR)/.gzip" && tar -zc$(shell if test -n "$(VERBOSE)"; then echo 'v'; fi)f "$(BUNDLES_DIR)/$(BUNDLE_NAME).tar.gz" "$(BUNDLE_NAME)"
	$(Q)rm -rf   "$(BUNDLES_DIR)/.gzip"

bundle_bzip2: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).tar.bz2'
	$(Q)mkdir -p "$(BUNDLES_DIR)/.bzip2/$(BUNDLE_NAME)"
	$(Q)cp -R    "$(BUNDLE_DIR)/"* "$(BUNDLES_DIR)/.bzip2/$(BUNDLE_NAME)/"
	$(Q)cd "$(BUNDLES_DIR)/.bzip2" && tar -jc$(shell if test -n "$(VERBOSE)"; then echo 'v'; fi)f "$(BUNDLES_DIR)/$(BUNDLE_NAME).tar.bz2" "$(BUNDLE_NAME)"
	$(Q)rm -rf   "$(BUNDLES_DIR)/.bzip2"

bundle_lzma: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).tar.lzma'
	$(Q)mkdir -p "$(BUNDLES_DIR)/.lzma/$(BUNDLE_NAME)"
	$(Q)cp -R    "$(BUNDLE_DIR)/"* "$(BUNDLES_DIR)/.lzma/$(BUNDLE_NAME)/"
	$(Q)cd "$(BUNDLES_DIR)/.lzma" && tar --lzma -c$(shell if test -n "$(VERBOSE)"; then echo 'v'; fi)f "$(BUNDLES_DIR)/$(BUNDLE_NAME).tar.lzma" "$(BUNDLE_NAME)"
	$(Q)rm -rf   "$(BUNDLES_DIR)/.lzma"

bundle_lha: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).lha'
	$(Q)mkdir -p "$(BUNDLES_DIR)/.lha/$(BUNDLE_NAME)"
	$(Q)cp -R    "$(BUNDLE_DIR)/"* "$(BUNDLES_DIR)/.lha/$(BUNDLE_NAME)/"
	$(Q)cd "$(BUNDLES_DIR)/.lha" && lha ao6 "$(BUNDLES_DIR)/$(BUNDLE_NAME).lha" "$(BUNDLE_NAME)"
	$(Q)rm -rf   "$(BUNDLES_DIR)/.lha"

bundle_dmg: bundle
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).dmg'
	$(Q)mkdir -p "$(BUNDLES_DIR)/OpenTTD $(REV)"
	$(Q)cp -R "$(BUNDLE_DIR)/" "$(BUNDLES_DIR)/OpenTTD $(REV)"
	$(Q)hdiutil create -ov -format UDZO -srcfolder "$(BUNDLES_DIR)/OpenTTD $(REV)" "$(BUNDLES_DIR)/$(BUNDLE_NAME).dmg"
	$(Q)rm -fr "$(BUNDLES_DIR)/OpenTTD $(REV)"

bundle_exe: all
	@echo '[BUNDLE] Creating $(BUNDLE_NAME).exe'
	$(Q)mkdir -p "$(BUNDLES_DIR)"
	$(Q)unix2dos "$(ROOT_DIR)/docs/"* "$(ROOT_DIR)/readme.txt" "$(ROOT_DIR)/COPYING" "$(ROOT_DIR)/changelog.txt" "$(ROOT_DIR)/known-bugs.txt"
	$(Q)cd $(ROOT_DIR)/os/windows/installer && makensis.exe //DVERSION_INCLUDE=version_$(PLATFORM).txt install.nsi
	$(Q)mv $(ROOT_DIR)/os/windows/installer/*$(PLATFORM).exe "$(BUNDLES_DIR)/$(BUNDLE_NAME).exe"

ifdef OSXAPP
install:
	@echo '[INSTALL] Cannot install the OSX Application Bundle'
else
install: bundle
	@echo '[INSTALL] Installing OpenTTD'
	$(Q)install -d "$(INSTALL_BINARY_DIR)"
	$(Q)install -d "$(INSTALL_ICON_DIR)"
	$(Q)install -d "$(INSTALL_DATA_DIR)/gm"
	$(Q)install -d "$(INSTALL_DATA_DIR)/ai"
	$(Q)install -d "$(INSTALL_DATA_DIR)/data"
	$(Q)install -d "$(INSTALL_DATA_DIR)/lang"
	$(Q)install -d "$(INSTALL_DATA_DIR)/gm"
	$(Q)install -d "$(INSTALL_DATA_DIR)/scripts"
	$(Q)install -d "$(INSTALL_DOC_DIR)"
ifeq ($(TTD), openttd.exe)
	$(Q)install -m 755 "$(BUNDLE_DIR)/$(TTD)" "$(INSTALL_BINARY_DIR)/${BINARY_NAME}.exe"
else
	$(Q)install -m 755 "$(BUNDLE_DIR)/$(TTD)" "$(INSTALL_BINARY_DIR)/${BINARY_NAME}"
endif
	$(Q)install -m 644 "$(BUNDLE_DIR)/lang/"* "$(INSTALL_DATA_DIR)/lang"
	$(Q)install -m 644 "$(BUNDLE_DIR)/ai/"* "$(INSTALL_DATA_DIR)/ai"
	$(Q)install -m 644 "$(BUNDLE_DIR)/data/"* "$(INSTALL_DATA_DIR)/data"
	$(Q)install -m 644 "$(BUNDLE_DIR)/gm/"* "$(INSTALL_DATA_DIR)/gm"
	$(Q)install -m 644 "$(BUNDLE_DIR)/scripts/"* "$(INSTALL_DATA_DIR)/scripts"
	$(Q)install -m 644 "$(BUNDLE_DIR)/docs/"* "$(INSTALL_DOC_DIR)"
	$(Q)install -m 644 "$(BUNDLE_DIR)/"*.txt "$(INSTALL_DOC_DIR)"
	$(Q)install -m 644 "$(BUNDLE_DIR)/COPYING" "$(INSTALL_DOC_DIR)"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.32.xpm" "$(INSTALL_ICON_DIR)/${BINARY_NAME}.32.xpm"
ifdef ICON_THEME_DIR
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/16x16/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.16.png" "$(INSTALL_ICON_THEME_DIR)/16x16/apps/${BINARY_NAME}.png"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/32x32/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.32.png" "$(INSTALL_ICON_THEME_DIR)/32x32/apps/${BINARY_NAME}.png"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/48x48/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.48.png" "$(INSTALL_ICON_THEME_DIR)/48x48/apps/${BINARY_NAME}.png"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/64x64/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.64.png" "$(INSTALL_ICON_THEME_DIR)/64x64/apps/${BINARY_NAME}.png"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/128x128/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.128.png" "$(INSTALL_ICON_THEME_DIR)/128x128/apps/${BINARY_NAME}.png"
	$(Q)install -d "$(INSTALL_ICON_THEME_DIR)/256x256/apps"
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/openttd.256.png" "$(INSTALL_ICON_THEME_DIR)/256x256/apps/${BINARY_NAME}.png"
else
	$(Q)install -m 644 "$(BUNDLE_DIR)/media/"*.png "$(INSTALL_ICON_DIR)"
endif
ifdef MAN_DIR
	$(Q)install -d "$(INSTALL_MAN_DIR)"
	$(Q)install -m 644 "$(BUNDLE_DIR)/man/openttd.6.gz" "$(INSTALL_MAN_DIR)/${BINARY_NAME}.6.gz"
endif
ifdef MENU_DIR
	$(Q)install -d "$(INSTALL_MENU_DIR)"
	$(Q)install -m 644 "$(ROOT_DIR)/media/openttd.desktop.install" "$(INSTALL_MENU_DIR)/${BINARY_NAME}.desktop"
endif
endif # OSXAPP