Skip to content

Instantly share code, notes, and snippets.

@i30817
Created February 18, 2020 09:43
Show Gist options
  • Save i30817/4efd24973810458507929c55aeca2721 to your computer and use it in GitHub Desktop.
Save i30817/4efd24973810458507929c55aeca2721 to your computer and use it in GitHub Desktop.
retroplay dat transformation test
#!/bin/bash
hash lftp 2>/dev/null || { echo >&2 "Program requires lftp but it's not installed. Aborting."; exit 1; }
hash xmlstarlet 2>/dev/null || { echo >&2 "Program requires xmlstarlet but it's not installed. Aborting."; exit 1; }
hash gunzip 2>/dev/null || { echo >&2 "Program requires gunzip but it's not installed. Aborting."; exit 1; }
hash mktemp 2>/dev/null || { echo >&2 "Program requires mktemp but it's not installed. Aborting."; exit 1; }
#cd to the script dir if executed from outside
SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_PATH"
if [ ! -t 1 ]; then #not from terminal
SCRIPT=$(basename "$(readlink -f "$0")")
SUBPROC=1 x-terminal-emulator --profile "$USER" --working-directory "$SCRIPT_PATH" -e "./$SCRIPT" &
exit 0
fi
DAT_DIR_1=$(mktemp -d)
DAT_DIR_2=$(mktemp -d)
set -euo pipefail
OLDIFS="$IFS"
IFS=$'\n\t'
#we want to interpret the commands here, so it's unquoted
lftp << EOF
set ssl:verify-certificate false
lftp "ftp://ftp:amiga@grandis.nu/Commodore_Amiga/Retroplay/"
mget "Commodore Amiga - WHDLoad - Games (*.zip" -O "$DAT_DIR_1"
mget "Commodore Amiga - HD Loaders*.zip" -O "$DAT_DIR_2"
bye
EOF
IFS="$OLDIFS"
XSL=$(mktemp)
cat << "EOF" > "$XSL"
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<!-- IdentityTransform -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- Remove the useless retroplay 'machine' and 'description' elements-->
<xsl:template match="machine">
<xsl:apply-templates select="rom"/>
</xsl:template>
<!-- transform the retroplay 'rom' element to have a wrapping 'machine' with the rom name minus extension-->
<xsl:template match="rom">
<!-- all of the files on these dirs on the retroplay dir have dot + 3 chars extensions -->
<!-- xsl strings start at '1' not '0' -->
<xsl:variable name="file" select="substring(@name,1, string-length(@name) - 4)" />
<xsl:variable name="tokens" select="str:tokenize($file, '_')" />
<xsl:variable name="suffix">
<xsl:for-each select="$tokens">
<xsl:if test="position()=1">WHDLoad</xsl:if>
<xsl:if test="position()!=1"><xsl:value-of select="concat(', ', .)"/></xsl:if>
</xsl:for-each>
</xsl:variable>
<machine>
<xsl:attribute name="name"><xsl:value-of select="$file"/></xsl:attribute>
<xsl:attribute name="variant_name"><xsl:value-of select="$suffix"/></xsl:attribute>
<description>
<xsl:value-of select="concat($tokens[1],' (Amiga, ', $suffix, ')')"/>
</description>
<xsl:copy><xsl:apply-templates select="@* | node()" /></xsl:copy>
</machine>
</xsl:template>
</xsl:stylesheet>
EOF
gunzip -c "$DAT_DIR_1"/* | xmlstarlet tr --xinclude "$XSL" | xmlstarlet fo > "Commodore - Amiga (WHDLoad).dat"
gunzip -c "$DAT_DIR_2"/* | xmlstarlet tr --xinclude "$XSL" | xmlstarlet fo > "Commodore - Amiga (HD).dat"
#keep terminal open if subprocess started to show output
#indicated by SUBPROC being set
[[ -v SUBPROC ]] && read -p "Press any key to exit" -n1 junk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment