#
#	mySTEP Makefile
#
#	Author:	Felipe A. Rodriguez <far@pcmagic.net>
#	Date:	January 1999
#
#	adapted for mySTEP by H. Nikolaus Schaller <hns@computer.org>, June 2003
#
###############################################################################

include Version.def

TAR := gnutar

MYSTEP_VERSION = $(MAJOR_VERSION).$(MINOR_VERSION)

CONFIGURE = Makefiles/configs/configure

# specify in order to permit proper merging into shared libraries of shared libraries

FRAMEWORKS= Foundation/DO \
			Foundation/Source \
			AppKit/Source/xraw \
			AppKit/Model \
			AppKit/Source \
			SystemStatus/Source \
			Cocoa/Source \
			PreferencePanes/Source \
			SystemUIPlugin/Source \
			AddressBook/Source \
			SimpleWebKit/Source \
			Message/Source \

# INSTALL_PATH = /usr/mGSTEP/lib

no_target::	
	@(if [ -f Makefiles/configs/config.make ]; then \
		$(MAKE) all; \
	else \
		echo "##"; \
		echo "##  mySTEP is not yet configured on this machine and you"; \
		echo "##  did not specify a build target.  The supported targets"; \
		echo "##  are (linux, freebsd, and winnt) i386 machines, "; \
		echo "##  (arm-linux) for ARM and (macosx) for PowerPC."; \
		echo "##" ; \
		if [ -f /bin/bash -a "$(SHELL)" == "/bin/sh" ]; then \
			$(SHELL) Makefiles/configs/configure; \
		else \
		echo "##  Enter: make <target>  (choosing one of the above targets).";\
		echo "##" ; \
		fi \
	fi)


config::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION))

all::	
	- for i in $(FRAMEWORKS); \
	do \
		( cd $$PWD/$$i; pwd; $(MAKE) all; ) \
	done 

tests::
	cd Foundation/Testing;  	$(MAKE) all 
	cd AppKit/Testing;      	$(MAKE) all 

install::
	@(echo "## Run: make install as ROOT ##.";) 
	@(echo "##"; echo "## Installing mySTEP to $(INSTALL_PATH)"; echo "##")
	$(MAKE) -f Makefiles/install.make install INSTALL_PATH=$(INSTALL_PATH)

clean::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) ARM-Linux)
	- for i in $(FRAMEWORKS); \
	do \
		(cd "$$PWD/$$i" && pwd &&  $(MAKE) clean;) \
	done 
	cd Foundation/Testing;  $(MAKE) clean;	$(MAKE) distclean	
	cd AppKit/Testing;      $(MAKE) clean

distclean::
	$(MAKE) clean
	- rm -f Makefiles/configs/config.make
	- rm -f Foundation/Headers/Foundation/config.h
	- rm -f *.ipk
	- rm -rf *.pkg
	- find . '(' -name build -o -name CVS ')' -prune -exec rm -rf {} ';'

linux::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) Linux)
	$(MAKE) all

freebsd::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) FreeBSD3)
	$(MAKE) all

winnt::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) WinNT)
	$(MAKE) all

#
# the following is added by hns@computer.org
# it is for cross-compiling for the Sharp Zaurus on MacOS X
#
 
TOOLS=/Developer/Zaurus-X-gcc/opt/Embedix/tools/arm-linux
SDK=/Developer/Zaurus-X-gcc/SDKs/mySTEP.sdk
PWD=`pwd`

Makefiles/configs/config.make:	Makefile Makefiles/configs/ARM-Linux
	$(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) ARM-Linux

arm-linux::	Makefiles/configs/config.make
	- mv $(SDK)/usr/mySTEP/lib/libobjc.so $(SDK)/usr/mySTEP # save if existent (may fail)
	- rm -r $(SDK)/usr/mySTEP/include $(SDK)/usr/mySTEP/lib
	- mkdir $(SDK)/usr/mySTEP/include $(SDK)/usr/mySTEP/lib
	- mv $(SDK)/usr/mySTEP/libobjc.so $(SDK)/usr/mySTEP/lib # restore (may fail)
	# publish headers for cross-compilation - but preserve modification date to keep make-dependencies working
	- for i in $(FRAMEWORKS); \
	do \
		[ $$i != 'AppKit/Source/xraw' ] && cp -Rp $$PWD/$$i/../Headers/* $(SDK)/usr/mySTEP/include; \
	done
	- rm -r $(SDK)/usr/mySTEP/include/Private
	# compile everything
	$(MAKE) all
	# publish compiled libraries after making
	- for i in $(FRAMEWORKS); \
	do \
		cp $$PWD/$$i/*/lib*.so $(SDK)/usr/mySTEP/lib; \
	done

