#!/bin/bash
#########################################################################################################
##
## Name:            assemble_mz2z009e.sh
## Created:         March 2026
## Author(s):       Philip Smart
## Description:     MZ-2Z009E BASIC build tool
##                  Builds the MZ-2Z009E BASIC floppy disk image in two variants:
##                    1. Original:  Unmodified MZ-2Z009E binary → .dsk
##                    2. RFS:       Patched with RFS device support → .dsk
##
##                  The RFS version adds an "RFS" device to the BASIC device chain,
##                  enabling SD card access through the picoZ80 RFS ROM at E800H:
##                    DIR "RFS:"    - List SD card directory (drive 0)
##                    DIR "RFS2:"   - List SD card directory (drive 1)
##                    LOAD "RFS:"   - Load program from SD card
##                    SAVE "RFS:"   - Save program to SD card
##
## Credits:
## Copyright:       (c) 2026 Philip Smart <philip.smart@net2net.org>
##
## History:         March 2026 - Initial script.
##
#########################################################################################################
## This source file 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 3 of the License, or
## (at your option) any later version.
##
## This source file 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/>.
#########################################################################################################

ROOTDIR="$(cd "$(dirname "$0")/.." && pwd)"
TOOLDIR=${ROOTDIR}/tools
DISDIR=${ROOTDIR}/dis
OUTDIR=${ROOTDIR}/tmp

ORIGINAL_BIN="${DISDIR}/mz2z009e.bin"
TEMPLATE_DSK="/dvlp/Projects/pico/projects/tzpuPico/MZ-2Z009.dsk"

# Verify original binary exists
if [ ! -f "${ORIGINAL_BIN}" ]; then
    echo "ERROR: Original binary not found at ${ORIGINAL_BIN}"
    echo "       This is the raw MZ-2Z009E BASIC binary (36864 bytes)."
    exit 1
fi

mkdir -p "${OUTDIR}"

echo "MZ-2Z009E BASIC Build Tool"
echo "=========================="
echo "  Source binary: ${ORIGINAL_BIN}"
echo "  Output dir:    ${OUTDIR}"
echo ""

# ---- Build 1: Original version ----
echo "--- Building original version ---"
cp "${ORIGINAL_BIN}" "${OUTDIR}/mz2z009e.bin"
python3 "${DISDIR}/build_dsk.py" "${OUTDIR}/mz2z009e.bin" "${OUTDIR}/mz2z009e.dsk" "${TEMPLATE_DSK}"

if [ $? -eq 0 ]; then
    echo ""
    echo "Original build successful."
    ls -la "${OUTDIR}/mz2z009e.dsk"
else
    echo ""
    echo "ERROR: Original .dsk build failed."
    exit 1
fi

echo ""

# ---- Build 2: RFS version ----
echo "--- Building RFS version ---"
python3 "${DISDIR}/patch_rfs.py" "${ORIGINAL_BIN}" "${OUTDIR}/mz2z009e_rfs.bin"

if [ $? -ne 0 ]; then
    echo ""
    echo "ERROR: RFS patching failed."
    exit 1
fi

python3 "${DISDIR}/build_dsk.py" "${OUTDIR}/mz2z009e_rfs.bin" "${OUTDIR}/mz2z009e_rfs.dsk" "${TEMPLATE_DSK}"

if [ $? -eq 0 ]; then
    echo ""
    echo "RFS build successful."
    ls -la "${OUTDIR}/mz2z009e_rfs.dsk"
else
    echo ""
    echo "ERROR: RFS .dsk build failed."
    exit 1
fi

echo ""
echo "=============================="
echo "Build complete. Output files:"
echo "  ${OUTDIR}/mz2z009e.dsk      (original)"
echo "  ${OUTDIR}/mz2z009e_rfs.dsk  (with RFS device)"
echo ""
echo "RFS device usage in MZ-2Z009E BASIC:"
echo "  DIR \"RFS:\"     - List SD card directory (default drive)"
echo "  DIR \"RFS2:\"    - List SD card drive 1"
echo "  LOAD \"RFS:\"    - Load from SD card"
echo "  SAVE \"RFS:\"    - Save to SD card"
echo ""
echo "Note: RFS device numbers use MZ-800 BASIC convention:"
echo "  RFS:  = drive 0 (default)"
echo "  RFS1: = drive 0"
echo "  RFS2: = drive 1"
echo "  ...   through RFS8: = drive 7"
