# NetHack 3.7  macOS.370 $NHDT-Date: 1693359574 2023/08/30 01:39:34 $  $NHDT-Branch: keni-crashweb2 $:$NHDT-Revision: 1.138 $
# Copyright (c) Kenneth Lorber, Kensington, Maryland, 2015.
# NetHack may be freely redistributed.  See license for details.
#
#---------------------------------------------------------------------
# MacOS hints file with support for multiple window ports (interfaces)
# Tested on:
#     - MacOS Catalina 10.15
#     - MacOS Ventura 13 (Intel processor)
#
# If this doesn't work for some other version of macOS, consider
# making a new hints file it, rather than changing this one.
# And let us know about it.
# Useful info: http://www.opensource.apple.com/darwinsource/index.html

#-PRE xxxx
# macOS hints file
#

# note: '#-INCLUDE' is not just a comment; multiw-1 contains sections 1 to 2
#-INCLUDE multiw-1.370

ifneq "$(USEMACPORTS)" "1"
HAVE_HOMEBREW := $(shell expr `which -s brew; echo $$?` = 0)
endif
ifneq "$(USEHOMEBREW)" "1"
HAVE_MACPORTS := $(shell expr `which -s port; echo $$?` = 0)
endif

ifndef LIBXPM
LIBXPM= -L/opt/X11/lib -lXpm
endif

# 4. Other

#-----------------------------------------------------------------------------
# You shouldn't need to change anything below here (in the hints file; if
# you're reading this in Makefile augmented by hints, that may not be true).
#

#-INCLUDE multiw-2.370

# compiler.370 contains compiler detection and adjustments common
# to both linux and macOS

#-INCLUDE compiler.370

ifdef WANT_WIN_QT
ifeq "$(HAVE_HOMEBREW)" "1"
ifdef WANT_WIN_QT5
# Qt5 installed via homebrew
QTDIR :=$(shell brew --prefix)/opt/qt@5
endif   # WANT_WIN_QT5
ifdef WANT_WIN_QT6
QTDIR :=$(shell brew --prefix)/opt/qt@6
endif   # WANT_WIN_QT6
endif   # HAVE_HOMEBREW
ifndef QTDIR
ifeq "$(HAVE_MACPORTS)" "1"
# Qt installed via macports
ifdef WANT_WIN_QT5
QTDIR=/opt/local/libexec/qt5
endif   # WANT_WIN_QT5
ifdef WANT_WIN_QT6
QTDIR=/opt/local/libexec/qt6
endif   # WANT_WIN_QT6
endif   # HAVE_MACPORTS
endif   # QTDIR
endif   # WANT_WIN_QT

# multiw-3.370 must come after compiler.370
# and after QTDIR is defined

#-INCLUDE multiw-3.370

ifeq "$(USE_ASAN)" "1"
CFLAGS +=-fsanitize=address
LFLAGS +=-fsanitize=address
endif

# NetHack sources control
NHCFLAGS+=-DDLB
NHCFLAGS+=-DHACKDIR=\"$(HACKDIR)\"
NHCFLAGS+=-DDEFAULT_WINDOW_SYS=\"$(WANT_DEFAULT)\"
NHCFLAGS+=-DSYSCF -DSYSCF_FILE=\"$(HACKDIR)/sysconf\" -DSECURE
#NHCFLAGS+=-DTIMED_DELAY
#NHCFLAGS+=-DDUMPLOG
#NHCFLAGS+=-DCONFIG_ERROR_SECURE=FALSE
NHCFLAGS+=-DGREPPATH=\"/usr/bin/grep\"
#NHCFLAGS+=-DCOMPRESS=\"/bin/gzip\" -DCOMPRESS_EXTENSION=\".gz\"
NHCFLAGS+=-DNOMAIL
#NHCFLAGS+=-DEXTRA_SANITY_CHECKS
#NHCFLAGS+=-DEDIT_GETLIN
#NHCFLAGS+=-DSCORE_ON_BOTL
#NHCFLAGS+=-DMSGHANDLER
#NHCFLAGS+=-DTTY_TILES_ESCCODES
#NHCFLAGS+=-DTTY_SOUND_ESCCODES
#NHCFLAGS+=-DNO_CHRONICLE
#NHCFLAGS+=-DLIVELOG
# not NHCFLAGS - needed for makedefs
CFLAGS+=-DCRASHREPORT=\"NetHackCrashReport.JavaScript\"