macosx::	
	@($(SHELL) $(CONFIGURE) $(MAJOR_VERSION) $(MINOR_VERSION) MacOSX)
	$(MAKE) all

bz2::	
	@(echo "##"; echo "## Creating bz2 distribution package"; echo "##")
	cp /Developer/Zaurus-X-gcc/SDKs/mySTEP.sdk/usr/mySTEP/etc/cross-makefile AppKit/Model		# make a backup of the latest script
	cd ..; $(TAR) cf /tmp/mySTEP-$(MYSTEP_VERSION).tar mySTEP
	[ -f /tmp/mySTEP-$(MYSTEP_VERSION).tar.bz2 ] && rm /tmp/mySTEP-$(MYSTEP_VERSION).tar.bz2
	bzip2 /tmp/mySTEP-$(MYSTEP_VERSION).tar

src.gz::	distclean
	@(echo "##"; echo "## Creating gz distribution package"; echo "##")
	cp /Developer/Zaurus-X-gcc/SDKs/mySTEP.sdk/usr/mySTEP/etc/cross-makefile AppKit/Model		# make a backup of the latest script
	cd ..; $(TAR) cf packages/mySTEP-src-$(MYSTEP_VERSION)B$(BUILD_NUMBER).tar \
		mySTEP/AddressBook \
		mySTEP/AppKit \
		mySTEP/Cocoa \
		mySTEP/Foundation \
		mySTEP/Makefile \
		mySTEP/Makefiles \
		mySTEP/Message \
		mySTEP/mySTEP.xcode \
		mySTEP/PreferencePanes\
		mySTEP/SimpleWebKit \
		mySTEP/SystemStatus \
		mySTEP/SystemUIPlugin \
		mySTEP/Version.def \
		mySTEP/WindowManager \
		mySTEP/HISTORY.rtf \
		'mySTEP/LGPL LICENCE.txt' \
		mySTEP/README \
		
	rm -f ../packages/mySTEP-src-$(MYSTEP_VERSION)B$(BUILD_NUMBER).tar.gz
	gzip ../packages/mySTEP-src-$(MYSTEP_VERSION)B$(BUILD_NUMBER).tar; wait

TOOLCHAIN=/Developer/Zaurus-X-gcc/opt/Embedix/tools/arm-linux/bin
IPK=/tmp/mySTEP-$(MAJOR_VERSION)_$(MINOR_VERSION)/files

