#!/bin/bash

# Slackware build script for clksignal-roms

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# I don't host any of the DOWNLOAD files (except the oric color
# palette). ROM sites tend to come and go, so at some point the
# download links will likely break. Let me know if this happens.

# To clean up the directory:
# rm -f *.rom *.ROM *.img *.zip *.chr basic chargen kernal dos154*

# 20230716 bkw:
# - clksignal 20230515's clksignal-qt now looks in a different dir for ROMS;
#   include a symlink in the package.

# 20230114 bkw:
# - add apple2gs.chr
# - add tos104uk.img
# - update STATUS.txt

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=clksignal-roms
VERSION=${VERSION:-20230716}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

install_rom() {
  install -m0644 -oroot -groot "$1" "$2"
}

ROMBASE=$PKG/usr/share/CLK
set_dest() {
  DEST="$ROMBASE/$1"
  mkdir -p "$DEST"
}

###### Apple II series ROMs, including the Disk II and IIgs.
set_dest AppleII

# wheels within wheels, plans within plans, zipfiles within zipfiles...
unzip $CWD/apple2_roms.zip apple.rom apple2o.rom 13-sector-disk-ii-roms.zip \
  apple_iie_rom.zip apple_2e_unenhanced_rom.zip ROMS.ZIP
unzip apple_iie_rom.zip APPLE2E.ROM
unzip apple_2e_unenhanced_rom.zip
unzip ROMS.ZIP 3410036.BIN 3420028A.BIN 3410027A.BIN

install_rom apple.rom $DEST/apple2.rom
install_rom apple2o.rom $DEST/apple2o.rom
install_rom APPLE2E.ROM $DEST/apple2e.rom
install_rom apple2eu.rom $DEST/apple2eu.rom
install_rom 3410036.BIN $DEST/apple2-character.rom

unzip $CWD/apple2ee.zip
install_rom 3420265a.chr $DEST/apple2e-character.rom

# CRC doesn't match, but the machine starts and seems to work
# correctly.
unzip $CWD/apple2e.zip 3420133a.chr
install_rom 3420133a.chr $DEST/apple2eu-character.rom

set_dest DiskII
install_rom 3410027A.BIN $DEST/boot-16.rom
install_rom 3420028A.BIN $DEST/state-machine-16.rom

unzip 13-sector-disk-ii-roms.zip '*.bin'
install_rom 13-sector-disk-ii-boot-rom.bin $DEST/boot-13.rom
install_rom 13-sector-disk-ii-state-machine-rom.bin $DEST/state-machine-13.rom

set_dest AppleIIgs
Z="$CWD/MESS 0.151 ROMs%2Fapple2gs.zip"
[ -e "$Z" ] || Z="$CWD/MESS%200.151%20ROMs%2Fapple2gs.zip"
[ -e "$Z" ] || Z="$CWD/apple2gs.zip"
unzip "$Z"

# Only include the ROM03 firmware.
# According to the CRC, this is an 'alternate' (or anyway, it's the 2nd
# of two known CRCs).
# Doesn't really matter: clksignal segfaults on trying to start up
# with --new=appleiigs.
cat 341-0737 341-0748 > rom03
install_rom rom03 $DEST/apple2gs.rom2
install_rom 341s0632-2.bin $DEST/341s0632-2
install_rom apple2gs.chr $DEST/apple2gs.chr

###### Commodore VIC-20 and 1540/1541 ROMs
# only English and PAL for now.
set_dest Vic20
install_rom $CWD/basic $DEST/basic.bin
install_rom $CWD/kernal $DEST/kernel-pal.bin
install_rom $CWD/chargen $DEST/characters-english.bin

# the 1541 isn't yet emulated by clksignal...
set_dest Commodore1540
install_rom $CWD/dos1540 $DEST/1540.bin
install_rom $CWD/dos1541 $DEST/1541.bin

###### Atari ST
set_dest AtariST
unzip $CWD/tos100uk.zip
install_rom tos100uk.img $DEST/tos100.img
unzip $CWD/tos104uk.zip
install_rom tos104uk.img $DEST/tos104.img

###### Colecovision
set_dest ColecoVision
unzip $CWD/coleco.zip
install_rom coleco.rom $DEST/coleco.rom

