# This file can be invoked from the various frameworkinstall... targets in the 
# main Makefile. The next couple of variables are overridden on the 
# commandline in that case.

VERSION=@VERSION@
ENSUREPIP=@ENSUREPIP@
builddir = ..
srcdir=@srcdir@
prefix=@prefix@
exec_prefix=@exec_prefix@
LIBDEST=$(prefix)/lib/python$(VERSION)
RUNSHARED=@RUNSHARED@
BUILDEXE=@BUILDEXEEXT@
BUILDPYTHON=$(builddir)/python$(BUILDEXE)
DESTDIR=
LDFLAGS=@LDFLAGS@
FRAMEWORKUNIXTOOLSPREFIX=@FRAMEWORKUNIXTOOLSPREFIX@
PYTHONFRAMEWORK=@PYTHONFRAMEWORK@
PYTHONFRAMEWORKIDENTIFIER=@PYTHONFRAMEWORKIDENTIFIER@
LIPO_32BIT_FLAGS=@LIPO_32BIT_FLAGS@
CC=@CC@
MACOSX_DEPLOYMENT_TARGET=@CONFIGURE_MACOSX_DEPLOYMENT_TARGET@
export MACOSX_DEPLOYMENT_TARGET

# These are normally glimpsed from the previous set
BINDIR=		@bindir@
PYTHONAPPSDIR=@FRAMEWORKINSTALLAPPSPREFIX@/$(PYTHONFRAMEWORK) $(VERSION)
APPINSTALLDIR=$(prefix)/Resources/Python.app

# Variables for installing the "normal" unix binaries
INSTALLED_PYTHONAPP=$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)

# Items more-or-less copied from the main Makefile
DIRMODE=755
FILEMODE=644
INSTALL=@INSTALL@
INSTALL_SYMLINK=ln -fsn
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_SCRIPT= @INSTALL_SCRIPT@
INSTALL_DATA=@INSTALL_DATA@
LN=@LN@
STRIPFLAG=-s
CPMAC=CpMac

APPTEMPLATE=$(srcdir)/Resources/app
APPSUBDIRS=MacOS Resources
CACHERSRC=$(srcdir)/scripts/cachersrc.py
compileall=$(srcdir)/../Lib/compileall.py

installapps: install_Python install_pythonw install_BuildApplet install_PythonLauncher \
	install_IDLE checkapplepython install_versionedtools


install_pythonw: pythonw
	$(INSTALL_PROGRAM) $(STRIPFLAG) pythonw "$(DESTDIR)$(prefix)/bin/pythonw$(VERSION)"
	$(INSTALL_PROGRAM) $(STRIPFLAG) pythonw "$(DESTDIR)$(prefix)/bin/python$(VERSION)"
	ln -sf python$(VERSION) "$(DESTDIR)$(prefix)/bin/python2"
	ln -sf python2 "$(DESTDIR)$(prefix)/bin/python"
	ln -sf pythonw$(VERSION) "$(DESTDIR)$(prefix)/bin/pythonw2"
	ln -sf pythonw2 "$(DESTDIR)$(prefix)/bin/pythonw"
ifneq ($(LIPO_32BIT_FLAGS),)
	lipo $(LIPO_32BIT_FLAGS) -output $(DESTDIR)$(prefix)/bin/python$(VERSION)-32 pythonw
	lipo $(LIPO_32BIT_FLAGS) -output $(DESTDIR)$(prefix)/bin/pythonw$(VERSION)-32 pythonw
	ln -sf python$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/python2-32"
	ln -sf python2-32 "$(DESTDIR)$(prefix)/bin/python-32"
	ln -sf pythonw$(VERSION)-32 "$(DESTDIR)$(prefix)/bin/pythonw2-32"
	ln -sf pythonw2-32 "$(DESTDIR)$(prefix)/bin/pythonw-32"
endif