ifdef MAKEFILE_SRC
# default
CURSESLIB = -lncurses
ifeq "$(CURSES_UNICODE)" "sys"
NHCFLAGS+=-DCURSES_UNICODE
WINLIB += $(CURSESLIB)
else
ifeq "$(CURSES_UNICODE)" "0"
WINLIB += $(CURSESLIB)
else
ifeq "$(USE_CURSESLIB)" "1"
NHCFLAGS+=-DCURSES_UNICODE
else
# If CURSES_UNICODE is defined, we need ncursesw.
# Without CURSES_UNICODE the following simpler setting works.
# CURSESLIB = -lncurses -ltinfo
comma:=,
NCURSES_LFLAGS = $(shell pkg-config ncursesw --libs)
ifeq (,$(findstring ncursesw, $(NCURSES_LFLAGS)))
ifeq (,$(findstring ncurses, $(NCURSES_LFLAGS)))
#this indicates that pkg-config itself was unavailable
CURSESLIB = $(NCURSES_LFLAGS)
NHCFLAGS+=-DCURSES_UNICODE
endif
endif
#$(info $(NCURSES_LFLAGS))
ifeq (,$(findstring ncursesw, $(NCURSES_LFLAGS)))
HAVE_NCURSESW=0
else
HAVE_NCURSESW=1
endif
ifeq "$(HAVE_NCURSESW)" "1"
CURSESLIB = $(NCURSES_LFLAGS)
NHCFLAGS+=-DCURSES_UNICODE
else
$(info Attention: CURSES_UNICODE is not being defined without ncursesw)
endif   #HAVE_NCURSESW
endif   #USE_CURSESLIB
ifdef CURSESLIB
PKGCFLAGS=
ifeq "$(HAVE_HOMEBREW)" "1"
HAVE_HOMEBREW_NCURSES := $(shell expr `brew ls --versions ncurses > /dev/null; echo $$?` = 0)
#$(info HAVE_HOMEBREW_NCURSES=$(HAVE_HOMEBREW_NCURSES))
ifeq "$(HAVE_HOMEBREW_NCURSES)" "1"
# Newer versions of ncurses are commonly installed via homebrew, but intentionally unlinked.
HOMEBREW_LFLAGS := -L$(shell brew --prefix)/opt/ncurses/lib
HOMEBREW_WINLIB := -I$(shell brew --prefix)/opt/ncurses/include
HOMEBREW_CFLAGS =
endif   #HAVE_HOMEBREW_NCURSES
endif   #HAVE_HOMEBREW
ifeq "$(HAVE_MACPORTS)" "1"
MACPORTS_CFLAGS = -I/opt/local/include
MACPORTS_LFLAGS = -L/opt/local/lib
CURSESLIB=
ifneq ($(wildcard /opt/local/lib/libncursesw*),)
MACPORTS_WINLIB = -lncursesw
else
MACPORTS_WINLIB = -lncurses
endif
endif   #HAVE_MACPORTS
LFLAGS += $(HOMEBREW_LFLAGS) $(MACPORTS_LFLAGS)
WINLIB += $(HOMEBREW_WINLIB) $(MACPORTS_WINLIB) $(CURSESLIB)
PKGCFLAGS += $(HOMEBREW_CFLAGS) $(MACPORTS_CFLAGS)
endif   #CURSESLIB
WINLIB += $(CURSESLIB)
#$(info WINLIB=$(WINLIB))
#$(info CURSESLIB=$(CURSESLIB))
endif   #CURSES_UNICODE=0
endif   #CURSES_UNIODE=sys
endif   #MAKEFILE_SRC

ifdef WANT_MACSOUND
HAVE_SNDLIB = 1
NEEDS_SND_USERSOUNDS = 1
NEEDS_SND_SEAUTOMAP = 1
NEEDS_WAV = 1
SNDCFLAGS+= -DSND_LIB_MACSOUND
SNDLIBSRC = ../sound/macsound/macsound.m
SNDLIBOBJ = macsound.o
LFLAGS += -framework AppKit
#ifdef WANT_SPEECH
LFLAGS += -framework AVFoundation
#endif
endif

#
#-INCLUDE multisnd1-pre.370
#

# WINCFLAGS set from multiw-2.370
# SNDCFLAGS set from multisnd-pre.370
CFLAGS+= $(PKGCFLAGS) $(WINCFLAGS)
CFLAGS+= $(SNDCFLAGS)
CFLAGS+= $(NHCFLAGS)

# WINCFLAGS set from multiw-2.370
# SNDCFLAGS set from multisnd-pre.370
CCXXFLAGS+= $(WINCFLAGS)
CCXXFLAGS+= $(SNDCFLAGS)
CCXXFLAGS+= $(NHCFLAGS)

VARDATND =
VARDATND0 =

ifdef WANT_WIN_CHAIN
HINTSRC=$(CHAINSRC)
HINTOBJ=$(CHAINOBJ)
endif # WANT_WIN_CHAIN

ifdef WANT_WIN_X11
USE_XPM=1
WINX11LIB = -lXaw -lXmu -lXext -lXt -lX11
VARDATND0 += x11tiles NetHack.ad pet_mark.xbm pilemark.xbm
# -x: if built without dodlb, some versions of mkfontdir think *.lev are fonts
POSTINSTALL += bdftopcf win/X11/nh10.bdf > $(HACKDIR)/nh10.pcf; ( cd $(HACKDIR); mkfontdir -x .lev );
# separate from CFLAGS so that we don't pass it to every file
X11CFLAGS = -I/opt/X11/include
# avoid repeated complaints about _X_NONNULL(args...) in <X11/Xfuncproto.h>
X11CFLAGS += -Wno-variadic-macros
ifdef USE_XPM
CFLAGS += -DUSE_XPM
WINX11LIB += -lXpm
VARDATND0 += rip.xpm
endif
WINLIB += $(WINX11LIB)
LFLAGS += -L/opt/X11/lib
endif	# WANT_WIN_X11

