#!/bin/bash
#########################################################################################################
##
## Name:            assemble_mz2z009e.sh
## Created:         March 2026
## Author(s):       Philip Smart
## Description:     Assemble MZ-2Z009E dz80 disassembly with GLASS Z80 assembler
##                  and verify binary matches original.
##
## 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/>.
#########################################################################################################

DISDIR="$(cd "$(dirname "$0")" && pwd)"
OUTDIR="${DISDIR}/tmp"

GLASS_JAR=""
for candidate in \
    "${DISDIR}/glass.jar" \
    "/srv/dvlp/Projects/RFS/tools/glass.jar" \
    "$(which glass.jar 2>/dev/null)"; do
    if [ -f "$candidate" ]; then
        GLASS_JAR="$candidate"
        break
    fi
done

if [ -z "$GLASS_JAR" ]; then
    echo "ERROR: glass.jar not found."
    exit 1
fi

SRC="${DISDIR}/mz2z009e_dz80.asm"
ORIGINAL_BIN="${DISDIR}/mz2z009e.bin"

if [ ! -f "${SRC}" ]; then
    echo "ERROR: Source file not found at ${SRC}"
    echo "       Run disassemble_mz2z009e.sh first to generate it."
    exit 1
fi

if [ ! -f "${ORIGINAL_BIN}" ]; then
    echo "ERROR: Original binary not found at ${ORIGINAL_BIN}"
    exit 1
fi

mkdir -p "${OUTDIR}"

echo "MZ-2Z009E Disk BASIC Assembly Tool"
echo "==================================="
echo "  Source:    ${SRC}"
echo "  Glass:     ${GLASS_JAR}"
echo "  Output:    ${OUTDIR}"
echo ""

echo "--- Assembling with GLASS Z80 assembler ---"
java -jar "${GLASS_JAR}" "${SRC}" "${OUTDIR}/mz2z009e.bin" "${OUTDIR}/mz2z009e.sym"

if [ $? -ne 0 ]; then
    echo ""
    echo "ERROR: Assembly failed. Fix errors above and retry."
    exit 1
fi

ORIG_SIZE=$(wc -c < "${ORIGINAL_BIN}")
ASM_SIZE=$(wc -c < "${OUTDIR}/mz2z009e.bin")
echo "  Binary:  ${OUTDIR}/mz2z009e.bin (${ASM_SIZE} bytes)"
echo "  Symbols: ${OUTDIR}/mz2z009e.sym"
echo "  Reference: ${ORIGINAL_BIN} (${ORIG_SIZE} bytes)"
echo ""

echo "--- Verifying binary match ---"
if cmp -s "${ORIGINAL_BIN}" "${OUTDIR}/mz2z009e.bin"; then
    echo "  PASS: Re-assembled binary is identical to original."
else
    echo "  FAIL: Re-assembled binary differs from original!"
    echo "  Differences:"
    cmp -l "${ORIGINAL_BIN}" "${OUTDIR}/mz2z009e.bin" | head -20
    exit 1
fi
echo ""

echo "==================================="
echo "Complete."
ls -la "${OUTDIR}"/mz2z009e*