#
# Install unix tools in /usr/local/bin. These are just aliases for the 
# actual installation inside the framework.
#
installunixtools:
	if [ ! -d "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" ]; then  \
		$(INSTALL) -d -m $(DIRMODE) "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" ;\
	fi
	for fn in python pythonw idle pydoc python-config smtpd.py 2to3 \
		  python2 pythonw2 idle2 \
		  pydoc2 python2-config smtpd2.py \
		  2to3-2 \
		  python$(VERSION) pythonw$(VERSION) idle$(VERSION) \
		  pydoc$(VERSION) python$(VERSION)-config smtpd$(VERSION).py \
		  2to3-$(VERSION) ;\
	do \
		ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\
	done
ifneq ($(LIPO_32BIT_FLAGS),)
	for fn in python-32 pythonw-32 \
		  python2-32 pythonw2-32 \
		  python$(VERSION)-32 pythonw$(VERSION)-32 ;\
	do \
		ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\
	done
endif
	-if test "x$(ENSUREPIP)" != "xno"  ; then \
		cd "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" && \
		for fn in \
				easy_install \
				pip \
				pip2 \
				; \
		do \
			rm -f $${fn} ;\
			$(LN) -s $(BINDIR)/$${fn} $${fn} ;\
		done ;\
	fi


#
# Like installunixtools, but only install links to the versioned binaries.
#
altinstallunixtools:
	if [ ! -d "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" ]; then  \
		$(INSTALL) -d -m $(DIRMODE) "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" ;\
	fi
	for fn in python$(VERSION) pythonw$(VERSION) idle$(VERSION) \
		  pydoc$(VERSION) python$(VERSION)-config smtpd$(VERSION).py ;\
	do \
		ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\
	done
ifneq ($(LIPO_32BIT_FLAGS),)
	for fn in python$(VERSION)-32 pythonw$(VERSION)-32 ;\
	do \
		ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\
	done
endif
	ln -fs "$(prefix)/bin/2to3-$(VERSION)" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/2to3-$(VERSION)"
	-if test "x$(ENSUREPIP)" != "xno"  ; then \
		cd "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" && \
		for fn in \
				easy_install-$(VERSION) \
				pip$(VERSION) \
				; \
		do \
			rm -f $${fn} ;\
			$(LN) -s $(BINDIR)/$${fn} $${fn} ;\
		done ;\
	fi

# By default most tools are installed without a version in their basename, to
# make it easier to install (and use) several python versions side-by-side move
# the tools to a version-specific name and add the non-versioned name as an
# alias.
install_versionedtools:
	for fn in idle pydoc ;\
	do \
		if [ -h "$(DESTDIR)$(prefix)/bin/$${fn}" ]; then \
			continue ;\
		fi ;\
		mv "$(DESTDIR)$(prefix)/bin/$${fn}" "$(DESTDIR)$(prefix)/bin/$${fn}$(VERSION)"  ;\
		ln -sf "$${fn}$(VERSION)" "$(DESTDIR)$(prefix)/bin/$${fn}2" ;\
		ln -sf "$${fn}2" "$(DESTDIR)$(prefix)/bin/$${fn}" ;\
	done
	if [ ! -h "$(DESTDIR)$(prefix)/bin/smtpd.py" ]; then \
		mv "$(DESTDIR)$(prefix)/bin/smtpd.py" "$(DESTDIR)$(prefix)/bin/smtpd$(VERSION).py"  ;\
		ln -sf "smtpd$(VERSION).py" "$(DESTDIR)$(prefix)/bin/smtpd2.py" ;\
		ln -sf "smtpd2.py" "$(DESTDIR)$(prefix)/bin/smtpd.py" ;\
	fi
	if [ ! -h "$(DESTDIR)$(prefix)/bin/2to3" ]; then \
		mv "$(DESTDIR)$(prefix)/bin/2to3" "$(DESTDIR)$(prefix)/bin/2to3-$(VERSION)"  ;\
		ln -sf "2to3-$(VERSION)" "$(DESTDIR)$(prefix)/bin/2to3-2" ;\
		ln -sf "2to3-2" "$(DESTDIR)$(prefix)/bin/2to3" ;\
	fi