ifdef WANT_WIN_QT
LINK = $(CXX)
ifdef WANT_WIN_QT5
# Qt5 requires C++11
QTCXXFLAGS += $(sort $(shell PKG_CONFIG_PATH=$(QTDIR)/lib/pkgconfig \
		pkg-config Qt5Gui Qt5Widgets Qt5Multimedia --cflags))
WINLIB += $(shell PKG_CONFIG_PATH=$(QTDIR)/lib/pkgconfig \
		pkg-config Qt5Gui Qt5Widgets Qt5Multimedia --libs)
endif    # WANT_WIN_QT5
ifdef WANT_WIN_QT6
QTCXXFLAGS += -std=c++17 -I $(QTDIR)/include -I $(QTDIR)/include/QtCore \
		-I $(QTDIR)/include/QtMultimedia
MOC = moc
MOCPATH = $(QTDIR)/share/qt/libexec/moc
WINLIB += -F $(QTDIR)/Frameworks -framework QtCore -framework QtGui \
		-framework QtWidgets -framework QtMultimedia
endif    # WANT_WIN_QT6
VARDATND0 += nhtiles.bmp rip.xpm nhsplash.xpm
# XXX if /Developer/qt exists and QTDIR not set, use that
ifndef QTDIR
$(error QTDIR not defined in the environment or Makefile)
endif	# QTDIR
# XXX make sure QTDIR points to something reasonable
else	# !WANT_WIN_QT
LINK=$(CC)
endif	# !WANT_WIN_QT

# prevent duplicate tile.o in WINOBJ
WINOBJ = $(WINOBJ0) $(sort $(XTRAOBJ))
# prevent duplicates in VARDATND if both X11 and Qt are being supported
VARDATND += $(sort $(VARDATND0))

GIT_HASH := $(shell echo `git rev-parse --verify HEAD` 2>&1)
GIT_BRANCH := $(shell echo `git rev-parse --abbrev-ref HEAD` 2>&1)

ifdef GIT_HASH
GITHASH = -DNETHACK_GIT_SHA=\"$(GIT_HASH)\"
endif
ifdef GIT_BRANCH
GITBRANCH = -DNETHACK_GIT_BRANCH=\"$(GIT_BRANCH)\"
endif

ifdef WANT_LIBNH
CFLAGS += -DSHIM_GRAPHICS -DNOTTYGRAPHICS -DNOSHELL -DLIBNH
LIBNHSYSSRC = ../sys/libnh/libnhmain.c \
		../sys/share/ioctl.c ../sys/share/unixtty.c \
		../sys/unix/unixunix.c ../sys/unix/unixres.c \
		../win/shim/winshim.c
LIBNHSYSOBJ = $(TARGETPFX)libnhmain.o $(TARGETPFX)ioctl.o \
		$(TARGETPFX)unixtty.o $(TARGETPFX)unixunix.o \
		$(TARGETPFX)unixres.o $(TARGETPFX)winshim.o
#don't bother building the game executable as it will fail
#without winshim
override GAME=
MOREALL += ( cd src ; $(MAKE) pregame ; $(MAKE) $(TARGETPFX)libnh.a )
endif  # WANT_LIBNH

# Lua
# when building liblua.a, avoid warning that use of tmpnam() should be
# replaced by mkstemp(); the lua code doesn't use nethack's config.h so
# this needs to be passed via make rather than defined in unixconf.h
ifdef GITSUBMODULES
LUAFLAGS=CC='$(CC)' MYCFLAGS=' -DLUA_USE_MACOSX'
ifneq "$(CCISCLANG)" ""
# clang
LUAFLAGS +=CWARNGCC=''
endif   # clang
override LUAHEADERS = submodules/lua
override LUA2NHTOP = ../..
override LUAMAKEFLAGS=$(LUAFLAGS)
endif   # GITSUBMODULES

ifdef WANT_SHARE_INSTALL

#ifdef MAKEFILE_SRC
#$(info WANT_SOURCE_INSTALL = $(WANT_SOURCE_INSTALL), WANT_SHARE_INSTALL = $(WANT_SHARE_INSTALL))
#endif
# if $GAMEUID is root, we install into roughly proper Mac locations, otherwise
# we install into ~/nethackdir
ifeq ($(GAMEUID),root)
PREFIX:=/Library/NetHack
SHELLDIR=/usr/local/bin
HACKDIR=$(PREFIX)/nethackdir
CHOWN=chown
CHGRP=chgrp
# We run sgid so the game has access to both HACKDIR and user preferences.
GAMEPERM = 02755
else	# ! root
PREFIX:=/Users/$(GAMEUID)
SHELLDIR=$(PREFIX)/bin
HACKDIR=$(PREFIX)/Library/NetHack/nethackdir
CHOWN=/usr/bin/true
CHGRP=/usr/bin/true
GAMEPERM = 0500
endif	# ! root
VARFILEPERM = 0664
VARDIRPERM = 0775
ROOTCHECK= [[ `id -u` == 0 ]] || ( echo "Must run install with sudo."; exit 1)
# XXX it's nice we don't write over sysconf, but we've already erased it
# make sure we have group GAMEUID and group GAMEGRP
PREINSTALL= . sys/unix/hints/macosx.sh user2 $(GAMEUID); \
		. sys/unix/hints/macosx.sh group2 $(GAMEGRP); \
		(mkdir $(SHELLDIR) || true); chown $(GAMEUID) $(SHELLDIR)
