# Copyright 2009 Ronald S. Burkey <info@sandroid.org>
#
# This file is part of yaAGC.
#
# yaAGC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# yaAGC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with yaAGC; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Filename:	Makefile
# Purpose:	This makefile is used to build the GUI-frontend program
#		called VirtualAGC.
# Mods:		2009-03-03 RSB	Wrote.
#		2009-03-13 RSB	Provisions for yaDSKY2 and yaDEDA2.
#		2009-03-14 RSB	For Mac OS, put yaDSKY, yaDEDA, yaDSK2,
#				yaDEDA2, and yaTelemetry into app bundles.
#				This doesn't seem to be necessary in 10.5,
#				but in 10.4 these apps don't respond to 
#				events otherwise.
#		2009-03-25 RSB	On Windows, replace VirtualAGC.zip with
#				VirtualAGC-setup.exe built by InstallJammer.
#		2009-03-26 RSB	On Linux, replaced VirtualAGC.tar.bz2 with
#				VirtualAGC-installer built by InstallJammer.
#				Actually, this is done only for USER rburkey.
#				For anyone else, you're simply left with the
#				installation directory rather than any tarball
#				or installer program created from it afterward,
#				since nobody else is likely to need to 
#				distribute it beyond the build computer.
#		2009-04-11 RSB	Tweaking Mac OS X packages.  Added Terminator
#				on Mac OS X.

APP=VirtualAGC

# For cross-platform building --- i.e., for building Win32 and Mac OS X versions
# of VirtualAGC on a Linux platform --- it is assumed that the IMCROSS
# environment (www.sandroid.org/imcross) is installed.  It is further supposed
# that the IMCROSS installation directory is ~/IMCROSS and that ~/IMCROSS/bin 
# is in the PATH.  The following variables just reflect the default setup for
# IMCROSS.
PREFIX_WIN=i386-mingw32
PREFIX_MAC=powerpc-apple-darwin9
BIN_WIN=${HOME}/IMCROSS/i386-mingw32/bin
BIN_MAC=${HOME}/IMCROSS/mac/bin
INC_MAC=${HOME}/IMCROSS/mac/include
SDK_MAC=${HOME}/IMCROSS/mac/SDKs/MacOSX10.4u.sdk
JAMMER_DIR=${HOME}/IMCROSS/installjammer

ifeq "${YADSKY_SUFFIX}" ""
YADSKY2=
else
YADSKY2=-DYADSKY2
endif
ifeq "${YADEDA_SUFFIX}" ""
YADEDA2=
else
YADEDA2=-DYADEDA2
endif

# This is just so that when I build it, it will be statically linked.  For
# anybody else, it will just accept whatever your wxWidgets default library is.
ifeq "${USER}" "rburkey"
WXSTATIC=--static
endif

.PHONY: default
default: Linux

.PHONY: all-archs
all-archs: Linux Win32 MacOsX

.PHONY: Linux
ifeq "${USER}" "rburkey"
Linux: VirtualAGC-installer
else
Linux: temp/lVirtualAGC
endif

.PHONY: Win32
Win32: VirtualAGC-setup.exe

.PHONY: MacOsX
MacOsX: VirtualAGC.app.tar.gz

.PHONY: clean
clean:
	-rm -rf temp
	-rm *~ *.bak VirtualAGC *.exe *-macosx VirtualAGC-installer
	-rm VirtualAGC.app.tar.gz VirtualAGC.tar.bz2 VirtualAGC.zip
	-rm -rf VirtualAGC-win32/output
	-rm -rf VirtualAGC-win32/build
	-rm VirtualAGC-win32/build.log
	-rm VirtualAGC-win32/temp.mpi
	-rm -rf VirtualAGC-linux/output
	-rm -rf VirtualAGC-linux/build
	-rm VirtualAGC-linux/build.log
	-rm VirtualAGC-linux/temp.mpi

