TARGET := apple2

GENERAL_CFLAGS := \
	-DSURL_TO_LANGCARD \
	--asm-define SURL_TO_LANGCARD \
	--asm-define SERIAL_ENABLE_IRQ \
	--asm-define SERIAL_LOW_LEVEL_CONTROL \
	-DDYN_BOX_DRAW -DAPPLE2_INCLUDE_IIE_CHARS \

FLOP_NAME = QUICKTAKE
CLEANDISK = ../../disks/ProDOS_2_4_3.po

ifdef DEBUG_HD
debug_CFLAGS := $(debug_CFLAGS) -DDEBUG_HD=$(DEBUG_HD) --asm-define DEBUG_HD=1
endif

ifdef DEBUG_FLOPPY
debug_CFLAGS := $(debug_CFLAGS) -DDEBUG_FLOPPY=$(DEBUG_FLOPPY)
endif

VERSION := $(shell grep define.VERSION ../surl-server/surl_protocol.h |cut -d'"' -f2)

slowtake_SOURCES := \
	qt-menu.c \
	qt-serial.c \
	qt-callbacks.s \
	qt1x0-serial.c \
	qt200-serial.c \
	qt-edit-image.c \
	qt-state.c \
	../lib/dget_text_single.c \
	../lib/strtrim.c \
	../lib/progress_bar.c \
	../lib/hgr_addrs.c

slowtake_CC65SOURCES := \
	qt-dither-common.s \
	qt-dither-horiz.s \
	qt-dither-vert.s \
	../lib/serial/asm/driver.s \
	../lib/serial/asm/acia.s \
	../lib/serial/asm/z8530.s \
	../lib/serial/asm/simple_serial.s \
	../lib/serial/asm/simple_serial_io.s \
	../lib/serial/asm/simple_serial_read.s \
	../lib/serial/asm/simple_serial_configure.s \
	../lib/fastirq.s \
	../lib/fastirq-iigs.s \
	../lib/a2_features.s \
	../lib/asm/scrollwindow.s \
	../lib/asm/path_helper.s \
	../lib/asm/malloc0.s \
	../lib/asm/hgr.s \
	../lib/asm/reserve_auxhgr.s \
	../lib/asm/auxhgr_file.s \
	../lib/asm/unlink_auxhgr.s \
	../lib/asm/backup_screen_holes.s \
	../lib/asm/file_select.s \
	../lib/dputc.s \
	../lib/scroll.s \
	../lib/FVTABZ.s \
	../lib/asm/clrzone.s \
	../lib/platform_sleep.s \
	../lib/extrazp.s \
	../lib/runtime_once_clean.s \
	../lib/a2_features.s \

slowtake_GCCSOURCES := \
	../lib/serial/c/simple_serial.c \
	../lib/serial/c/simple_serial_io.c \
	../lib/serial/c/serial_control.c \
	../lib/c/scrollwindow.c \
	../lib/c/malloc0.c \
	../lib/c/hgr.c \
	../lib/c/file_select.c \
	../lib/tgi_sdl.c \
	../lib/tgi_fastline.c

qtktconv_SOURCES := \
	qt-conv.c \
	../lib/progress_bar.c

qtktconv_CC65SOURCES := \
	qtkt.s \
	scaler.s \
	../lib/asm/reserve_auxhgr.s \
	../lib/asm/auxhgr_file.s \
	../lib/asm/unlink_auxhgr.s \
	../lib/asm/path_helper.s \
	../lib/extrazp.s \
	../lib/a2_features.s \
	../lib/check_floppy.s

qtktconv_GCCSOURCES := \
	qtkt.c \
	scaler.c

qtknconv_SOURCES := \
	qt-conv.c \
	qtkn.c \
	../lib/progress_bar.c

qtknconv_CC65SOURCES := \
	../lib/square_table.s \
	qtk_bithuff.s \
	qtkn_platform.s \
	scaler.s \
	../lib/asm/reserve_auxhgr.s \
	../lib/asm/auxhgr_file.s \
	../lib/asm/unlink_auxhgr.s \
	../lib/extrazp.s \
	../lib/asm/path_helper.s \
	../lib/mult8x8x16.s \
	../lib/mult16x16mid16.s \
	../lib/approxdiv16x8.s \
	../lib/a2_features.s \
	../lib/check_floppy.s
	
