aboutsummaryrefslogtreecommitdiff
path: root/dla2img.sh
diff options
context:
space:
mode:
Diffstat (limited to 'dla2img.sh')
-rwxr-xr-xdla2img.sh66
1 files changed, 52 insertions, 14 deletions
diff --git a/dla2img.sh b/dla2img.sh
index f765b69..4258947 100755
--- a/dla2img.sh
+++ b/dla2img.sh
@@ -1,18 +1,35 @@
#!/bin/sh
-# This script tested with bash 5.1, zsh 5.8, ksh 93u, and dash 0.5.11.4.
-# If your environment doesn't have a shell compatible with one of those,
-# I can't help you.
+# This script deliberately written with old-school shell syntax, to
+# support as many Bourne-like shells as possible. Do not complain that
+# backticks are deprecated: this is supposed to (potentially) work
+# on ancient shells that might exist on e.g. an Atari ST or Amiga. It
+# *does* work on a wide variety of modern shells.
-SELF="$(basename $0)"
+# The only required external commands we call are magick, rm, and cat.
+# If stat is present, it will be called (but no problem, if it's
+# missing).
+
+# Tested with ImageMagick 7.1, but doesn't use any fancy features so
+# it should work with any fairly recent ImageMagick.
+
+SELF="`basename $0`"
# allow overriding magick path via environment.
-MAGICK=${MAGICK:-magick}
+if [ "$MAGICK" = "" ]; then
+ MAGICK="magick"
+fi
# only change this if the save file size changes in dla.xex.
SIZE="256x170"
-usage() {
+# ...and if you change that, change this too:
+FILESIZE="5440"
+
+INFILE="$1"
+OUTFILE="$2"
+
+if [ -z "$INFILE" -o -z "$OUTFILE" -o "$INFILE" = "-h" -o "$INFILE" = "--help" ]; then
cat <<EOF
$SELF - convert saved files from dla.xex to image files.
@@ -33,30 +50,51 @@ of supported arguments.
See also: https://slackware.uk/~urchlay/repos/dla-asm
EOF
-}
+ exit 0
+fi
-INFILE="$1"
-OUTFILE="$2"
+shift
+shift
-if [ -z "$INFILE" -o -z "$OUTFILE" -o "$INFILE" = "-h" -o "$INFILE" = "--help" ]; then
- usage
- exit 0
+if [ ! -e "$INFILE" ]; then
+ cat "$INFILE" > /dev/null # to get an error like "No such file or directory"
+ exit 1
fi
-shift 2
+# if "stat" is missing, e.g. because we're on a non-GNU system that
+# lacks the command, just skip the file size check (magick will error
+# out instead; less user-friendly but no big deal).
+if stat /dev/null >/dev/null 2>&1; then
+ if [ "`stat -L -c '%s' "$INFILE"`" != "$FILESIZE" ]; then
+ echo "$SELF: '$INFILE' not a DLA file (size != $FILESIZE)." 1>&2
+ exit 1
+ fi
+fi
+
+rm -f "$OUTFILE"
# do the conversion... if $OUTFILE has an unknown extension, we will
# not get an error, and the conversion will succeed (the output will
# just be a copy of the input!)
-$MAGICK convert -size "$SIZE" -depth 1 "$@" gray:"$INFILE" "$OUTFILE"
+# The "eval" is here to make this script work on the original Bourne
+# shell (yes, the one from V7 UNIX). Seriously. Even with spaces
+# in the arguments!
+eval "$MAGICK convert -size '$SIZE' -depth 1 "$@" gray:'$INFILE' '$OUTFILE'"
# if magick fails, exit with its error status (it already printed its
# error messages to stderr).
S="$?"
if [ "$S" != "0" ]; then
+ echo "$SELF: conversion failed (invalid output file extension?)." 1>&2
exit "$S"
fi
+# if magick "succeeds" but the output file doesn't exist, complain.
+if [ ! -e "$OUTFILE" ]; then
+ echo "$SELF: conversion failed (probably '$INFILE' is not a DLA) file)." 1>&2
+ exit 1;
+fi
+
# if magick "succeeds" but the output is identical to the input, let
# the user know.
if cmp "$INFILE" "$OUTFILE" >/dev/null 2>&1; then