SOURCES:=${APP}.cpp \
	../yaAGC/agc_utilities.c \
	../yaAGC/DecodeDigitalDownlink.c \
	../yaAGC/agc_engine.c \
	../yaAGC/Backtrace.c \
	../yaAGC/random.c \
	../yaAGC/SocketAPI.c

HEADERS:=${APP}.h

${APP}: ${SOURCES} ${HEADERS}
	g++ \
		${YADSKY2} ${YADEDA2} \
		`wx-config ${WXSTATIC} --cxxflags` \
		-o $@ ${SOURCES} \
		`wx-config ${WXSTATIC} --libs`
	strip $@

${APP}.exe: ${SOURCES} ${HEADERS}
	${PREFIX_WIN}-g++ \
		${YADSKY2} ${YADEDA2} \
		`${BIN_WIN}/wx-config --static --cxxflags` \
		-o $@ ${SOURCES} \
		`${BIN_WIN}/wx-config --static --libs`
	${PREFIX_WIN}-strip $@

${APP}-macosx: ${SOURCES} ${HEADERS}
	${PREFIX_MAC}-g++ \
		${YADSKY2} ${YADEDA2} \
		-arch ppc -arch i386 -I${INC_MAC} -isysroot ${SDK_MAC} \
		`${BIN_MAC}/wx-config --static --cxxflags` \
		-o $@ ${SOURCES} \
		`${BIN_MAC}/wx-config --static --libs`
	${PREFIX_MAC}-strip $@

temp/lVirtualAGC/bin temp/VirtualAGC/bin temp/VirtualAGC.app/Contents/MacOS:
	mkdir -p $@