POSTINSTALL+= sys/unix/hints/macosx.sh editsysconf sys/unix/sysconf $(HACKDIR)/sysconf; \
		$(CHOWN) $(GAMEUID) $(HACKDIR)/sysconf; \
		$(CHGRP) $(GAMEGRP) $(HACKDIR)/sysconf; \
		chmod $(VARFILEPERM) $(HACKDIR)/sysconf; \
		util/makedefs --grep-defined CRASHREPORT && \
		 ( cp win/macosx/NetHackCrashReport.JavaScript $(HACKDIR) && \
		 chmod 0500 $(HACKDIR)/NetHackCrashReport.JavaScript )

else ifdef WANT_SOURCE_INSTALL

#ifdef MAKEFILE_SRC
#$(info WANT_SOURCE_INSTALL = $(WANT_SOURCE_INSTALL), WANT_SHARE_INSTALL = $(WANT_SHARE_INSTALL))
#endif
PREFIX=$(abspath $(NHSROOT))
# suppress nethack.sh
#SHELLDIR=
HACKDIR=$(PREFIX)/playground
CHOWN=/usr/bin/true
CHGRP=/usr/bin/true
GAMEPERM = 0700
VARFILEPERM = 0600
VARDIRPERM = 0700
POSTINSTALL+= sys/unix/hints/macosx.sh editsysconf sys/unix/sysconf $(HACKDIR)/sysconf; \
		util/makedefs --grep-defined CRASHREPORT && \
		    ( cp win/macosx/NetHackCrashReport.JavaScript $(HACKDIR) && \
		    chmod 0500 $(HACKDIR)/NetHackCrashReport.JavaScript )

# We can use "make all" to build the whole thing - but it misses some things:
MOREALL=$(MAKE) install
CFLAGS+=-DSYSCF -DSYSCF_FILE=\"$(HACKDIR)/sysconf\" -DSECURE

else	# !WANT_SOURCE_INSTALL

#ifdef MAKEFILE_SRC
#$(info WANT_SOURCE_INSTALL = $(WANT_SOURCE_INSTALL), WANT_SHARE_INSTALL = $(WANT_SHARE_INSTALL))
#endif
PREFIX:=$(wildcard ~)
SHELLDIR=$(PREFIX)/bin
HACKDIR=$(PREFIX)/nethackdir
CHOWN=/usr/bin/true
CHGRP=/usr/bin/true
GAMEPERM = 0700
VARFILEPERM = 0600
VARDIRPERM = 0700

ifdef ($(WANT_DEFAULT),X11)
# install nethack.rc as ~/.nethackrc if no ~/.nethackrc exists
PREINSTALL+= cp -n win/X11/nethack.rc ~/.nethackrc || true;
endif	# WANT_DEFAULT X11

PREINSTALL+= (mkdir $(SHELLDIR) || true);
POSTINSTALL+= sys/unix/hints/macosx.sh editsysconf sys/unix/sysconf $(HACKDIR)/sysconf; \
		$(CHOWN) $(GAMEUID) $(HACKDIR)/sysconf; \
		$(CHGRP) $(GAMEGRP) $(HACKDIR)/sysconf; \
		chmod $(VARFILEPERM) $(HACKDIR)/sysconf; \
		util/makedefs --grep-defined CRASHREPORT && \
		  ( cp win/macosx/NetHackCrashReport.JavaScript $(HACKDIR) && \
		  chmod 0500 $(HACKDIR)/NetHackCrashReport.JavaScript )

endif   # !WANT_SHARE_INSTALL

INSTDIR=$(HACKDIR)
VARDIR=$(HACKDIR)


# ~/Library/Preferences/NetHack Defaults
# OPTIONS=name:player,number_pad,menustyle:partial,!time,showexp
# OPTIONS=hilite_pet,toptenwin,msghistory:200,windowtype:Qt
#
# Install.Qt mentions a patch for macos - it's not there (it seems to be in the Qt binary
# package under the docs directory).
#
#-INCLUDE cross-pre.370
#
#
#-INCLUDE gbdates-pre.370
#
#
#-INCLUDE multisnd2-pre.370
#

ifdef MAKEFILE_TOP
.PHONY: bundle
NHTOP=
endif
ifdef MAKEFILE_SRC
.PHONY: buildbundle bundledir updatescript
NHTOP=../
abspath := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
ABSBUNDLEPATH := $(dir $(abspath))
$(info ABSBUNDLEPATH=$(ABSBUNDLEPATH))

