#################################################
# @PACKAGE_STRING@
# @configure_input@
#################################################

@SET_MAKE@
SHELL = /bin/sh

#################################################
# Standard definitions
#################################################

prefix      ?= @prefix@
exec_prefix ?= @exec_prefix@
bindir      ?= @bindir@
datarootdir ?= @datarootdir@
datadir     ?= @datadir@
libdir      ?= @libdir@
docdir      ?= @docdir@
pdfdir      ?= @pdfdir@
mandir      ?= @mandir@
# project root-dir (directory of configure script)
top_srcdir  ?= @top_srcdir@
# project src-dir (directory of the current Makefile)
srcdir      ?= @srcdir@

# file-type suffix of executables (e.g. ".exe" in windows)
EXEEXT ?= @EXEEXT@

#################################################
# Tools
#################################################

# recursive dir creation tool (mkdir -p)
MKDIR        ?= @MKDIR_P@
RM           ?= rm -f
RM_REC       ?= $(RM) -r

# install tool
INSTALL         ?= @INSTALL@
INSTALL_DATA    ?= @INSTALL_DATA@
INSTALL_PROGRAM ?= @INSTALL_PROGRAM@

#################################################
# General package configuration
#################################################

USDX_PACKAGE_NAME := @PACKAGE_NAME@
USDX_VERSION := @PACKAGE_VERSION@
USDX_TARNAME := @PACKAGE_TARNAME@

#################################################
# USDX Paths
#################################################

USDX_SRC_DIR   := $(top_srcdir)/src
USDX_GAME_DIR  := $(top_srcdir)/game
USDX_TOOLS_DIR := $(top_srcdir)/tools
USDX_BUILD_DIR := $(top_srcdir)/build
USDX_LIB_DIR   := $(USDX_SRC_DIR)/lib

INSTALL_DATADIR := $(datadir)/$(USDX_PACKAGE_NAME)

#################################################
# Binary name
#################################################

# name of executable
USDX_BIN_NAME ?= ultrastardx$(EXEEXT)
USDX_BIN := $(USDX_GAME_DIR)/$(USDX_BIN_NAME)

#################################################
# ProjectM
#################################################

PROJECTM_CWRAPPER_DIR := $(USDX_LIB_DIR)/projectM/cwrapper
USE_PROJECTM_CWRAPPER = @USE_PROJECTM_CWRAPPER@

#################################################
# OpenCV
#################################################

OPENCV_CWRAPPER_DIR := $(USDX_LIB_DIR)/openCV3
USE_OPENCV_CWRAPPER = @USE_OPENCV_CWRAPPER@
USE_OPENCV_CWRAPPER_DLL ?= no
ifeq ($(EXEEXT),.exe)
USE_OPENCV_CWRAPPER_DLL := yes
endif
ifeq ($(PPLATFORM),darwin)
USE_OPENCV_CWRAPPER_DLL := yes
endif

#################################################
# Dependencies
#################################################

DEPS :=
ifeq ($(USE_PROJECTM_CWRAPPER), yes)
DEPS += $(PROJECTM_CWRAPPER_DIR)
endif

ifeq ($(USE_OPENCV_CWRAPPER), yes)
ifneq ($(USE_OPENCV_CWRAPPER_DLL), yes)
DEPS += $(OPENCV_CWRAPPER_DIR)
endif
endif

#################################################
# general targets
#################################################

##
# IMPORTANT:
# Always assure that this Makefile still works with the -jN 
# parameter set. This is important as Gentoo uses parallel 
# make (-j2) by default.
# If parallel execution is enabled you cannot rely on a 
# specific order the prerequisites are build. 
#
# Example:
#   target: dependency dependant
#
# will first build "dependency" and "dependant" afterwards with a
# sequential execution (-j1). With parallel execution "dependant"
# might be started before "dependency" is finished and make will
# crash.
#
# If it is not possible, add the virtual .NOTPARALLEL target.
# This will disable parallel execution for ALL targets.
##

BUILD_TARGETS = all debug release rebuild build
.PHONY: $(BUILD_TARGETS)
$(BUILD_TARGETS): all-deps
	$(MAKE) -C $(USDX_SRC_DIR) $@
	@$(MAKE) --no-print-directory whitespace-warn

.PHONY: all-deps
all-deps:
	@for dir in $(DEPS); do \
	  $(MAKE) -C "$$dir" all; \
	done

.PHONY: clean
clean: clean-src clean-deps

.PHONY: clean-src
clean-src:
	$(MAKE) -C $(USDX_SRC_DIR) clean

.PHONY: clean-deps
clean-deps:
	@for dir in $(DEPS); do \
	  $(MAKE) -C "$$dir" clean; \
	done

