#!/bin/bash
#########################################################################################################
##
## Name:            make_sdcard.sh
## Created:         August 2018
## Author(s):       Philip Smart
## Description:     Sharp MZ series SD Card Packaging tool
##                  This is a very basic script to package programs into images for writing onto an
##                  SD Card as used in the Rom Filing System. The image is comprised of several parts,
##                  ie. <RFS IMAGE> + <CPM DISK IMAGE 0> .. <CPM DIK IMAGE n>.
##
## Credits:         
## Copyright:       (c) 2020 Philip Smart <philip.smart@net2net.org>
##
## History:         March 2020   - Initial script written.
##
#########################################################################################################
## 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=../../MZ80A_RFS
MZF_PATH=${ROOTDIR}/software/MZF
TOOL_DIR=${ROOTDIR}/software/tools
IMAGE_DIR=${ROOTDIR}/software/roms
CPM_DIR=${ROOTDIR}/software/CPM/SDC16M/RAW
SDTOOL=${TOOL_DIR}/sdtool
RFS_IMAGE_FILE=${IMAGE_DIR}/SHARP_MZ80A_RFS_IMAGE_1.img
CPM_IMAGE_FILES="SDCDISK0.RAW SDCDISK1.RAW SDCDISK2.RAW SDCDISK3.RAW SDCDISK4.RAW SDCDISK5.RAW SDCDISK6.RAW"
SD_IMAGE_FILE=${IMAGE_DIR}/SHARP_MZ80A_RFS_CPM_IMAGE_1.img

# Create the initial RFS image.
${SDTOOL} --image ${RFS_IMAGE_FILE} --create
if [ $? != 0 ]; then
    echo "Failed to create initial RFS Image, aborting."
    exit 1
fi