ifdef WANT_BUNDLE
#
# bundle
#
# $(HACKDIR)/$(GAME).app/
#              Contents/
#                 Frameworks/
#                 Info.plist
#                 MacOS/
#                    $(GAME)
#                 PkgInfo/
#                 PlugIns/
#                 Resources/
#                 SharedFrameWorks/
#
BUNDLE_NAME = NetHackTerm
BUNDLE_GAMENAME = NetHack
BUNDLE_PARENTDIR = $(ABSBUNDLEPATH)bundle
BUNDLE_TOP = $(BUNDLE_PARENTDIR)/$(BUNDLE_NAME).app
BUNDLE_CONTENTSDIR   = $(BUNDLE_TOP)/Contents
BUNDLE_FRAMEWORKSDIR = $(BUNDLE_CONTENTSDIR)/Frameworks
BUNDLE_MACOSDIR      = $(BUNDLE_CONTENTSDIR)/MacOS
BUNDLE_PKGINFODIR    = $(BUNDLE_CONTENTSDIR)/PkgInfo
BUNDLE_PLUGINSDIR    = $(BUNDLE_CONTENTSDIR)/PlugIns
BUNDLE_RESOURCESDIR  = $(BUNDLE_CONTENTSDIR)/Resources
BUNDLE_SHAREDFRAMEWORKSDIR = $(BUNDLE_CONTENTSDIR)/SharedFrameworks
BUNDLE_INFOPLIST     = $(BUNDLE_CONTENTSDIR)/Info.plist
BUNDLE_GAME          = $(BUNDLE_MACOSDIR)/$(BUNDLE_GAMENAME)
BUNDLE_LAUNCHER      = $(BUNDLE_MACOSDIR)/$(BUNDLE_NAME)
ifdef NEEDS_WAV
BUNDLEWAVS = $(addsuffix .wav, $(SNDWAVS))
endif

IMAGESLIST = $(BUNDLE_NAME).icns

RESOURCES_LIST = $(BUNDLEWAVS) $(IMAGESLIST)

BUNDLE_ADD_TO_RESOURCES = $(addprefix $(BUNDLE_RESOURCESDIR)/, $(RESOURCES_LIST))

MACOS_LIST = $(BUNDLE_GAMENAME)
# NetHackGuidebook.applescript NetHackRecover.applescript NetHackTerm.applescript

BUNDLE_ADD_TO_MACOS = $(addprefix $(BUNDLE_MACOSDIR)/, $(MACOS_LIST))

endif   # WANT_BUNDLE
#
# We include the bundle outside of a WANT_BUNDLE block because
# it is unlikely that anyone will use 'make WANT_BUNDLE=1 clean'
#
CLEANMORE += rm -rf ../bundle ;
endif   # MAKEFILE_SRC

#
#-POST

#
#-INCLUDE gbdates-post.370
#
#
#-INCLUDE multisnd-post.370
#

ifdef WANT_LIBNH
$(TARGETPFX)libnh.a: $(HOBJ) $(LIBNHSYSOBJ) ../lib/lua/liblua.a
	$(AR) rcs $@ $(HOBJ) $(LIBNHSYSOBJ) ../lib/lua/liblua.a
	@echo "$@ built."
$(TARGETPFX)libnhmain.o : ../sys/libnh/libnhmain.c $(HACK_H)
	$(CC) $(CFLAGS) -c -o$@ $<
$(TARGETPFX)winshim.o : ../win/shim/winshim.c $(HACK_H)
	$(CC) $(CFLAGS) -c -o$@ $<
endif  # WANT_LIBNH

# -- bundle ---
#
ifdef MAKEFILE_TOP
bundle: update uudecode
	( cd src ; $(MAKE) WANT_BUNDLE=1 buildbundle )

uudecode:
	(cd util ; $(MAKE) WANT_BUNDLE=1 uudecode )
endif   # MAKEFILE_TOP
ifdef WANT_BUNDLE
ifdef MAKEFILE_SRC

BUNDLEDIRS = $(BUNDLE_PARENTDIR) $(BUNDLE_TOP) $(BUNDLE_CONTENTSDIR) $(BUNDLE_FRAMEWORKSDIR) \
		$(BUNDLE_MACOSDIR) $(BUNDLE_PKGINFODIR) $(BUNDLE_PLUGINSDIR) \
		$(BUNDLE_RESOURCESDIR) $(BUNDLE_SHAREDFRAMEWORKSDIR)

BUNDLELIST = $(BUNDLE_INFOPLIST) $(BUNDLE_LAUNCHER) $(BUNDLE_ADD_TO_MACOS) $(BUNDLE_ADD_TO_RESOURCES)

buildbundle: bundledir $(BUNDLEDIRS) $(BUNDLELIST) updatescript
	@echo $(GAME) bundle : $(BUNDLE_TOP)
	@echo $(GAME)   game : $(BUNDLE_GAME)

$(BUNDLE_RESOURCESDIR)/%: $(NHTOP)sound/wav/% | $(BUNDLE_RESOURCESDIR)
	cp $< $@

