###############################################################################
# Free42 -- an HP-42S calculator simulator
# Copyright (C) 2004-2009  Thomas Okken
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# This program 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 this program; if not, see http://www.gnu.org/licenses/.
###############################################################################

CFLAGS = -MMD -Wall -g -DPALMOS
#CFLAGS = -MMD -Wall -g -DPALMOS -save-temps
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
LDFLAGS = -g

SRCS = shell1.cc shell2.cc shell_spool.cc myglue.cc filesys.cc memmgr.c \
	core_main.cc core_commands1.cc core_commands2.cc core_commands3.cc \
	core_commands4.cc core_commands5.cc core_commands6.cc core_display.cc \
	core_globals.cc core_helpers.cc core_keydown.cc core_linalg1.cc \
	core_linalg2.cc core_math1.cc core_math2.cc core_phloat.cc \
	core_sto_rcl.cc core_tables.cc core_variables.cc
OBJS = shell1.o shell2.o shell_spool.o myglue.o filesys.o memmgr.o \
	core_main.o core_commands1.o core_commands2.o core_commands3.o \
	core_commands4.o core_commands5.o core_commands6.o core_display.o \
	core_globals.o core_helpers.o core_keydown.o core_linalg1.o \
	core_linalg2.o core_math1.o core_math2.o core_phloat.o \
	core_sto_rcl.o core_tables.o core_variables.o

ifdef BCD_MATH
CXXFLAGS += -DBCD_MATH
SRCS += bcdfloat.cc bcd.cc bcdmath.cc bcdfloat2.cc
OBJS += bcdfloat.o bcd.o bcdmath.o bcdfloat2.o
SHELL_DEF = shell_dec.def
TARGET = free42dec_68k.prc
else
OBJS += MathLib.o
SHELL_DEF = shell_bin.def
TARGET = free42bin_68k.prc
endif

$(TARGET): shell shell.ro Original.prc
	build-prc -o $(TARGET) $(SHELL_DEF) shell shell.ro Original.prc

$(SRCS): symlinks

symlinks:
	ln -s ../common/free42.h
	ln -s ../common/core_commands1.cc
	ln -s ../common/core_commands1.h
	ln -s ../common/core_commands2.cc
	ln -s ../common/core_commands2.h
	ln -s ../common/core_commands3.cc
	ln -s ../common/core_commands3.h
	ln -s ../common/core_commands4.cc
	ln -s ../common/core_commands4.h
	ln -s ../common/core_commands5.cc
	ln -s ../common/core_commands5.h
	ln -s ../common/core_commands6.cc
	ln -s ../common/core_commands6.h
	ln -s ../common/core_display.cc
	ln -s ../common/core_display.h
	ln -s ../common/core_globals.cc
	ln -s ../common/core_globals.h
	ln -s ../common/core_helpers.cc
	ln -s ../common/core_helpers.h
	ln -s ../common/core_keydown.cc
	ln -s ../common/core_keydown.h
	ln -s ../common/core_math1.cc
	ln -s ../common/core_math1.h
	ln -s ../common/core_math2.cc
	ln -s ../common/core_math2.h
	ln -s ../common/core_main.cc
	ln -s ../common/core_main.h
	ln -s ../common/core_linalg1.cc
	ln -s ../common/core_linalg1.h
	ln -s ../common/core_linalg2.cc
	ln -s ../common/core_linalg2.h
	ln -s ../common/core_phloat.cc
	ln -s ../common/core_phloat.h
	ln -s ../common/core_sto_rcl.cc
	ln -s ../common/core_sto_rcl.h
	ln -s ../common/core_tables.cc
	ln -s ../common/core_tables.h
	ln -s ../common/core_variables.cc
	ln -s ../common/core_variables.h
	ln -s ../common/shell.h
	ln -s ../common/shell_spool.cc
	ln -s ../common/shell_spool.h
	ln -s ../common/bcd.cc
	ln -s ../common/bcd.h
	ln -s ../common/bcdfloat.cc
	ln -s ../common/bcdfloat.h
	ln -s ../common/bcdmath.cc
	ln -s ../common/bcdmath.h
	ln -s ../common/bcd2.h
	ln -s ../common/bcdfloat2.cc
	ln -s ../common/bcdfloat2.h
	touch symlinks