.PHONY: clean-game
clean-game:
	$(RM) $(USDX_GAME_DIR)/*.log
	$(RM) $(USDX_GAME_DIR)/*.db
	$(RM) $(USDX_GAME_DIR)/*.ini
	-rmdir $(USDX_GAME_DIR)/screenshots
	-rmdir $(USDX_GAME_DIR)/playlists
	-rmdir $(USDX_GAME_DIR)/songs

# just clean the game build data but no dependencies (libs, tools, ...)
.PHONY: mostlyclean
mostlyclean: clean-src

.PHONY: distclean
distclean: clean clean-game
	$(RM) config.log config.status aclocal.m4 
	$(RM_REC) autom4te.cache
	$(RM) $(USDX_SRC_DIR)/config.inc 
	$(RM) $(srcdir)/Makefile $(USDX_SRC_DIR)/Makefile $(PROJECTM_CWRAPPER_DIR)/Makefile

.PHONY: maintainer-clean
maintainer-clean: distclean

# remove temporary and backup files
.PHONY: tidy
tidy:
# FPC stuff
	find $(srcdir) -name "*.compiled" | xargs $(RM)
# Delphi stuff
	find $(srcdir) -name "__history" | xargs $(RM_REC)
	find $(srcdir) -name "*.identcache" -o -name "*.dcu" | xargs $(RM)
# Backup files
	find $(srcdir) -name "*~" -o -name "*.bak" -o -name "*.orig" | xargs $(RM)

# check for trailing whitespace in the same paths as CI
.PHONY: whitespace-check
whitespace-check:
	@base_ref="$${BASE_REF:-origin/master}"; \
	if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then \
	  echo "error: whitespace-check must run inside a git checkout"; \
	  exit 1; \
	fi; \
	if ! git rev-parse --verify "$$base_ref" >/dev/null 2>&1; then \
	  echo "error: git ref '$$base_ref' not found (set BASE_REF=... if needed)"; \
	  exit 1; \
	fi; \
	base_sha=`git merge-base HEAD "$$base_ref"`; \
	git --no-pager -c core.whitespace=blank-at-eol diff --no-ext-diff --color --check "$$base_sha" -- './src' ':!./src/lib' ':!./src/webSDK'

# warning-only variant for local builds
.PHONY: whitespace-warn
whitespace-warn:
	@$(MAKE) --no-print-directory whitespace-check || { \
	  echo "warning: whitespace issues detected (run 'make whitespace-check' for details)"; \
	  true; \
	}


#################################################
# auto-update
#################################################

Makefile: $(srcdir)/Makefile.in $(USDX_SRC_DIR)/Makefile.in $(USDX_SRC_DIR)/config.inc.in $(PROJECTM_CWRAPPER_DIR)/Makefile.in config.status
	@echo "-----------------------------------"
	@echo "Performing reconfiguration..."
	./config.status
	@echo "-----------------------------------"

config.status: configure
	./config.status --recheck

.PHONY: reconf
reconf:
	./autogen.sh

#################################################
# install/uninstall
#################################################

##
# For information on directory and install conventions see
#   "info autoconf", Section 4.8.2. Installation Directory Variables
#                    Section 4.8.1, 4.8.3 and 4.8.4 
# Notes:
# - "make install" must not rebuild, so do not depend on all.
# - use the DESTDIR variable as prefix for installation directories, 
#   otherwise Gentoo will not be able to install as it uses a sandbox.
##

.PHONY: install
install: install-all
	@echo "--------------------------------"
	@echo "$(USDX_PACKAGE_NAME) installed."
	@echo "Start with: $(bindir)/$(USDX_BIN_NAME)"
	@echo "--------------------------------"

# strip binaries during install
install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' \
                install

.PHONY: install-all
install-all: install-exec install-plugins install-data

.PHONY: install-exec
install-exec:
	$(MKDIR) "$(DESTDIR)$(bindir)"
	$(INSTALL_PROGRAM) "$(USDX_BIN)" "$(DESTDIR)$(bindir)"

# FPC target platform and processor
PPLATFORM  := @FPC_PLATFORM@
PPROCESSOR := @FPC_PROCESSOR@

.PHONY: install-plugins
install-plugins:
	$(MKDIR) "$(DESTDIR)$(INSTALL_DATADIR)/plugins"
	$(INSTALL) "$(USDX_GAME_DIR)/plugins"/* "$(DESTDIR)$(INSTALL_DATADIR)/plugins"

.PHONY: install-data
install-data:
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/avatars" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/avatars" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/covers" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/covers" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/fonts" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/fonts" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/languages" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/languages" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/resources" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/resources" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/sounds" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/sounds" \
            install-data-recursive
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/themes" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/themes" \
            install-data-recursive
	if [ "@DEFINE_USE_LOCAL_PROJECTM_PRESETS@" = DEFINE ] ; then \
	    $(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/visuals" \
	        RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/visuals" \
	        install-data-recursive ; \
	fi
	$(MAKE) RECURSIVE_SRC_DIR="$(USDX_GAME_DIR)/webs" \
            RECURSIVE_DST_DIR="$(DESTDIR)$(INSTALL_DATADIR)/themes" \
            install-data-recursive
	$(INSTALL_DATA) "LICENSE" "$(DESTDIR)$(INSTALL_DATADIR)/LICENSE"

.PHONY: install-data-recursive
install-data-recursive:
# Note: the project contains filesnames with whitespace 
	$(MKDIR) "$(RECURSIVE_DST_DIR)"
	@for file in "$(RECURSIVE_SRC_DIR)"/*; do \
	  if test -f "$$file"; then \
	    filename=`basename "$$file"`; \
	    $(INSTALL_DATA) "$$file" "$(RECURSIVE_DST_DIR)/$$filename" || exit 1; \
	  fi; \
	  if test -d "$$file"; then \
	    subdir="$$file"; \
	    subdirname=`basename "$$subdir"`; \
	    $(MAKE) RECURSIVE_SRC_DIR="$$subdir" \
	            RECURSIVE_DST_DIR="$(RECURSIVE_DST_DIR)/$$subdirname" \
	            install-data-recursive || exit 1; \
	  fi; \
	done

.PHONY: uninstall
uninstall: uninstall-all

.PHONY: uninstall-all
uninstall-all: uninstall-data uninstall-exec uninstall-plugins

.PHONY: uninstall-data
uninstall-data:
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/avatars"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/covers"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/fonts"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/languages"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/resources"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/soundfonts"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/sounds"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/themes"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/visuals"
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/webs"
	$(RM)     "$(DESTDIR)$(INSTALL_DATADIR)/LICENSE"
	-rmdir "$(DESTDIR)$(INSTALL_DATADIR)"

.PHONY: uninstall-exec
uninstall-exec:
	$(RM) "$(DESTDIR)$(bindir)/$(USDX_BIN_NAME)"

.PHONY: uninstall-plugins
uninstall-plugins:
	$(RM_REC) "$(DESTDIR)$(INSTALL_DATADIR)/plugins"

#################################################
# Distributable source-package (TODO)
#################################################

#disttmpdir := $(USDX_PACKAGE_NAME)-$(USDX_VERSION)-src
disttmpdir := $(USDX_PACKAGE_NAME)-1.1_alpha-src
# choose all files in SVN that are not deleted
svn-files := svn status -v | grep -v "^[?D]" | cut -c8- | tr -s " " | cut -f5- -d" "

.PHONY: dist
dist:
	@$(svn-files) | while read FILE; do \
	  if test -d "$$FILE"; then \
	    echo "MKDIR: $(disttmpdir)/$$FILE"; \
	    $(MKDIR) "$(disttmpdir)/$$FILE" || exit 1; \
	  else \
	    echo "COPY:  $$FILE"; \
	    cp "$$FILE" "$(disttmpdir)/$$FILE" || exit 1; \
	  fi; \
	done
	tar cvf $(disttmpdir).tar $(disttmpdir)
	gzip $(disttmpdir).tar
	$(RM_REC) $(disttmpdir)

#################################################
# Debian package
#################################################

debpkgdir    ?= dists/debian
debpkgtmpdir := $(debpkgdir)/deb-package
debpkgprefix := $(USDX_PACKAGE_NAME)
debpkgname   := $(debpkgprefix)_$(USDX_VERSION)_$(PPROCESSOR).deb

.PHONY: deb
deb: all
	$(RM_REC) $(debpkgtmpdir)

	$(MKDIR) $(debpkgdir)
	$(MKDIR) $(debpkgtmpdir)/DEBIAN

	$(MAKE) DESTDIR=$(debpkgtmpdir)/ install-all

	$(INSTALL_DATA) $(debpkgdir)/$(debpkgprefix).control $(debpkgtmpdir)/DEBIAN/control

	dpkg-deb --build $(debpkgtmpdir)
	mv $(debpkgtmpdir)/../deb-package.deb $(debpkgdir)/$(debpkgname)

	$(RM_REC) $(debpkgtmpdir)

#################################################
# RPM (TODO)
#################################################

.PHONY: rpm
rpm: all
	@echo "Coming soon"


#################################################
# macOS defines
#################################################

# otool: macOS object file displaying tool
OTOOL := /usr/bin/otool 
# install_name_tool: macOS tool to change dynamic shared library install names
INSTALL_NAME_TOOL := /usr/bin/install_name_tool
# hdiutil: macOS disk image tool
HDIUTIL := /usr/bin/hdiutil
# pkgingLibDir := path for libraries installed using packaging system
pkgingLibDir := @MACOS_PACKAGING_LIBDIR@

#################################################
# macOS app-bundle
#################################################

macos_bundle_path := UltraStarDeluxe.app/Contents
.PHONY: macos-app
macos-app: all 
# Create double clickable macOS application.

	@echo ""
	@echo "Creating the macOS application"
	@echo ""

	$(MKDIR) $(macos_bundle_path)

# Put the icon file into its particular place. 
# Must be done BEFORE info.plist is created.
	$(MKDIR) $(macos_bundle_path)/resources
	$(INSTALL_DATA) icons/ultrastardx.icns $(macos_bundle_path)/resources/

# the info.plist file
	$(INSTALL_DATA) $(USDX_SRC_DIR)/macos/Info.plist $(macos_bundle_path)/

# Copy the resources. 
	$(MAKE) install-all INSTALL_DATADIR=$(macos_bundle_path) bindir=$(macos_bundle_path)/MacOS

# Copy the lua plugins. 
	$(MAKE) install-plugins INSTALL_DATADIR=$(macos_bundle_path) bindir=$(macos_bundle_path)/MacOS

# Create the song directory. 
	$(MKDIR) $(macos_bundle_path)/songs

# final messages
	@echo ""
	@echo "macOS application created (UltraStarDeluxe.app)."
	@echo "Using @MACOS_PACKAGING@ with libdir @MACOS_PACKAGING_LIBDIR@"
	@echo "Please report issues to the developer team, preferably on Discord (https://discord.gg/qfS2b7VwDG)."
	@echo "Have fun."
	@echo ""

# export some variables used by Makefile.macos-helper
export USDX_BIN
export pkgingLibDir
export macos_bundle_path
export INSTALL
export INSTALL_NAME_TOOL
export OTOOL

.PHONY: macos-standalone-app
macos-standalone-app: macos-app 
# Create double clickable standalone macOS application.

	@echo ""
	@echo "Creating the standalone macOS application"
	@echo ""

	@rm -f $(macos_bundle_path)/MacOS/$(USDX_BIN_NAME)
	@$(MAKE) -f Makefile.macos-helper

# do  not forget to sign the code
#	codesign -f -s USDX-codesign UltraStarDeluxe.app

# final messages
	@echo ""
	@echo "Standalone macOS application created."
	@echo ""

.PHONY: macos-dmg
macos-dmg: macos-standalone-app
	@if test -d "/Volumes/UltraStarDeluxe"; then\
		echo "Error: there is already a disk mounted on /Volumes/UltraStarDeluxe";\
		exit 1;\
	fi
	$(RM) UltraStarDeluxe.dmg
	$(RM) UltraStarDeluxe.sparseimage
	$(HDIUTIL) create -type SPARSE -size 1g -fs HFS+ -volname UltraStarDeluxe -ov UltraStarDeluxe.sparseimage
	$(HDIUTIL) attach UltraStarDeluxe.sparseimage
	/bin/ln -s /Applications /Volumes/UltraStarDeluxe/Applications
	/bin/cp -R UltraStarDeluxe.app /Volumes/UltraStarDeluxe
	$(MKDIR) /Volumes/UltraStarDeluxe/Licenses
	find $(USDX_GAME_DIR) -name 'LICENSE.*' -maxdepth 1 -exec cp {} /Volumes/UltraStarDeluxe/Licenses \;
#	/bin/cp ultrastardx/icons/UltraStarDeluxeVolumeIcon.icns /Volumes/UltraStarDeluxe/.VolumeIcon.icns
#	/Developer/Tools/SetFile -a C /Volumes/UltraStarDeluxe/.VolumeIcon.icns /Volumes/UltraStarDeluxe
	codesign --force --deep --sign - /Volumes/UltraStarDeluxe/UltraStarDeluxe.app
	$(HDIUTIL) detach /Volumes/UltraStarDeluxe
	$(HDIUTIL) convert UltraStarDeluxe.sparseimage -format UDBZ -o UltraStarDeluxe.dmg
	$(RM) UltraStarDeluxe.sparseimage

	@echo ""
	@echo "DMG Disk image created (UltraStarDeluxe.dmg)."
	@echo ""

# remove macOS apllication bundle and disk image
.PHONY: clean-macos
clean-macos: clean-macos-app clean-macos-dmg

.PHONY: clean-macos-app
clean-macos-app:
	$(RM_REC) UltraStarDeluxe.app 

.PHONY: clean-macos-dmg
clean-macos-dmg:
	$(RM) UltraStarDeluxe.dmg 
	$(RM) UltraStarDeluxe.sparseimage