qtknconv_GCCSOURCES := \
	qtk_bithuff.c \
	qtkn_platform.c \
	scaler.c

jpegconv_SOURCES := \
	qt-conv.c \
	jpeg.c \
	../lib/progress_bar.c

jpegconv_CC65SOURCES := \
	../lib/square_table.s \
	jpeg_arrays.s \
	jpeg_platform.s \
	scaler.s \
	../lib/asm/reserve_auxhgr.s \
	../lib/asm/auxhgr_file.s \
	../lib/asm/unlink_auxhgr.s \
	../lib/asm/path_helper.s \
	../lib/extrazp.s \
	../lib/a2_features.s \
	../lib/check_floppy.s

jpegconv_GCCSOURCES := \
	jpeg_platform.c \
	scaler.c

GCCSOURCES := \
	../lib/extended_conio.c \
	../lib/c/path_helper.c \
	../lib/c/clrzone.c

DEPS :=

CFLAGS  = -I ../lib -I . -DCONF_FILE_PATH=\"/etc/a2tools/quicktake.conf\"

CLCFLAGS = -t $(TARGET) -O -Cl -Wl -D,__STACKSIZE__=0x200 $(GENERAL_CFLAGS) $(debug_CFLAGS) \

slowtake_CLCFLAGS = $(CLCFLAGS) \
	-DPROGRAM_NAME='"SLOWTAKE"' \
	-DBUFFER_SIZE=2048  --asm-define BUFFER_SIZE=2048 \
	-DBAND_HEIGHT=20    --asm-define BAND_HEIGHT=20 \
	-DFILE_WIDTH=256    --asm-define FILE_WIDTH=256 \
	-C ../../config/apple2enh-slowtake.cfg \
	--start-addr 0x1900  #One file buffer

# 4 full tracks cache!
qtktconv_CLCFLAGS = \
	-DCACHE_SIZE=20480  --asm-define CACHE_SIZE=20480 \
	-DRAW_X_OFFSET=1    --asm-define RAW_X_OFFSET=1 \
	-DRAW_Y_OFFSET=1    --asm-define RAW_Y_OFFSET=1 \
	-DRAW_WIDTH=768     --asm-define RAW_WIDTH=768 \
	-DBAND_HEIGHT=20    --asm-define BAND_HEIGHT=20 \
	-DFILE_WIDTH=256    --asm-define FILE_WIDTH=256 \
	-DQTKTCONV=1 \
	$(CLCFLAGS) -C ../../config/apple2enh-alignbss.cfg \
	--start-addr 0x1000  #Two file buffers

# 3.5 tracks cache
qtknconv_CLCFLAGS = \
	-DCACHE_SIZE=16384  --asm-define CACHE_SIZE=16384 \
	-DRAW_X_OFFSET=0    --asm-define RAW_X_OFFSET=0 \
	-DRAW_Y_OFFSET=0    --asm-define RAW_Y_OFFSET=0 \
	-DRAW_WIDTH=320     --asm-define RAW_WIDTH=320 \
	-DBAND_HEIGHT=20    --asm-define BAND_HEIGHT=20 \
	-DDECODE_WIDTH=320  --asm-define DECODE_WIDTH=320 \
	-DDECODE_HEIGHT=240 --asm-define DECODE_HEIGHT=240 \
	-DFILE_WIDTH=256    --asm-define FILE_WIDTH=256 \
	-DQTKNCONV=1 \
	$(CLCFLAGS) -C ../../config/apple2enh-alignbssdata.cfg \
	--start-addr 0x1000  #Two file buffers

