# $Id: Makefile.bundle 24091 2012-04-04 15:55:40Z rubidium $

# This file is part of catcodec.
# catcodec 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.
# catcodec 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 catcodec. If not, see <http://www.gnu.org/licenses/>.

#
# Creation of bundles
#

# Standard make convention variables/locations
prefix         ?= /usr/local
exec_prefix    ?= $(prefix)
datarootdir    ?= $(prefix)/share
datadir        ?= $(datarootdir)

bindir         ?= $(exec_prefix)/bin
mandir         ?= $(datarootdir)/man
man1dir        ?= $(mandir)/man1
docdir         ?= $(datarootdir)/doc/$(PACKAGE_NAME)

openttddir     ?= $(datarootdir}/openttd
openttdgmdir   ?= $(openttddir)/gm/$(PACKAGE_NAME)
openttddatadir ?= $(openttddir)/data/$(PACKAGE_NAME)

# 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)" "./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 = catcodec-custom-$(REV)-$(OS)
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)/man"
	$(Q)cp "$(CATCODEC)"                      "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/COPYING"              "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/changelog.txt"        "$(BUNDLE_DIR)/"
	$(Q)cp "$(ROOT_DIR)/docs/readme.txt"      "$(BUNDLE_DIR)/docs/"
	$(Q)cp "$(ROOT_DIR)/docs/catcodec.1"      "$(BUNDLE_DIR)/man/"
	$(Q)gzip -9                               "$(BUNDLE_DIR)/man/catcodec.1"
ifeq ($(CATCODEC), catcodec.exe)
	$(Q)unix2dos "$(BUNDLE_DIR)/COPYING"
	$(Q)unix2dos "$(BUNDLE_DIR)/"*.txt
endif

install: bundle
	@echo '[INSTALL] Installing catcodec'
	$(Q)install -d "$(DESTDIR)$(bindir)"
	$(Q)install -m 755 "$(BUNDLE_DIR)/$(CATCODEC)" "$(DESTDIR)$(bindir)/"
ifndef DO_NOT_INSTALL_DOCS
	$(_C)install -d "$(DESTDIR)$(docdir)"
	$(_C)install -m 644 "$(BUNDLE_DIR)/docs/"* "$(DESTDIR)$(docdir)"
endif
ifndef DO_NOT_INSTALL_CHANGELOG
	$(_C)install -d "$(DESTDIR)$(docdir)"
	$(_C)install -m 644 "$(BUNDLE_DIR)/changelog.txt" "$(DESTDIR)$(docdir)"
endif
ifndef DO_NOT_INSTALL_LICENSE
	$(_C)install -d "$(DESTDIR)$(docdir)"
	$(_C)install -m 644 "$(BUNDLE_DIR)/COPYING" "$(DESTDIR)$(docdir)"
endif
ifndef DO_NOT_INSTALL_MAN
	$(_C)install -d "$(DESTDIR)$(man1dir)"
	$(_C)install -m 644 "$(BUNDLE_DIR)/man/"*.1.gz "$(DESTDIR)$(man1dir)/"
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_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_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)/catcodec $(REV)"
	$(Q)cp -R "$(BUNDLE_DIR)/" "$(BUNDLES_DIR)/catcodec $(REV)"
	$(Q)hdiutil create -ov -format UDZO -srcfolder "$(BUNDLES_DIR)/catcodec $(REV)" "$(BUNDLES_DIR)/$(BUNDLE_NAME).dmg"
	$(Q)rm -fr "$(BUNDLES_DIR)/catcodec $(REV)"