pythonw: $(srcdir)/Tools/pythonw.c Makefile
	$(CC) $(LDFLAGS) -DPYTHONFRAMEWORK='"$(PYTHONFRAMEWORK)"' -o $@ $(srcdir)/Tools/pythonw.c -I.. -I$(srcdir)/../Include ../$(PYTHONFRAMEWORK).framework/Versions/$(VERSION)/$(PYTHONFRAMEWORK)

install_PythonLauncher:
	cd PythonLauncher && make install DESTDIR=$(DESTDIR)

install_Python:
	@for i in "$(PYTHONAPPSDIR)" "$(APPINSTALLDIR)" "$(APPINSTALLDIR)/Contents"; do \
		if test ! -d "$(DESTDIR)$$i"; then \
			echo "Creating directory $(DESTDIR)$$i"; \
			$(INSTALL) -d -m $(DIRMODE) "$(DESTDIR)$$i"; \
		fi;\
	done
	@for i in $(APPSUBDIRS); do \
		if test ! -d "$(DESTDIR)$(APPINSTALLDIR)/Contents/$$i"; then \
			echo "Creating directory $(DESTDIR)$(APPINSTALLDIR)/Contents/$$i"; \
			$(INSTALL) -d -m $(DIRMODE) "$(DESTDIR)$(APPINSTALLDIR)/Contents/$$i"; \
		else	true; \
		fi; \
	done
	@for d in . $(APPSUBDIRS); \
	do \
		a=$(APPTEMPLATE)/$$d; \
		if test ! -d $$a; then continue; else true; fi; \
		b="$(DESTDIR)$(APPINSTALLDIR)/Contents/$$d"; \
		for i in $$a/*; \
		do \
			case $$i in \
			*CVS) ;; \
			*.svn) ;; \
			*.py[co]) ;; \
			*.orig) ;; \
			*~) ;; \
			*idx) \
				echo $(CPMAC) "$$i" $$b; \
				$(CPMAC) "$$i" "$$b"; \
				;; \
			*) \
				if test -d $$i; then continue; fi; \
				if test -x $$i; then \
				    echo $(INSTALL_SCRIPT) "$$i" "$$b"; \
				    $(INSTALL_SCRIPT) "$$i" "$$b"; \
				else \
				    echo $(INSTALL_DATA) "$$i" "$$b"; \
				    $(INSTALL_DATA) "$$i" "$$b"; \
				fi;; \
			esac; \
		done; \
	done
	$(INSTALL_PROGRAM) $(STRIPFLAG) $(BUILDPYTHON) "$(DESTDIR)$(APPINSTALLDIR)/Contents/MacOS/$(PYTHONFRAMEWORK)"
	sed -e "s!%bundleid%!$(PYTHONFRAMEWORKIDENTIFIER)!g" \
		-e "s!%version%!`$(RUNSHARED) $(BUILDPYTHON) \
		-c 'import platform; print(platform.python_version())'`!g" \
		< "$(DESTDIR)$(APPINSTALLDIR)/Contents/Info.plist.in" \
		> "$(DESTDIR)$(APPINSTALLDIR)/Contents/Info.plist"
	rm "$(DESTDIR)$(APPINSTALLDIR)/Contents/Info.plist.in"


install_IDLE:
	cd IDLE && make install

install_BuildApplet:
	if ! $(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) \
			-c 'import EasyDialogs' 2>/dev/null ; then \
		echo "EasyDialogs not available in this Python - skipping Build Applet.app" ; \
	else \
		$(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) $(srcdir)/scripts/BuildApplet.py \
			--destroot "$(DESTDIR)" \
			--python=$(prefix)/Resources/Python.app/Contents/MacOS/Python \
			--output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app" \
			$(srcdir)/scripts/BuildApplet.py && \
		if [ -n "$(LIPO_32BIT_FLAGS)" ] ; then \
			rm "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python" && \
			lipo $(LIPO_32BIT_FLAGS) \
				-output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python" \
				$(BUILDPYTHON) ; \
		fi \
	fi

MACLIBDEST=$(LIBDEST)/plat-mac
MACTOOLSDEST=$(prefix)/Mac/Tools
MACTOOLSSRC=$(srcdir)/Mac/Tools
MACTOOLSSUBDIRS=IDE

installmacsubtree:
	@for i in $(MACTOOLSDEST); \
	do \
		if test ! -d $(DESTDIR)$$i; then \
			echo "Creating directory $(DESTDIR)$$i"; \
			$(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$$i; \
		else	true; \
		fi; \
	done
	@for d in $(MACTOOLSSUBDIRS); \
	do \
		a=$(MACTOOLSSRC)/$$d; \
		if test ! -d $$a; then continue; else true; fi; \
		b=$(DESTDIR)$(MACTOOLSDEST)/$$d; \
		if test ! -d $$b; then \
			echo "Creating directory $$b"; \
			$(INSTALL) -d -m $(DIRMODE) $$b; \
		else	true; \
		fi; \
	done
	@for d in $(MACTOOLSSUBDIRS); \
	do \
		a=$(MACTOOLSSRC)/$$d; \
		if test ! -d $$a; then continue; else true; fi; \
		b=$(DESTDIR)$(MACTOOLSDEST)/$$d; \
		for i in $$a/*; \
		do \
			case $$i in \
			*CVS) ;; \
			*.svn) ;; \
			*.py[co]) ;; \
			*.orig) ;; \
			*~) ;; \
			*.rsrc) \
				echo $(CPMAC) $$i $$b ; \
				$(CPMAC) $$i $$b ; \
				;; \
			*) \
				if test -d $$i; then continue; fi; \
				if test -x $$i; then \
				    echo $(INSTALL_SCRIPT) $$i $$b; \
				    $(INSTALL_SCRIPT) $$i $$b; \
				else \
				    echo $(INSTALL_DATA) $$i $$b; \
				    $(INSTALL_DATA) $$i $$b; \
				fi;; \
			esac; \
		done; \
	done


	$(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) $(CACHERSRC) -v $(DESTDIR)$(MACLIBDEST) $(DESTDIR)$(MACTOOLSDEST)
	$(RUNSHARED) $(BUILDPYTHON) -Wi -tt $(compileall) -d $(MACTOOLSDEST) -x badsyntax $(DESTDIR)$(MACTOOLSDEST)
	$(RUNSHARED) $(BUILDPYTHON) -O -Wi -tt $(compileall) -d $(MACTOOLSDEST) -x badsyntax $(DESTDIR)$(MACTOOLSDEST)

$(INSTALLED_PYTHONAPP): install_Python

installextras: $(srcdir)/Extras.ReadMe.txt $(srcdir)/Extras.install.py
	$(INSTALL) -d "$(DESTDIR)$(PYTHONAPPSDIR)/Extras"
	$(INSTALL) $(srcdir)/Extras.ReadMe.txt "$(DESTDIR)$(PYTHONAPPSDIR)/Extras/ReadMe.txt"
	$(RUNSHARED) $(BUILDPYTHON) $(srcdir)/Extras.install.py $(srcdir)/../Demo \
		"$(DESTDIR)$(PYTHONAPPSDIR)/Extras/Demo"
	$(RUNSHARED) $(BUILDPYTHON) $(srcdir)/Extras.install.py $(srcdir)/Demo \
		"$(DESTDIR)$(PYTHONAPPSDIR)/Extras/Demo.Mac"


checkapplepython: $(srcdir)/Tools/fixapplepython23.py
	@if ! $(RUNSHARED) $(BUILDPYTHON) $(srcdir)/Tools/fixapplepython23.py -n; then \
		echo "* WARNING: Apple-installed Python 2.3 will have trouble building extensions from now on."; \
		echo "* WARNING: Run $(srcdir)/Tools/fixapplepython23.py with \"sudo\" to fix this."; \
	fi


clean:
	rm pythonw
	cd PythonLauncher && make clean
	cd IDLE && make clean

Makefile: $(srcdir)/Makefile.in ../config.status
	cd .. && CONFIG_FILES=Mac/Makefile CONFIG_HEADERS= $(SHELL) ./config.status