jpegconv_CLCFLAGS = \
	-DCACHE_SIZE=15360  --asm-define CACHE_SIZE=15360 \
	-DRAW_X_OFFSET=0    --asm-define RAW_X_OFFSET=0 \
	-DRAW_Y_OFFSET=0    --asm-define RAW_Y_OFFSET=0 \
	-DRAW_WIDTH=512     --asm-define RAW_WIDTH=512 \
	-DBAND_HEIGHT=20    --asm-define BAND_HEIGHT=20 \
	-DINPUT_WIDTH=640   --asm-define INPUT_WIDTH=640 \
	-DINPUT_HEIGHT=480  --asm-define INPUT_HEIGHT=480 \
	-DDECODE_WIDTH=320  --asm-define DECODE_WIDTH=320 \
	-DDECODE_HEIGHT=240 --asm-define DECODE_HEIGHT=240 \
	-DFILE_WIDTH=256    --asm-define FILE_WIDTH=256 \
	-DJPEGCONV=1        --asm-define JPEGCONV=1 \
	$(CLCFLAGS) -C ../../config/apple2enh-alignbssdata.cfg \
	--start-addr 0x1000  #Two file buffers

GCCCFLAGS = -g -Wall -Wno-pointer-sign -Wno-char-subscripts \
	-Wno-unused-but-set-variable -Wno-unused-variable -Wno-unknown-pragmas \
	-fno-omit-frame-pointer \
	$(shell sdl-config --cflags)
qtktconv_GCCCFLAGS = \
	-DCACHE_SIZE=20480 \
	-DRAW_X_OFFSET=1 \
	-DRAW_Y_OFFSET=1 \
	-DRAW_WIDTH=768 \
	-DBAND_HEIGHT=20 \
	-DFILE_WIDTH=256 \
	-DQTKTCONV=1 \

qtknconv_GCCCFLAGS = \
	-DCACHE_SIZE=16384 \
	-DRAW_X_OFFSET=0 \
	-DRAW_Y_OFFSET=0 \
	-DRAW_WIDTH=320 \
	-DBAND_HEIGHT=20 \
	-DINPUT_WIDTH=640 \
	-DINPUT_HEIGHT=480 \
	-DDECODE_WIDTH=320 \
	-DDECODE_HEIGHT=240 \
	-DFILE_WIDTH=256 \
	-DQTKNCONV=1 \

jpegconv_GCCCFLAGS = \
	-DCACHE_SIZE=14336 \
	-DRAW_X_OFFSET=0 \
	-DRAW_Y_OFFSET=0 \
	-DRAW_WIDTH=512 \
	-DBAND_HEIGHT=20 \
	-DINPUT_WIDTH=640 \
	-DINPUT_HEIGHT=480 \
	-DDECODE_WIDTH=320 \
	-DDECODE_HEIGHT=240 \
	-DFILE_WIDTH=256 \
	-DJPEGCONV=1 \

GCCLDFLAGS = $(shell sdl-config --libs)

.PHONY: all clean

all: quicktake.po

../image-viewer/imgview.bin:
	make -C ../image-viewer/ clean all

about: about.in
	sed "s/__VERSION__/v$(VERSION)/" about.in > about

add_disk: about
	java -jar ../../bin/ac.jar -p $(IMG) $(PREFIX)SLOWTAKE.SYSTEM SYS < ../../bin/loader.system; \
	java -jar ../../bin/ac.jar -d $(IMG) $(PREFIX)BASIC.SYSTEM; \
	java -jar ../../bin/ac.jar -p $(IMG) $(PREFIX)ABOUT BIN 0x2000 < about; \
	for r in *.DRV; do \
		java -jar ../../bin/ac.jar -p $(IMG) $(PREFIX)$$(basename $$r) BIN 0xC00 < $$r; \
	done; \
	for r in *.bin ../image-viewer/imgview.bin; do \
		java -jar ../../bin/ac.jar -as $(IMG) $(PREFIX)$$(basename $$r | sed "s/\.bin$///") BIN < $$r; \
	done;

quicktake.po: splash.h slowtake.bin qtktconv.bin qtknconv.bin jpegconv.bin loader.x86_64 qtktconv.x86_64 qtknconv.x86_64 jpegconv.x86_64 ../image-viewer/imgview.bin
	cp $(CLEANDISK) $@; \
	java -jar ../../bin/ac.jar -n $@ $(FLOP_NAME)
	IMG=$@ PREFIX= make add_disk