#$(BUNDLE_MACOSDIR)/%: $(NHTOP)win/macosx/% | $(BUNDLE_MACOSDIR)
#	cp $< $@

$(BUNDLE_MACOSDIR)/%: $(dir $(GAMEBIN))/% | $(BUNDLE_MACOSDIR)
	cp $< $@

$(BUNDLE_PARENTDIR):
		mkdir -p $@

$(BUNDLE_TOP): $(BUNDLE_PARENTDIR)
		mkdir -p $@

$(BUNDLE_CONTENTSDIR): $(BUNDLE_TOP)
		mkdir -p $@

$(BUNDLE_FRAMEWORKSDIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_MACOSDIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_PKGINFODIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_PLUGINSDIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_RESOURCESDIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_SHAREDFRAMEWORKSDIR): $(BUNDLE_CONTENTSDIR)
		mkdir -p $@

$(BUNDLE_GAME): nethack
	cp nethack $@

#FIXME: replace hardcoded information with actual values derived from NetHack build
$(BUNDLE_INFOPLIST):
	@echo '<?xml version="1.0" encoding="UTF-8"?>' >$@
	@echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$@
	@echo '<plist version="1.0">' >>$@
	@echo '<dict>' >>$@
	@echo '<key>CFBundleInfoDictionaryVersion</key>' >>$@
	@echo '<string>6.0</string>' >>$@
	@echo '<key>CFBundleDevelopmentRegion</key>' >>$@
	@echo '<string>en</string>' >>$@
	@echo '<key>CFBundlePackageType</key>' >>$@
	@echo '<string>APPL</string>' >>$@
	@echo '<key>CFBundleGetInfoString</key>' >>$@
	@echo '<string>$(BUNDLE_GAMENAME) 3.7 for MacOS</string>' >>$@
	@echo '<key>CFBundleIdentifier</key>' >>$@
	@echo '<string>org.nethack.macos</string>' >>$@
	@echo '<key>CFBundleExecutable</key>' >>$@
	@echo '<string>$(BUNDLE_NAME)</string>' >>$@
	@echo '<key>CFBundleIconFile</key>' >>$@
	@echo '<string>$(BUNDLE_NAME)</string>' >>$@
	@echo '<key>CFBundleDisplayName</key>' >>$@
	@echo '<string>$(BUNDLE_NAME)</string>' >>$@
	@echo '<key>CFBundleName</key>' >>$@
	@echo '<string>$(BUNDLE_NAME)</string>' >>$@
	@echo '<key>CFBundleVersion</key>' >>$@
	@echo '<string>3.7.0b73</string>' >>$@
	@echo '<key>CFBundleShortVersionString</key>' >>$@
	@echo '<string>3.7.0</string>' >>$@
	@echo '<key>NSHumanReadableCopyright</key>' >>$@
	@echo '<string>©By Stichting Mathematisch Centrum and M. Stephenson 1985-2023</string>' >>$@
	@echo '<key>LSApplicationCategoryType</key>' >>$@
	@echo '<string>public.app-category.adventure-games</string>' >>$@
	@echo '</dict>' >>$@
	@echo '</plist>' >>$@
#		$(NHTOP)sys/unix/hints/macosx.sh infoplist >$@

$(BUNDLE_LAUNCHER):
	@echo '#!/bin/sh' >$@
	@echo 'dir=$$(dirname $$0)' >>$@
	@echo 'open -a Terminal file://$${dir}/$(BUNDLE_GAMENAME)' >>$@
	chmod +x $@

updatescript:
ifneq "$(SHELLDIR)" ""
	if test -f $(SHELLDIR)/$(GAME); then \
	  cp $(SHELLDIR)/$(GAME) $(GAME).tmp; \
	  sed -i '' 's;\$$HACKDIR/$(GAME);$(BUNDLE_GAME);' $(SHELLDIR)/$(GAME) ; \
	  diff -u $(GAME).tmp $(SHELLDIR)/$(GAME) | grep "="; \
	  rm -f $(GAME).tmp; \
	fi;
endif

# We can't use a general rule because the file name of the .uu does not match the
# uudecode target file name inside the .uu file, in this case (nhicns.uu to nethack.icns)
$(BUNDLE_RESOURCESDIR)/$(BUNDLE_NAME).icns: $(BUNDLE_RESOURCESDIR) $(NHTOP)util/uudecode \
						$(NHTOP)win/share/nhicns.uu
	$(NHTOP)util/uudecode $(NHTOP)win/share/nhicns.uu
	mv nethack.icns $@

endif   # MAKEFILE_SRC
endif   # WANT_BUNDLE

#
# --- end of bundle ---
#
#
#-INCLUDE cross-post.370
#

ifdef MAKEFILE_TOP
###
### Packaging
###
# Notes:
# 1) The Apple developer utilities must be installed in the default location.
# 2) Do a normal build before trying to package the game.
# 3) This matches the 3.4.3 Term package, but there are some things that
#    should be changed.
#
# Packages that are being distributed must be signed by a Developer ID
# Installer certificate.  Set DEVELOPER_CERT to the name of the certificate
# if you wish for your package to be signed for distribution.
#
# If building a package for signing, you must use sudo approriately.
# the binaries and package using sudo but you DO NOT use sudo to sign the
# package.  If you use sudo to sign the package, it will fail.
#
#   sudo make all
#   sudo make build_tty_pkg
#   make sign_tty_pkg
#

