# RetroForth                                          http://www.retroforth.org
# -----------------------------------------------------------------------------
RELEASE = 24
NASM = nasm			# Name of the executable for NASM
FASM = fasm			# Name of the executable for FASM

NASM_ARGS = -felf -oretro.o	# NASM command line arguments
SDL = /usr/lib/libSDL-1.2.so.0	# Path to libSDL (or a command to locate it)
CC = gcc			# Req. for proper linking with SVGALIB & libSDL

all:
	@echo "RetroForth Release 6.$(RELEASE)"
	@echo "---------------------------------------------------------------"
	@echo "Setting up common files..."
	@make -kis setup
	@echo "FASM Targets"
	@echo "		fasm-windows-console" >ERRORLOG
	@make -kis fasm-windows-console 2>>ERRORLOG
	@echo "		fasm-linux-console" >>ERRORLOG
	@make -kis fasm-linux-console 2>>ERRORLOG
	@echo "NASM Targets"
	@echo "		nasm-linux-console" >>ERRORLOG
	@make -kis nasm-linux-console 2>>ERRORLOG
	@echo "		nasm-linux-svga" >>ERRORLOG
	@make -kis nasm-linux-svga 2>>ERRORLOG
	@echo "		nasm-linux-sdl" >>ERRORLOG
	@make -kis nasm-linux-sdl 2>>ERRORLOG
	@make -kis clean
	@echo "Any errors encountered are listed in ERRORLOG"

setup:
	@make -kis purge
	@cd ports/linux-console && cp ../forth* .
	@cd ports/linux-svga && cp ../forth* .
	@cd ports/linux-sdl && cp ../forth* .
	@cd ports/nasm-linux-console && cp ../forth* .

nasm-linux-console:
	@echo "| linux-console"
	@cd ports/nasm-linux-console && $(NASM) retro.asm $(NASM_ARGS)
	@cd ports/nasm-linux-console && ld retro.o -orf
	@cd ports/nasm-linux-console && chmod +x rf
	@cd ports/nasm-linux-console && rm -f retro.o

nasm-linux-svga:
	@echo "| linux-svga"
	@cd ports/linux-svga && $(NASM) retro.asm $(NASM_ARGS)
	@cd ports/linux-svga && gcc retro.o -lvga -orf
	@cd ports/linux-svga && chmod u+s rf
	@cd ports/linux-svga && rm -f retro.o

nasm-linux-sdl:
	@echo "| linux-sdl"
	@cd ports/linux-sdl && $(NASM) retro.asm $(NASM_ARGS)
	@cd ports/linux-sdl && gcc retro.o $(SDL) -orf
	@cd ports/linux-sdl && rm -f retro.o

fasm-linux-console:
	@echo "| linux-console"
	@cd ports/linux-console && $(FASM) retro.asm rf >~1
	@cd ports/linux-console && chmod +x rf

fasm-windows-console:
	@echo "| windows-console"
	@cd ports/windows-console && $(FASM) retro.asm rf.exe >~1

clean:
	@echo "cleaning up..."
	@echo "hack" >~1
	@find | grep \\~ >a
	@sed 's/./rm -f ./' a >b
	@. b
	@rm -f a b

purge:
	@echo "Purging source tree..."
	@make -kis clean >ERRORLOG
	@cd ports && rm -f linux-console/rf linux-sdl/rf linux-svga/rf	
	@cd ports && rm -f nasm-linux-console/rf windows-console/rf.exe
	@cd ports && rm -f linux-console/forth* linux-sdl/forth*
	@cd ports && rm -f linux-svga/forth*
	@cd ports && rm -f nasm-linux-console/forth*
	@rm -f ERRORLOG
dist:
	@echo "Generating retroforth-6.$(RELEASE).tar.gz ..."
	@make -kis purge
	@cd .. && tar cf retroforth-6.$(RELEASE).tar retroforth
	@cd .. && gzip -9 retroforth-6.$(RELEASE).tar