# This target creates a Resources directory for any of the installation
# packages.  Except for gtk+ related stuff, none of the contents of the
# Resources directory vary by target.
temp/lVirtualAGC/Resources temp/VirtualAGC/Resources temp/VirtualAGC.app/Contents/Resources:
	mkdir -p $@/pixmaps $@/source $@
	mkdir $@/source/Colossus249
	cp -a ../scenarios $@
	cp ../Colossus249/*.binsource ../Colossus249/*.s $@/source/Colossus249
	./lst2html.sh ../Colossus249/Colossus249.lst $@/source/Colossus249/Colossus249.html
	mkdir $@/source/Luminary131
	cp ../Luminary131/*.binsource ../Luminary131/*.s $@/source/Luminary131
	./lst2html.sh ../Luminary131/Luminary131.lst $@/source/Luminary131/Luminary131.html
	mkdir $@/source/Artemis072
	cp ../Artemis072/*.binsource ../Artemis072/*.s $@/source/Artemis072
	mkdir $@/source/Validation
	cp ../Validation/*.s $@/source/Validation
	./lst2html.sh ../Validation/Validation.lst $@/source/Validation/Validation.html
	mkdir $@/source/FP6
	cp ../FP6/*.s $@/source/FP6
	./lst2html.sh ../FP6/FP6.lst $@/source/FP6/FP6.html
	mkdir $@/source/FP8
	cp ../FP8/*.s $@/source/FP8
	./lst2html.sh ../FP8/FP8.lst $@/source/FP8/FP8.html
	cp -a *.png $@
	cp -a ../yaDSKY/src/*.ini $@
ifeq "${YADSKY_SUFFIX}" ""	
	cp -a ../yaDSKY/pixmaps $@/pixmaps/yaDSKY
else
	cp -a ../yaDSKY2/*.jpg $@
endif	
	-rm -rf $@/pixmaps/yaDSKY/CVS $@/pixmaps/yaDSKY/.xvpics
ifeq "${YADEDA_SUFFIX}" ""	
	cp -a ../yaDEDA/pixmaps $@/pixmaps/yaDEDA
else
	cp -a ../yaDEDA2/*.xpm $@
endif
	cp -a ../jWiz/*.jpg $@
	-rm -rf $@/pixmaps/yaDEDA/CVS $@/pixmaps/yaDEDA/.xvpics
	cp -a ../yaTelemetry/*.jpg $@
	cp -a ../Artemis072/Artemis072.bin $@/source/Artemis072
	cp -a ../Colossus249/Colossus249.bin $@/source/Colossus249
	cp -a ../Colossus249/Colossus249.symtab $@/source/Colossus249/Colossus249.bin.symtab
	cp -a ../Luminary131/Luminary131.bin $@/source/Luminary131
	cp -a ../Luminary131/Luminary131.symtab $@/source/Luminary131/Luminary131.bin.symtab
	cp -a ../Validation/Validation.bin $@/source/Validation
	cp -a ../Validation/Validation.s.symtab $@/source/Validation/Validation.bin.symtab
	cp -a ../FP6/FP6.bin $@/source/FP6
	cp -a ../FP6/FP6.symtab $@/source/FP6/FP6.bin.symtab
	cp -a ../FP8/FP8.bin $@/source/FP8
	cp -a ../FP8/FP8.symtab $@/source/FP8/FP8.bin.symtab
	cp -a ../Contributed/LM_Simulator $@
	cp -a VirtualAGC.tcl $@
	-rm -rf $@/LM_Simulator/CVS
	-rm -rf $@/LM_Simulator/modules/CVS
	-rm -rf $@/LM_Simulator/doc/CVS
	-rm -rf $@/LM_Simulator/Documentation/CVS
	cp template.app/Contents/Resources/VirtualAGC.icns $@

# The targets below create a bin directory for the distribution
# packages.  All of the contents of the bin directory vary by
# target architecture.

EXECUTABLES:=\
	VirtualAGC \
	../yaAGC/yaAGC \
	../yaAGS/yaAGS \
	../yaACA3/yaACA3 \
	../yaYUL/yaYUL \
	../Luminary131/bdiffhead \
	../Luminary131/CheckDec \
	../Luminary131/Oct2Bin \
	../Luminary131/webb2burkey-rope \
	../yaLEMAP/yaLEMAP \
	../yaLEMAP/binLEMAP \
	../yaUniverse/yaUniverse \
	../ControlPulseSim/ControlPulseSim \
	../yaTelemetry/yaTelemetry \
	../jWiz/jWiz
ifeq "${YADSKY_SUFFIX}" ""
EXECUTABLES+=../yaDSKY/src/yaDSKY
else
EXECUTABLES+=../yaDSKY2/yaDSKY2
endif
ifeq "${YADEDA_SUFFIX}" ""
EXECUTABLES+=../yaDEDA/src/yaDEDA
else
EXECUTABLES+=../yaDEDA2/yaDEDA2
endif

temp/lVirtualAGC : % : VirtualAGC %/bin %/Resources
	cp -a ${EXECUTABLES} $@/bin
	-cp -a ../yaACA/yaACA ../yaACA2/yaACA2 $@/bin
	cp ApolloPatch2-transparent.png $@/bin/ApolloPatch2.png
	for n in $@/bin/* ; do strip $$n ; done
	cp -a ../SimStop $@/bin
	chmod +x $@/bin/SimStop

temp/VirtualAGC : % : VirtualAGC.exe %/bin %/Resources
	for n in ${EXECUTABLES} ../yaAGC/WinAGC ; do cp $$n.exe $@/bin ; done
	#-cp -a ../yaACA/yaACA.exe ../yaACA/yaACA-cfg.exe $@/bin
	cp -a ../yaACA2/yaACA2.exe $@/bin
	cp VirtualAGC-win32/ApolloPatch2.ico $@/bin
	for n in $@/bin/* ; do i386-mingw32-strip $$n ; done
ifeq "$YADSKY2" ""	
	cp ${HOME}/IMCROSS/i386-mingw32/bin/*.dll $@/Resources
endif
ifeq "$YADEDA2" ""	
	cp ${HOME}/IMCROSS/i386-mingw32/bin/*.dll $@/Resources
endif
	-mkdir $@/Resources/etc
	cp -a ${HOME}/IMCROSS/i386-mingw32/etc/gtk-2.0 $@/Resources/etc

.PHONY: create-bundle
create-bundle:
	-rm -rf temp/VirtualAGC.app
	mkdir -p temp/VirtualAGC.app/Contents
	cp -a template.app/Contents/Info.plist temp/VirtualAGC.app/Contents

# The Mac application tarball requires a file called Terminator.app.tar.bz2 in the 
# parent directory of yaAGC.  This file is obtained by downloading terminator.dmg
# from http://software.jessies.org/terminator/, installing it in Mac OS X to 
# create Terminator.app, and then making a tarball from Terminator.app.  Since the
# binary is not changed from what the supplier provides, the GPL requires merely
# that I provide a link to the source tarball at the supplier's site.
temp/VirtualAGC.app : % : VirtualAGC-macosx create-bundle %/Contents/MacOS %/Contents/Resources
	for n in ${EXECUTABLES} ; do cp $$n-macosx $@/Contents/MacOS ; done
	-cp -a ../yaACA/yaACA-macosx $@/Contents/MacOS
	cd $@/Contents/MacOS ; \
	for n in *-macosx ; \
	do \
		mv $$n `echo $$n | sed "s/-macosx//"` ; \
	done
	for n in yaDSKY2 yaDEDA2 yaTelemetry jWiz ; \
	do \
	  cp -a template.app $@/Contents/MacOS/$$n.app ; \
	  mv $@/Contents/MacOS/$$n.app/Contents/Resources/VirtualAGC.icns $@/Contents/MacOS/$$n.app/Contents/Resources/$$n.icns ; \
	  sed --in-place "s/VirtualAGC/$$n/" $@/Contents/MacOS/$$n.app/Contents/Info.plist ; \
	  mv $@/Contents/MacOS/$$n $@/Contents/MacOS/$$n.app/Contents/MacOS ; \
	done
	tar -C $@/Contents/MacOS -xjf ../../Terminator.app.tar.bz2
	cp -a ../SimStop $@/Contents/MacOS
	chmod +x $@/Contents/MacOS/SimStop
ifeq "$YADSKY2" ""	
	-cp ${HOME}/IMCROSS/mac/lib/*.dylib $@/Contents/MacOS
endif
ifeq "$YADEDA2" ""	
	-cp ${HOME}/IMCROSS/mac/lib/*.dylib $@/Contents/MacOS
endif

# No longer used.  Replaced by VirtualAGC-installer
VirtualAGC.tar.bz2: temp/lVirtualAGC
	tar -C temp -cjf $@ lVirtualAGC

VirtualAGC-installer: temp/lVirtualAGC
	sed "s/APPLICATION_VERSION/${NVER}/" VirtualAGC-linux/VirtualAGC-linux.mpi >VirtualAGC-linux/temp.mpi
	cd VirtualAGC-linux && \
	${JAMMER_DIR}/installjammer --build temp.mpi
	cp -p VirtualAGC-linux/output/Virtual* $@
	rm -rf VirtualAGC-linux/build VirtualAGC-linux/output

# No longer used.  Replaced by VirtualAGC-setup.exe
VirtualAGC.zip: temp/VirtualAGC
	cd temp && zip -r ../$@ VirtualAGC

VirtualAGC-setup.exe: temp/VirtualAGC
	sed "s/APPLICATION_VERSION/${NVER}/" VirtualAGC-win32/VirtualAGC-win32.mpi >VirtualAGC-win32/temp.mpi
	cd VirtualAGC-win32 && \
	${JAMMER_DIR}/installjammer --build temp.mpi
	cp -p VirtualAGC-win32/output/*-Setup.exe $@
	rm -rf VirtualAGC-win32/build VirtualAGC-win32/output

VirtualAGC.app.tar.gz: temp/VirtualAGC.app
	tar -C temp -czf $@ VirtualAGC.app