##### Amiga
# handle filename with spaces (wget) or hex escapes (curl -O, some browsers).
set_dest Amiga
Z="$CWD/Kickstart v1.3 rev 34.5 (1987)(Commodore)(A500-A1000-A2000-CDTV).rom"
if [ ! -e "$Z" ]; then
  Z="$CWD/Kickstart%20v1.3%20rev%2034.5%20%281987%29%28Commodore%29%28A500-A1000-A2000-CDTV%29.rom"
fi
install_rom "$Z" $DEST/Kickstart-v1.3-rev34.5-1987-Commodore-A500-A1000-A2000-CDTV.rom

##### Amstrad CPC
set_dest AmstradCPC

# firmware and BASIC are combined, split 'em apart with dd.
unzip $CWD/cpc464.zip
dd if=cpc464.rom of=$DEST/os464.rom bs=16k count=1
dd if=cpc464.rom of=$DEST/basic464.rom bs=16k count=1 skip=1
install_rom cpcados.rom $DEST/amsdos.rom

unzip $CWD/cpc664.zip
dd if=cpc664.rom of=$DEST/os664.rom bs=16k count=1
dd if=cpc664.rom of=$DEST/basic664.rom bs=16k count=1 skip=1

unzip $CWD/cpc6128.zip
dd if=cpc6128.rom of=$DEST/os6128.rom bs=16k count=1
dd if=cpc6128.rom of=$DEST/basic6128.rom bs=16k count=1 skip=1

##### Electron
set_dest Electron
unzip $CWD/electron.zip
install_rom basic.rom $DEST/basic.rom
install_rom os.rom $DEST/os.rom

##### Macintosh
set_dest Macintosh
unzip $CWD/mac128k.zip
unzip $CWD/mac512k.zip
unzip $CWD/macplus.zip
install_rom mac128k.rom $DEST/mac128k.rom
install_rom mac512k.rom $DEST/mac512k.rom
install_rom macplus.rom $DEST/macplus.rom

##### Sega Master System
set_dest MasterSystem
unzip $CWD/sms1pal.zip bios13fx.rom
unzip $CWD/smsj.zip jbios21.rom
install_rom bios13fx.rom $DEST/bios.sms
install_rom jbios21.rom $DEST/japanese-bios.sms

##### Enterprise
set_dest Enterprise
Z="$CWD/MESS 0.151 ROMs%2Fep64.zip"
[ ! -e "$Z" ] && Z="$CWD/MESS%200.151%20ROMs%2Fep64.zip"
[ ! -e "$Z" ] && Z="$CWD/ep64.zip"
unzip "$Z"
install_rom 9256ds-0038_enter05-23-a.u2 $DEST/exos20.bin

##### MSX. "any MSX BIOS", whatever that means.
set_dest MSX
install_rom $CWD/MSX.ROM $DEST/msx.rom
install_rom $CWD/DISK.ROM $DEST/disk.rom

##### Oric
# only have 2 of the 4 disk interfaces and 2 of the 3 BASIC ROMs.
# oric-pal-prom.zip originally came from
# https://forum.defence-force.org/viewtopic.php?f=8&t=2248&hilit=colour+rom&start=15
# ...and isn't copyrighted AFAIK.
set_dest Oric
unzip $CWD/oric1.zip
unzip $CWD/orica.zip
unzip $CWD/oric-pal-prom.zip
install_rom basic10.rom $DEST/basic10.rom
install_rom basic11b.rom $DEST/basic11.rom
install_rom jasmin.rom $DEST/jasmin.rom
install_rom microdis.rom $DEST/microdisc.rom
install_rom tbp24s10n.bin $DEST/colour.rom

##### ZX80/81
set_dest ZX8081
unzip $CWD/zx80.zip zx80.rom
unzip $CWD/zx81.zip zx81a.rom
install_rom zx80.rom $DEST/zx80.rom
install_rom zx81a.rom $DEST/zx81.rom

# 20230716 bkw: newer clksignal-qt requires this:
mkdir -p $PKG/usr/share/clksignal-qt
ln -s ../CLK $PKG/usr/share/clksignal-qt/ROMImages

# No docs, include our own.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cat $CWD/README > $PKGDOC/README.txt
cat $CWD/STATUS.txt > $PKGDOC/STATUS.txt
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