ifdef WANT_WIN_TTY
DEVUTIL=/Developer/Applications/Utilities
SVS=$(shell $(NHSROOT)/util/makedefs --svs)
SVSDOT=$(shell $(NHSROOT)/util/makedefs --svs .)

PKGROOT_UG	= PKGROOT/$(PREFIX)
PKGROOT_UGLN	= PKGROOT/$(HACKDIR)
PKGROOT_BIN	= PKGROOT/$(SHELLDIR)

#DEVELOPER_CERT = Developer ID Installer: Bart House
DEVELOPER_CERT = NONE

spotless::
	rm -rf RESOURCES
	rm -rf PKG
	rm -rf PKGSCRIPTS
	rm -rf PKGROOT
	rm -f Info.plist
	rm -f Distribution.xml
	rm -f NetHack-*-mac-Term*

build_tty_pkg:
ifneq (,$(WANT_WIN_X11)$(WANT_WIN_QT))
	-echo build_tty_pkg only works for a tty-only build
	exit 1
else
	rm -rf NetHack-$(SVS)-mac-Term.pkg NetHack-$(SVS)-mac-Term.dmg
	$(MAKE) build_package_root
	rm -rf RESOURCES
	mkdir RESOURCES
	#enscript --language=rtf -o - < dat/license >RESOURCES/License.rtf
	sys/unix/hints/macosx.sh descplist > RESOURCES/Description.plist
	sys/unix/hints/macosx.sh infoplist > Info.plist

	mkdir PKGROOT/Applications
	#osacompile -o NetHackQt/NetHackQt.app/nethackdir/NetHackRecover.app \
	#	 win/macosx/NetHackRecover.applescript
	#cp win/macosx/recover.pl NetHackQt/NetHackQt.app/nethackdir
	osacompile -o PKGROOT/Applications/NetHackRecover.app \
		 win/macosx/NetHackRecover.applescript
	cp win/macosx/recover.pl $(PKGROOT_UGLN)

	osacompile -o PKGROOT/Applications/NetHackTerm.app \
		 win/macosx/NetHackTerm.applescript

	# XXX integrate into Makefile.doc
	(cd doc; cat Guidebook.mn | ../util/makedefs --grep --input - --output - \
	| tbl tmac.n - | groff | pstopdf -i -o Guidebook.pdf)
	cp doc/Guidebook.pdf $(PKGROOT_UG)/doc/NetHackGuidebook.pdf

	osacompile -o PKGROOT/Applications/NetHackGuidebook.app \
		 win/macosx/NetHackGuidebook.applescript

	mkdir -p PKG
	pkgbuild --root PKGROOT --identifier org.nethack.term --scripts PKGSCRIPTS PKG/NH-Term.pkg
	productbuild --synthesize --product Info.plist --package PKG/NH-Term.pkg Distribution.xml
	productbuild --distribution Distribution.xml --resources RESOURCES --package-path PKG NetHack-$(SVS)-mac-Term-unsigned.pkg
ifeq ($(DEVELOPER_CERT),NONE)
	cp NetHack-$(SVS)-mac-Term-unsigned.pkg NetHack-$(SVS)-mac-Term.pkg
	hdiutil create -verbose -srcfolder NetHack-$(SVS)-mac-Term-unsigned.pkg NetHack-$(SVS)-mac-Term-unsigned.dmg
	@echo -------------------------------------------
	@echo PACKAGE IS NOT SIGNED FOR DISTRIBUTION!!!!!
	@echo ===========================================
else
	@echo "run 'make sign_tty_pkg' to complete package"
endif

sign_tty_pkg:
	productsign --timestamp=none --sign "$(DEVELOPER_CERT)" NetHack-$(SVS)-mac-Term-unsigned.pkg NetHack-$(SVS)-mac-Term.pkg || (echo "Package signing failed"; exit 1)
	spctl -a -v --type install NetHack-$(SVS)-mac-Term.pkg || (echo "Package not signed properly"; exit 1)
	hdiutil create -verbose -srcfolder NetHack-$(SVS)-mac-Term.pkg NetHack-$(SVS)-mac-Term.dmg

build_package_root:
	cd src/..       # make sure we are at TOP
	rm -rf PKGROOT
	mkdir -p $(PKGROOT_UG)/lib $(PKGROOT_BIN) $(PKGROOT_UG)/man/man6 $(PKGROOT_UG)/doc $(PKGROOT_UGLN)
	install -p src/nethack $(PKGROOT_BIN)
	# XXX should this be called nethackrecover?
	install -p util/recover $(PKGROOT_BIN)
	install -p doc/nethack.6 $(PKGROOT_UG)/man/man6
	install -p doc/recover.6 $(PKGROOT_UG)/man/man6
	install -p doc/Guidebook $(PKGROOT_UG)/doc
	install -p dat/nhdat $(PKGROOT_UGLN)