shell-sections.s shell-sections.ld: $(SHELL_DEF)
	m68k-palmos-multigen -b shell-sections $(SHELL_DEF)

.c.o:
	m68k-palmos-gcc $(CFLAGS) -c -o $@ $<

.cc.o:
	m68k-palmos-g++ $(CXXFLAGS) -c -o $@ $<

shell: $(OBJS) shell-sections.s shell-sections.ld
	m68k-palmos-g++ $(LDFLAGS) -o shell $(OBJS) \
		-lnoexcept -lPalmOSGlue \
		shell-sections.s shell-sections.ld
	
shell.rcp: shell.rcp.in
	sed "s/%VERSION%/$(shell cat ../VERSION)/g" shell.rcp.in > shell.rcp

shell.ro: shell.rcp
	pilrc -MD -q -allowEditID -ro -o shell.ro shell.rcp

Original.prc: skin2prc
	sh make-skin ../palmskins/Original.layout \
		../palmskins/Original.gif 100 500 1 72 Original off 1

Reverse.prc: skin2prc
	sh make-skin ../palmskins/Reverse.layout \
		../palmskins/Reverse.gif 1000 2000 1 72 Reverse off 1

Standard.prc: skin2prc
	sh make-skin ../palmskins/Standard.layout \
		../palmskins/Standard.gif 1000 2000 1,4,8 72 Standard off 1

StandardHD.prc: skin2prc
	sh make-skin ../palmskins/StandardHD.layout \
		../palmskins/StandardHD.gif 1000 2000 1,4,8 144 StandardHD off 2

SemiReal42.prc: skin2prc
	sh make-skin ../palmskins/SemiReal42.layout \
		../palmskins/SemiReal42.gif 1000 2000 1,4,8 72 SemiReal42 off 1

SemiReal42HD.prc: skin2prc
	sh make-skin ../palmskins/SemiReal42HD.layout \
		../palmskins/SemiReal42HD.gif 1000 2000 1,4,8 144 SemiReal42HD off 2

SemiAuto42HD.prc: skin2prc
	sh make-skin ../palmskins/SemiAuto42HD.layout \
		../palmskins/SemiAuto42HD.gif 1000 2000 1,4,8 144 SemiAuto42HD off 2

Realism.prc: skin2prc
	sh make-skin ../palmskins/Realism.layout \
		../palmskins/Realism.gif 1000 2000 1,4,8 144 Realism off 2

Ehrling42sm.prc: skin2prc
	sh make-skin ../palmskins/Ehrling42sm.layout \
		../palmskins/Ehrling42sm.gif 1000 2000 1,4,8 144 Ehrling42sm off 2

Free42PalmOSSkins.zip: Reverse.prc Standard.prc StandardHD.prc SemiReal42.prc SemiReal42HD.prc SemiAuto42HD.prc Realism.prc Ehrling42sm.prc
	rm -f Free42PalmOSSkins.zip
	zip Free42PalmOSSkins.zip Reverse.prc Standard.prc StandardHD.prc \
		SemiReal42.prc SemiReal42HD.prc SemiAuto42HD.prc \
		Realism.prc Ehrling42sm.prc

skin2prc: skin2prc.cc skin2prc.h
	c++ -o skin2prc skin2prc.cc

clean:
	rm -f `ls *.prc | grep -v '^free42'` \
		shell shell.rcp shell.ro armcore armcore.ro *.o *.i *.ii \
		*.s *.d *.out *.grc shell.gdb `find . -type l` \
		shell-sections.s shell-sections.ld symlinks* \
		skin2prc skin2prc.exe tmp_skin.*

cleaner:
	rm -f *.prc shell shell.rcp shell.ro armcore armcore.ro *.o *.i *.ii \
		*.s *.d *.out *.grc shell.gdb `find . -type l` \
		shell-sections.s shell-sections.ld symlinks* \
		skin2prc skin2prc.exe tmp_skin.* Free42PalmOSSkins.zip

-include $(patsubst %.cc,%.d,$(patsubst %.c,%.d,$(SRCS)))
-include MathLib.d
