spacetaxi/scripts/importData.sh
2026-10-05 18:50:09 -05:00

92 lines
2.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# importData.sh - install the one file Space Taxi needs that is not in the
# repository: a memory image of the original Commodore 64 game, which the
# build extracts the 24 levels and the title screen from (program.mk). It is
# never downloaded from anywhere: you supply your copy, and it is recognised
# by its checksum, so its file name does not matter.
#
# Usage: scripts/importData.sh PATH...
# PATH the image itself, a folder holding it, or a .zip or
# .tar.gz archive with it inside (such as the owner's
# private bundle)
# Installs it as stuff/raw.bin. stuff/ is never committed: the image is a
# copy of a commercial game.
set -euo pipefail
repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
dest=$repo/stuff/raw.bin
# sha1 of the C64 memory image the levels are extracted from.
wantSha1=5bfa4ba70431135575dab610a73dde02b8095953
work=""
# The PATH being scanned, for the messages.
origin=""
# cleanup -- remove the scratch folder archives are unpacked into.
cleanup() {
if [ -n "$work" ]; then
rm -rf "$work"
fi
}
# isImage FILE -- true when FILE is the image.
isImage() {
[ "$(sha1sum < "$1" | cut -d' ' -f1)" = "$wantSha1" ]
}
# scanFile FILE -- install FILE if it is the image, or the image from inside
# FILE when it is an archive.
scanFile() {
local unpacked
if isImage "$1"; then
install -D -m 644 "$1" "$dest"
echo "importData: installed $dest from $origin"
exit 0
fi
case "$1" in
*.zip|*.ZIP|*.tar.gz|*.tgz)
unpacked=$(mktemp -d -p "$work")
case "$1" in
*.zip|*.ZIP) unzip -qq -o "$1" -d "$unpacked" 2>/dev/null || true ;;
*) tar -xzf "$1" -C "$unpacked" 2>/dev/null || true ;;
esac
scanTree "$unpacked"
;;
esac
}
# scanTree DIR -- install the image if any file under DIR is it.
scanTree() {
local file
while IFS= read -r -d '' file; do
scanFile "$file"
done < <(find "$1" -type f -print0)
}
if [ $# -eq 0 ]; then
sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'
exit 2
fi
if [ -f "$dest" ] && isImage "$dest"; then
echo "importData: $dest is already in place"
exit 0
fi
work=$(mktemp -d)
trap cleanup EXIT
for path in "$@"; do
origin=$path
if [ -d "$path" ]; then
scanTree "$path"
elif [ -f "$path" ]; then
scanFile "$path"
else
echo "importData: no such file or folder: $path" >&2
exit 2
fi
done
echo "importData: none of $* is the Space Taxi image (sha1 $wantSha1)" >&2
exit 1