###############################################################
#
# Makefile for Caltrain
#
# Targets:
#		all			-	build and install  
#		clean		-	clean everything
#		check		-	run self-tests
#
# This makefile uses the following environment variables:
#
#   PALMTOOLSU - path to directory where palm tools should go.
#       ex: "/usr/local/PalmTools"
#
#	
# Conventions:
#	 "ALLUPPER"  case variable names are generally exported or provided by
#				        the environment
#	 "initLower" case variables are local to the makefile
#
#
# Commonly used  Automatic Variables:
#	  $< represents the first dependency
#	  $^ represents all dependencies
#	  $? represents all dependencies that are newer than the target
#	  $@ represents the target 
#
#
###################################################################


# ====================================================
# Include the common Make variables for PalmOS executables
# ====================================================
# This one is constant
include $(PALMTOOLSU)/bin/Palm-DefaultVars.make

# This one can be modified for different build options
#  and is optional 
-include $(PALMTOOLSU)/bin/Palm-CustomVars.make



# The shell to use
SHELL		  = /bin/sh


# Compiler Flags
CFLAGS			= $(PalmCFlags) -I$(rscDir) 

# Directory paths
objDir		  = ../Obj
srcDir		  = ../Src
rscDir		  = ../Rsc
testsDir	  = ../Tests

# Target info
progName	  = Caltrain
progDBName	  = "Caltrain"
progCreator	  = CLTN
progPrcFile	  = $(progName)App.prc
progTestFile  = $(progName)Test.prc


# These are fake targets used to perform certain actions. Use the .PHONY
#  command to make sure they don't get confused with actual filenames
.PHONY : all install clean help 

# Set the default install directory if not passed down to us
#  from the parent makefile
ifndef InstallDir
#  InstallDir = $(PALMTOOLSU)/bin/Device
  InstallDir = /output
endif


#############################################################
# Master Builds
#############################################################
all: 	$(objDir)/$(progPrcFile)
	$(MAKE) install

install:   
	cp $(objDir)/$(progPrcFile) $(InstallDir)

clean:
	rm -f $(objDir)/*

check:
	cmp $(objDir)/$(progTestFile) $(testsDir)/$(progPrcFile)



#############################################################
# Implicit build rules
#############################################################
$(objDir)/%.o :  $(srcDir)/%.c
	$(CC) $(CFLAGS) -c $< -o $@




#############################################################
# Program Build
#############################################################

# ------------------------------------------------------------
# Compile objects 
# ------------------------------------------------------------
hdrList = $(srcDir)/Caltrain.h 

objList = $(objDir)/Caltrain.o

$(objDir)/Caltrain.o: $(srcDir)/Caltrain.c $(hdrList)


# ------------------------------------------------------------
# Link and combine with resources. 
# ------------------------------------------------------------
$(objDir)/$(progPrcFile): $(objList) $(rscDir)/$(progName).rcp
	$(CC) $(CFLAGS) $(objList) -o $(objDir)/$(progDBName).code.1.sym
	$(PALMRC) -rcp $(rscDir)/$(progName).rcp \
		-gccApp $(objDir)/$(progDBName).code.1.sym \
		-I $(srcDir) -I $(rscDir) \
		-o $(objDir)/$(progPrcFile) \
		-name $(progDBName) -cr $(progCreator) 

	# The $(progTestFile) PRC has zero creation and mod dates for comparison
	# purposes
	$(PALMRC) -rcp $(rscDir)/$(progName).rcp \
		-gccApp $(objDir)/$(progDBName).code.1.sym \
		-I $(srcDir) -I $(rscDir) \
		-o $(objDir)/$(progTestFile) \
		-name $(progDBName) -cr $(progCreator) \
		-zCrDate -zModDate
	ls -l $(objDir)/*.prc