hdv: 
	java -jar ../../bin/ac.jar -d $(IMG) $(FLOP_NAME)
	PREFIX=$(FLOP_NAME)/ make add_disk

splash.hgr: splash.png
	../surl-server/hgr-convert $<

splash.h: splash.hgr
	../../bin/make-splash.sh splash.png

slowtake.bin: $(slowtake_CC65SOURCES) $(slowtake_SOURCES)
	cl65 --debug-info -Wl --dbgfile,"slowtake.dbg" $(CFLAGS) $(slowtake_CLCFLAGS) -vm -m slowtake.map -o $@ $^ $(TARGET)-iobuf-0800.o

slowtake.x86_64: $(slowtake_SOURCES) $(slowtake_GCCSOURCES) $(GCCSOURCES)
	gcc $(CFLAGS) $(GCCCFLAGS) $^ $(GCCLDFLAGS) -o $@

qtktconv.bin: $(qtktconv_CC65SOURCES) $(qtktconv_SOURCES)
	cl65 --debug-info -Wl --dbgfile,"qtktconv.dbg" $(CFLAGS) $(qtktconv_CLCFLAGS) -vm -m qtktconv.map -o $@ $^ $(TARGET)-iobuf-0800.o

qtktconv.x86_64: $(qtktconv_SOURCES) $(qtktconv_GCCSOURCES) $(GCCSOURCES)
	gcc $(CFLAGS) $(GCCCFLAGS) $(qtktconv_GCCCFLAGS) $^ $(GCCLDFLAGS) -o $@

qtknconv.bin: $(qtknconv_CC65SOURCES) $(qtknconv_SOURCES)
	cl65 --debug-info -Wl --dbgfile,"qtknconv.dbg" $(CFLAGS) $(qtknconv_CLCFLAGS) -vm -m qtknconv.map -o $@ $^ $(TARGET)-iobuf-0800.o

qtknconv.x86_64: $(qtknconv_SOURCES) $(qtknconv_GCCSOURCES) $(GCCSOURCES)
	gcc $(CFLAGS) $(GCCCFLAGS) $(qtknconv_GCCCFLAGS) $^ $(GCCLDFLAGS) -o $@

jpegconv.bin: $(jpegconv_CC65SOURCES) $(jpegconv_SOURCES)
	cl65 --debug-info -Wl --dbgfile,"jpegconv.dbg" $(CFLAGS) $(jpegconv_CLCFLAGS) -vm -m jpegconv.map -o $@ $^ $(TARGET)-iobuf-0800.o

jpegconv.x86_64: $(jpegconv_SOURCES) $(jpegconv_GCCSOURCES) $(GCCSOURCES)
	gcc $(CFLAGS) $(GCCCFLAGS) $(jpegconv_GCCCFLAGS) $^ $(GCCLDFLAGS) -o $@

loader.x86_64: loader.c
	gcc -o $@ -I/usr/include/SDL -I../lib $< ../lib/extended_conio.c ../lib/c/clrzone.c -lSDL_image -lSDL

clean:
	rm -f *.bin *.x86_64 *.o *.map splash.h splash.hgr *.dbg *.lbl splash.h *.hgr *.po

upload: clean all
	test -d /run/user/1000/gvfs/smb-share\:server\=diskstation.lan\,share\=a2repo/apple2/quicktake/ || \
	mkdir -p /run/user/1000/gvfs/smb-share\:server\=diskstation.lan\,share\=a2repo/apple2/quicktake/ && \
	cp *.bin about /run/user/1000/gvfs/smb-share\:server\=diskstation.lan\,share\=a2repo/apple2/quicktake/ && \
	cp *.po /run/user/1000/gvfs/smb-share\:server\=diskstation.lan\,share\=a2repo/apple2/disks/

dist: clean quicktake.po
	mkdir -p ../../dist/
	cp quicktake.po ../../dist/