#XXX no code to package NetHackCrashReport.JavaScript
	sys/unix/hints/macosx.sh editsysconf sys/unix/sysconf $(PKGROOT_UGLN)/sysconf
	cd dat; install -p $(DATNODLB) ../$(PKGROOT_UGLN)
# XXX these files should be somewhere else for good Mac form
	touch $(PKGROOT_UGLN)/perm $(PKGROOT_UGLN)/record $(PKGROOT_UGLN)/logfile $(PKGROOT_UGLN)/xlogfile $(PKGROOT_UGLN)/livelog
	mkdir $(PKGROOT_UGLN)/save
# XXX what about a news file?

	mkdir -p PKGSCRIPTS
	echo '#!/bin/sh'                              >  PKGSCRIPTS/postinstall
	echo dseditgroup -o create -r '"Games Group"' -s 3600 $(GAMEGRP) >> PKGSCRIPTS/postinstall
	echo $(CHOWN) $(GAMEUID) $(HACKDIR)        >> PKGSCRIPTS/postinstall
	echo $(CHOWN) $(GAMEUID) $(HACKDIR)/*        >> PKGSCRIPTS/postinstall
	echo $(CHGRP) $(GAMEGRP) $(HACKDIR)        >> PKGSCRIPTS/postinstall
	echo $(CHGRP) $(GAMEGRP) $(HACKDIR)/*        >> PKGSCRIPTS/postinstall
	echo $(CHOWN) $(GAMEUID) $(SHELLDIR)/nethack  >> PKGSCRIPTS/postinstall
	echo $(CHGRP) $(GAMEGRP) $(SHELLDIR)/nethack  >> PKGSCRIPTS/postinstall
	echo $(CHOWN) $(GAMEUID) $(SHELLDIR)/recover  >> PKGSCRIPTS/postinstall
	echo $(CHGRP) $(GAMEGRP) $(SHELLDIR)/recover  >> PKGSCRIPTS/postinstall
	echo chmod $(VARDIRPERM)  $(HACKDIR)          >> PKGSCRIPTS/postinstall
	echo chmod $(VARDIRPERM)  $(HACKDIR)/save     >> PKGSCRIPTS/postinstall
	echo chmod $(FILEPERM)    $(HACKDIR)/license  >> PKGSCRIPTS/postinstall
	echo chmod $(FILEPERM)    $(HACKDIR)/nhdat    >> PKGSCRIPTS/postinstall
	echo chmod $(FILEPERM)    $(HACKDIR)/symbols  >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/perm     >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/record   >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/logfile  >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/xlogfile >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/livelog  >> PKGSCRIPTS/postinstall
	echo chmod $(VARFILEPERM) $(HACKDIR)/sysconf  >> PKGSCRIPTS/postinstall
	echo chmod $(GAMEPERM)   $(SHELLDIR)/nethack  >> PKGSCRIPTS/postinstall
	echo chmod $(EXEPERM)    $(SHELLDIR)/recover  >> PKGSCRIPTS/postinstall
	chmod 0775 PKGSCRIPTS/postinstall

endif	# end of build_tty_pkg
endif	# WANT_WIN_TTY for packaging

ifdef WANT_WIN_QT
# XXX untested and incomplete (see below)
build_qt_pkg:
ifneq (,$(WANT_WIN_X11)$(WANT_WIN_TTY))
	-echo build_qt_pkg only works for a qt-only build
	exit 1
else
	$(MAKE) build_package_root
	rm -rf NetHackQt
	mkdir -p NetHackQt/NetHackQt.app/nethackdir/save
	mkdir NetHackQt/Documentation
	cp doc/Guidebook.txt doc/nethack.txt doc/recover.txt NetHackQt/Documentation

	osacompile -o NetHackQt/NetHackQt.app/nethackdir/NetHackRecover.app \
		 win/macosx/NetHackRecover.applescript
	cp win/macosx/recover.pl NetHackQt/NetHackQt.app/nethackdir

	mkdir -p NetHackQt/NetHackQt.app/Contents/Frameworks
	cp $(QTDIR)/libqt-mt.3.dylib NetHackQt/NetHackQt.app/Contents/Frameworks

	mkdir NetHackQt/NetHackQt.app/Contents/MacOS
	mv PKGROOT/nethack NetHackQt/NetHackQt.app/Contents/MacOS

	mv PKGROOT/lib/nethackdir NetHackQt/NetHackQt.app/nethackdir

# XXX still missing:
#NetHackQt/NetHackQt.app
# /Contents
#	Info.plist
#	Resources/nethack.icns
#NetHackQt/Documentation
#NetHackQtRecover.txt
#NetHack Defaults.txt
#changes.patch XXX is this still needed?  why isn't it part of the tree?
#  doesn't go here
	hdiutil create -verbose -srcfolder NetHackQt NetHack-$(SVS)-macosx-qt.dmg
endif	# end of build_qt_pkg
endif	# WANT_WIN_QT for packaging
endif	# MAKEFILE_TOP
#