ipk:: # arm-linux tests
	@(echo "##"; echo "## Creating .ipk installation package"; echo "##")
	-rm -rf $(IPK)
	#
	# control files
	#
	mkdir -p $(IPK)
	(echo "2.0") >$(IPK)/debian-binary
	(echo "#!/bin/sh"; \
		echo "# here we should check for removeable media destination and ln -s /medium/home/myPDA /home/myPDA" ;\
		) >$(IPK)/preinst
	(echo "##"; \
		echo "Package: mySTEP"; \
		echo "Section: x11"; \
		echo "#Depends: "; \
		echo "Priority: optional"; \
		echo "Version: $(MYSTEP_VERSION)"; \
		echo "Maintainer: H. Nikolaus Schaller <hns@computer.org>"; \
		echo "Author: Felipe A. Rodriguez <far@pcmagic.net>, H. Nikolaus Schaller <hns@computer.org>, others (from GNUstep)"; \
		echo "Licence: LGPL"; \
		echo "Architecture: arm"; \
		echo "Description: Foundation, AppKit, PreferencePanes, WebKit, AddressBook libraries."; \
		echo "##") >$(IPK)/control
	(echo "#!/bin/sh"; \
		echo "chown root /home/myPDA/bin/gdomap" ;\
		echo "chmod u+s /home/myPDA/bin/gdomap" ;\
		) >$(IPK)/postinst
	$(TAR) cvf $(IPK)/control.tar --owner 500 --group 1 -C $(IPK) ./control ./preinst ./postinst
	gzip $(IPK)/control.tar; wait
	#
	# data files
	#
	#
	# mySTEP libraries
	#
	mkdir -p $(IPK)/home/myPDA/lib
	- for i in $(FRAMEWORKS); \
	do \
		cp $$i/*/lib*.so $(IPK)/home/myPDA/lib; \
	done
	# special files
	mkdir -p $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/CS
	#mkdir -p $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/TZ
	cp Foundation/NSCharacterSets/*.dat $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/CS
	#cp -R Foundation/NSTimeZones/NSTimeZones/abbreviations $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/TZ
	#cp -R Foundation/NSTimeZones/NSTimeZones/regions $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/TZ
	#cp -R Foundation/NSTimeZones/NSTimeZones/zones $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/TZ/zones
	cp Foundation/Source/Info.plist $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents
	mkdir -p $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Images
	mkdir -p $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Panels
	(cd Appkit/Images && tar czf - *.tiff) | (cd $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Images && tar xvzf -)
	(cd Appkit/Panels && tar czf - *.lproj/*.mib) | (cd $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Panels && tar xvzf -)
#	cp Appkit/Images/*.tiff $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Images
#	cp Appkit/Panels/*/*.mib $(IPK)/home/myPDA/System/Library/Sys.bundle/Contents/Resources/Panels
	#
	# distributed object server and other tools
	#
	mkdir -p $(IPK)/home/myPDA/bin
	cp Foundation/DO/gdomap $(IPK)/home/myPDA/bin
	cp Foundation/Source/defaults $(IPK)/home/myPDA/bin
	cp AppKit/Testing/run $(IPK)/home/myPDA/bin
	cp AppKit/Testing/debug $(IPK)/home/myPDA/bin
	#
	# strip all binaries
	#
	- $(TOOLCHAIN)/strip $(IPK)/home/myPDA/bin/*
	#
	# add testing functions
	#
	mkdir -p $(IPK)/home/myPDA/Applications/Testing
	- ( for i in Foundation/Testing/*; \
		do \
		[ ! -d $$i -a -x $$i ] && cp -RP $$i $(IPK)/home/myPDA/Applications/Testing; \
		done )
#	- $(TOOLCHAIN)/strip $(IPK)/home/myPDA/Applications/Testing/*
#	- cp -R Foundation/Testing/*.bundle $(IPK)/home/myPDA/Applications/Testing
#	- cp -R AppKit/Testing/*.app $(IPK)/home/myPDA/Applications/Testing
	#
	# clean up MacOS specific files and directories
	#
	find $(IPK) \( -name $$'Icon\r' -or -name '.DS_Store' -or -name 'CVS' -or -name '*.nib' -or -path '*/Contents/MacOS' -or -name pbdevelopment.plist \) -prune -print -exec rm -r {} \;
	#
	# make data.tar.gz
	#
	$(TAR) cf $(IPK)/data.tar --exclude '._*' --owner 500 --group 1 -C $(IPK) ./home
	gzip $(IPK)/data.tar; wait
	$(TAR) tzf $(IPK)/data.tar.gz	# list back for controlling purposes
	#
	# make .ipk
	#
	$(TAR) cf ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm --owner 500 --group 1 -C $(IPK) ./control.tar.gz ./data.tar.gz ./debian-binary
	rm -f ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm.ipk
	gzip -S .ipk ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm; wait
	# just list again
	ls -l ../packages/mySTEP_*
	$(TAR) tvzf ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm.ipk

# send .ipk to Zaurus for installation
send2zaurus:: ipk
	$(TAR) cf - --owner 500 --group 1 ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm.ipk | \
		ssh -e none -l root $(ZAURUS) sh -c "cd;cd /mnt/card/Documents/application/ipkg && tar xvf -" 
	date	# as confirmation

# send libraries and some binaries directly to Zaurus

patch2zaurus:: arm-linux ipk
	# send files
	$(TAR) xzf ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm.ipk --to-stdout ./data.tar.gz | ssh -l root $(ZAURUS) sh -c "cd; cd / && tar xvzf -"
	# run postinst script
	$(TAR) xzf ../packages/mySTEP_$(MAJOR_VERSION).$(MINOR_VERSION)_arm.ipk --to-stdout ./control.tar.gz | $(TAR) xzf - --to-stdout ./postinst | ssh -l root $(ZAURUS) sh -c "cd /home/myPDA; sh"
	date	# done