ACLOCAL_AMFLAGS = -Im4

AUTOMAKE_OPTIONS = gnu

INSTALL_DIR = /usr/bin/install -d -m 755
OTOOL = /usr/bin/otool
HDIUTIL = /usr/bin/hdiutil
OSX_BUNDLE_NAME = Valknut
OSX_APP_DIR = $(OSX_BUNDLE_NAME).app
ICONS_DIR = $(PACKAGE)/icons
OSX_ICONS_TARGET = $(OSX_APP_DIR)/icons
OSX_CNT_TARGET = $(OSX_APP_DIR)/Contents
OSX_MACOS_TARGET = $(OSX_CNT_TARGET)/MacOS
OSX_RSC_TARGET = $(OSX_CNT_TARGET)/Resources
OSX_DMG_TARGET = "$(OSX_BUNDLE_NAME) $(PACKAGE_VERSION)"

SUBDIRS = valknut

install-data-local:
	$(mkinstalldirs) $(DESTDIR)$(pkgdatadir)
	$(INSTALL_DATA) $(srcdir)/COPYING $(DESTDIR)$(pkgdatadir)/
	$(mkinstalldirs) $(DESTDIR)$(datadir)/applications
	$(INSTALL_DATA) $(srcdir)/valknut.desktop $(DESTDIR)$(datadir)/applications/

define install_osx_libraries
$(shell $(INSTALL) -m 755 $(dylib) $(OSX_MACOS_TARGET))
$(shell /usr/bin/install_name_tool -change $(dylib) \@executable_path/$(notdir $(dylib)) $(OSX_MACOS_TARGET)/$(PACKAGE))
$(shell /usr/bin/install_name_tool -id \@executable_path/$(notdir $(dylib)) $(OSX_MACOS_TARGET)/$(notdir $(dylib)))
$(foreach linked_dylibs_2,$(shell $(OTOOL) -L $(dylib) | grep version | cut -f 1 -d ' ' | grep -v \/System\/Library | grep -v \usr\/lib | grep -v executable_path),$(rename_secondary_osx_libraries))
endef

define rename_secondary_osx_libraries
$(shell /usr/bin/install_name_tool -change $(linked_dylibs_2) \@executable_path/$(notdir $(linked_dylibs_2)) $(OSX_MACOS_TARGET)/$(notdir $(dylib)))
endef

install-osx: install-osx-app-bundle install-osx-standalone-app install-osx-disk-image

install-osx-app-bundle:
	/bin/rm -rf "$(abs_builddir)/$(OSX_APP_DIR)"
	$(MAKE) mandir="$(abs_builddir)/$(OSX_APP_DIR)/man" datadir="$(abs_builddir)/$(OSX_APP_DIR)" pkgdatadir="$(abs_builddir)/$(OSX_APP_DIR)" bindir="$(abs_builddir)/$(OSX_MACOS_TARGET)" install-strip
	$(INSTALL_DIR) $(OSX_CNT_TARGET)
	$(INSTALL_DATA) Info.plist $(OSX_CNT_TARGET)/Info.plist
	$(INSTALL_DATA) PkgInfo.in $(OSX_CNT_TARGET)/PkgInfo
	$(INSTALL_DIR) $(OSX_MACOS_TARGET)
	$(INSTALL) -s -m 755 $(PACKAGE)/$(PACKAGE) $(OSX_MACOS_TARGET)/$(PACKAGE)
	$(INSTALL_DIR) $(OSX_RSC_TARGET)
	$(INSTALL_DATA) $(ICONS_DIR)/icon_128x128.icns $(OSX_RSC_TARGET)/$(OSX_BUNDLE_NAME).icns

install-osx-standalone-app: install-osx-app-bundle
	$(foreach dylib,$(shell $(OTOOL) -L $(OSX_MACOS_TARGET)/$(PACKAGE) | grep version | cut -f 1 -d ' ' | grep -v \/System\/Library | grep -v \/usr\/lib),$(install_osx_libraries))

install-osx-disk-image: install-osx-app-bundle install-osx-standalone-app
	/bin/rm -f $(PACKAGE)-$(PACKAGE_VERSION).sparseimage
	/bin/rm -f $(PACKAGE)-$(PACKAGE_VERSION)-`uname -p`.dmg
	$(HDIUTIL) create -type SPARSE -size 50m -fs HFS+ -volname $(OSX_DMG_TARGET) -attach $(PACKAGE)-$(PACKAGE_VERSION).sparseimage
	/bin/cp -R $(OSX_APP_DIR) /Volumes/$(OSX_DMG_TARGET)
	/bin/cp $(PACKAGE)/icons/$(OSX_BUNDLE_NAME)VolumeIcon.icns /Volumes/$(OSX_DMG_TARGET)/.VolumeIcon.icns
	/Developer/Tools/SetFile -a C /Volumes/$(OSX_DMG_TARGET)/.VolumeIcon.icns /Volumes/$(OSX_DMG_TARGET)
	$(HDIUTIL) detach /Volumes/$(OSX_DMG_TARGET)
	$(HDIUTIL) convert -imagekey zlib-level=9 -format UDZO $(PACKAGE)-$(PACKAGE_VERSION).sparseimage -o $(PACKAGE)-$(PACKAGE_VERSION)-`uname -p`.dmg
	/bin/rm -f $(PACKAGE)-$(PACKAGE_VERSION).sparseimage
	@echo ""
	@echo "Done building a distributable disk image for $(OSX_BUNDLE_NAME) $(PACKAGE_VERSION)."
	@echo "The disk image can be found at the root of the"
	@echo "build directory `pwd`"
	@echo "as '$(PACKAGE)-$(PACKAGE_VERSION)-`uname -p`.dmg'."

clean-osx:
	/bin/rm -rf *.dmg *.sparseimage Valknut.app