# Manually choose the programs you want installed into the RFS image. The files will be first placed into
# the directory in the order they appear here, which initially is alphabetic order.
RFS_INCLUDE=
RFS_INCLUDE+="${MZF_PATH}/1Z-013B.MZF:"
RFS_INCLUDE+="${MZF_PATH}/2Z009E.MZF:"
RFS_INCLUDE+="${MZF_PATH}/2z-046a.MZF:"
RFS_INCLUDE+="${MZF_PATH}/5Z-009A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/5Z-009B.MZF:"
RFS_INCLUDE+="${MZF_PATH}/6502_Betriebssys.MZF:"
RFS_INCLUDE+="${MZF_PATH}/80A_PENCIL.A2_C2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/80A_PENCIL.A2_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/80zbasic.MZF:"
RFS_INCLUDE+="${MZF_PATH}/A-BASIC_SA-5510.MZF:"
RFS_INCLUDE+="${MZF_PATH}/AIP_-_LOGO_xrr.MZF:"
RFS_INCLUDE+="${MZF_PATH}/APOLLO_CHESS_v2a.MZF:"
RFS_INCLUDE+="${MZF_PATH}/B880.A3_P6.MZF:"
RFS_INCLUDE+="${MZF_PATH}/B880_MASTER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASICRFS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC80A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_MZ-5Z008_2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_MZ-5Z008.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_MZ-5Z009_modified.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_MZ-5Z009.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_OM-1000.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_OM-1001.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_OM-500.MZF:"
RFS_INCLUDE+="${MZF_PATH}/basic_sa-5510.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC.SA-5510.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SA-5575_C.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SA-5575_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SA-5577_C.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SA-5577_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SA-5580.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BASIC_SP-5025.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BAS_MOD_v3.74.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BATTLE_GAME.MZF:"
RFS_INCLUDE+="${MZF_PATH}/BINARY_COUNT.MZF:"
RFS_INCLUDE+="${MZF_PATH}/bomberman_MZ700.m12:"
RFS_INCLUDE+="${MZF_PATH}/BYTESAVER_SA5510.MZF:"
RFS_INCLUDE+="${MZF_PATH}/cannon_ball.m12:"
RFS_INCLUDE+="${MZF_PATH}/clock1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CLUB_COPY.U1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CLUB_MON.A1_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CLUB_MON.A1_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/cmttofd.MZF:"
RFS_INCLUDE+="${MZF_PATH}/COLONY.MZF:"
RFS_INCLUDE+="${MZF_PATH}/COMPILER_A2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CONVERTER_A_700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CONVERTER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/COPIER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/COSMO_BLASTER_MZ700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/cpm22.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CPM_RFS_1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CPM_RFS_2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/CRUISER3_MZ1500.M12:"
RFS_INCLUDE+="${MZF_PATH}/DCS_MZ80A_APPEND.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DCS_MZ80A_RENUM.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DELETE.MZF:"
RFS_INCLUDE+="${MZF_PATH}/diamond.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DISASM_8800.A15.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DISASM_B800.A15.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DISKEDIT.A4B.MZF:"
RFS_INCLUDE+="${MZF_PATH}/DISKEDIT.A7_40T.MZF:"
RFS_INCLUDE+="${MZF_PATH}/diskutility.MZF:"
RFS_INCLUDE+="${MZF_PATH}/doordoor:"
RFS_INCLUDE+="${MZF_PATH}/Doordoor.mzt:"
RFS_INCLUDE+="${MZF_PATH}/EXPRESS_BAS_700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/EXPRESS_COMPILER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/EXPRESS_PLUS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/EXT.BASIC_OM-500.MZF:"
RFS_INCLUDE+="${MZF_PATH}/FDCOPY.MZF:"
RFS_INCLUDE+="${MZF_PATH}/FDCOPY.MZT:"
RFS_INCLUDE+="${MZF_PATH}/FD_Editor_MZ700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Filing(CMT).MZF:"
RFS_INCLUDE+="${MZF_PATH}/Filing_CMT_.MZT:"
RFS_INCLUDE+="${MZF_PATH}/FLAP.MZF:"
RFS_INCLUDE+="${MZF_PATH}/flugsim_MZ700.m12:"
RFS_INCLUDE+="${MZF_PATH}/fortransosz80.MZF:"
RFS_INCLUDE+="${MZF_PATH}/FRONT_PANEL_v1.5.MZF:"
RFS_INCLUDE+="${MZF_PATH}/GALAXI_FORM.MZF:"
RFS_INCLUDE+="${MZF_PATH}/GALAXY_INVADERS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/GDP9-BA.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Greedy_Gremlins.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Hardcopy.MZF:"
RFS_INCLUDE+="${MZF_PATH}/hi-ramcheck.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HP4TMZ7L.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HP4TMZ7.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HU-BASIC.A1_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HU-BASIC.A1_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HU-BASIC.A2_80M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HU-BASIC.A2_80S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HU-BASIC_V1.3_K.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HUCALC_80A+_C2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HUCALC_80A+_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HUCALC_80A+_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/hudson_basic.MZF:"
RFS_INCLUDE+="${MZF_PATH}/HUNCHY.MZF:"
RFS_INCLUDE+="${MZF_PATH}/INSTRUCS_v1.1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/JIGSAW.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Joy.MZF:"
RFS_INCLUDE+="${MZF_PATH}/k-basic_v.5.MZF:"
RFS_INCLUDE+="${MZF_PATH}/KNIFORTH.MZF:"
RFS_INCLUDE+="${MZF_PATH}/KUMA_INTERPR..MZF:"
RFS_INCLUDE+="${MZF_PATH}/KuPTest.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Lady_Bug_MZ80K.m12:"
RFS_INCLUDE+="${MZF_PATH}/LAND_ESCAPE.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Le_Mans.MZF:"
RFS_INCLUDE+="${MZF_PATH}/loader.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MAGIC_PAINTBOX.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MAN-HUNT.MZF:"
RFS_INCLUDE+="${MZF_PATH}/m_c_Breakout_2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/m_c_Hissing_Sid.MZF:"
RFS_INCLUDE+="${MZF_PATH}/m_c_Race_Chase.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MEMORY_TEST.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MINI_DATACARD..MZF:"
RFS_INCLUDE+="${MZF_PATH}/minotaur.MZF:"
RFS_INCLUDE+="${MZF_PATH}/ML-SP_8002_BBG.MZF:"
RFS_INCLUDE+="${MZF_PATH}/monitor2:"
RFS_INCLUDE+="${MZF_PATH}/monitor3.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MONITOR6.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MOVING_SEARCHER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Mz1571.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MZ-2Z009.MZF:"
RFS_INCLUDE+="${MZF_PATH}/mz-5z009_modified2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MZ700BAS.M12:"
RFS_INCLUDE+="${MZF_PATH}/MZ700BAS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MZ-700_FORTH.MZF:"
RFS_INCLUDE+="${MZF_PATH}/MZ80A_basic.DSK:"
RFS_INCLUDE+="${MZF_PATH}/MZ-80A_GALACTIC.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Mzprint.MZF:"
RFS_INCLUDE+="${MZF_PATH}/nakamoto_MZ700.m12:"
RFS_INCLUDE+="${MZF_PATH}/NEW_INVADERS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/OPENING_DATA.MZF:"
RFS_INCLUDE+="${MZF_PATH}/OTHELLO.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PAC-MAN3.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PAC-MAN.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PAINFUL_MAN.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PAINTBOX.BAS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PCG_BASIC_MZ700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PCG_BASIC.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Pcgrally_MZ800.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PROBE_A_1200.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PROBE_A_8000.MZF:"
RFS_INCLUDE+="${MZF_PATH}/PROBE_A_B600.MZF:"
RFS_INCLUDE+="${MZF_PATH}/QD_BAS_5Z008_MZ700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/QDCOPY.MZF:"
RFS_INCLUDE+="${MZF_PATH}/RAM_CHECK_A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/REALFORT.MZF:"
RFS_INCLUDE+="${MZF_PATH}/ROUND_SHOOT.MZF:"
RFS_INCLUDE+="${MZF_PATH}/sa-5510_Bas_MZ80K.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SA-5510_Compiler.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SA-5510+KN.COMM..MZF:"
RFS_INCLUDE+="${MZF_PATH}/sa-6510.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SARGON_2.71.MZF:"
RFS_INCLUDE+="${MZF_PATH}/S-Basic-Cent-2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/schach2_MZ700.m12:"
RFS_INCLUDE+="${MZF_PATH}/SCRAMBLE_A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/sdtest.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SECTOR_R_W.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SECTOR_R_W(NEC).MZF:"
RFS_INCLUDE+="${MZF_PATH}/send-1_MZ700.m12:"
RFS_INCLUDE+="${MZF_PATH}/SEND-1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SHARPLAN01.MZF:"
RFS_INCLUDE+="${MZF_PATH}/sharpmz-test.mzf:"
RFS_INCLUDE+="${MZF_PATH}/sharpmz-test.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SHARP_PENCIL.A1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SHARP_PENCIL.ALF.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SLAVE_v1.1A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/S-MASTER.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SNAKE&SNAKE_EXP1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SNOWFLAKES.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SOLO_BASIC.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SP-4015.A1_C.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SP-4015.A1_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SP-5060.A1_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SP-5060.A1_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SPACE_INVADERS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SP-CONVERT.A1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/STKEEPER2BAS700A.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUB-MONITOR-700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUCOPY_A000.A16.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUPERFIRE.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUPER_PUCK-MAN.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUPERTAPE_2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUTAM1F.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUTAMC9.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUTAPEBA.MZF:"
RFS_INCLUDE+="${MZF_PATH}/SUTAPEMO.MZF:"
RFS_INCLUDE+="${MZF_PATH}/tetris-2_MZ800.MZF:"
RFS_INCLUDE+="${MZF_PATH}/TETRIS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/TEXT_BASIC_I.MZF:"
RFS_INCLUDE+="${MZF_PATH}/TEXT~ED_v1.2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/textsobs5.MZF:"
RFS_INCLUDE+="${MZF_PATH}/TRANS.MZF:"
RFS_INCLUDE+="${MZF_PATH}/TRANS.MZT:"
RFS_INCLUDE+="${MZF_PATH}/ufo.MZF:"
RFS_INCLUDE+="${MZF_PATH}/UNI=BASIC800.MZF:"
RFS_INCLUDE+="${MZF_PATH}/UNIVERSAL_BASIC.MZF:"
RFS_INCLUDE+="${MZF_PATH}/URAS-700.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Utility_2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Utility.MZT:"
RFS_INCLUDE+="${MZF_PATH}/Utility_V_1.1.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Utility_V_2.0.MZF:"
RFS_INCLUDE+="${MZF_PATH}/WDPRO_2.37AT_C2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/WDPRO_2.37AT.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Wooky.MZF:"
RFS_INCLUDE+="${MZF_PATH}/XPATCH_5510_v2.2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Z80_MACHINE.A1_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Z80_MACHINE.A1_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Z80_MACHINE.A2_M.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Z80MACHINE.A3_C2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Z80_MACHINE.A3_S.MZF:"
RFS_INCLUDE+="${MZF_PATH}/ZEN7E.A2.MZF:"
RFS_INCLUDE+="${MZF_PATH}/Zexas_MZ800.MZF:"
RFS_INCLUDE+="${MZF_PATH}/ZSP.MZF"

IFS=":"; for f in ${RFS_INCLUDE}
do
    ${SDTOOL} --image ${RFS_IMAGE_FILE} --add ${f}
    if [ $? != 0 ]; then
        echo "Failed to add:${f} into the RFS Image, aborting."
        exit 2
    fi
done
${SDTOOL} --image ${RFS_IMAGE_FILE} --list-dir

# Concatenate the RFS and CPM images to make a final SD card image.
#
echo "Adding RFS Image:$RFS_IMAGE_FILE} to start of SD Card image."
cat ${RFS_IMAGE_FILE} > ${SD_IMAGE_FILE}
IFS=" "; for f in ${CPM_IMAGE_FILES}
do
    echo "Adding CPM Drive Image:${f} to SD Card image."
    cat ${CPM_DIR}/${f} >> ${SD_IMAGE_FILE}
done
echo "SD Card image generated, file:${SD_IMAGE_FILE}"

exit 0
