#!/bin/sh
# Enable the "Setup" system (File Manager / 351Files, Copy USB ROMs) on a card
# that is already flashed with the patched SYSTEM.
#
# Why this is needed: EmulationStation prefers
# /storage/.config/emulationstation/es_systems.cfg
# over the image default in /usr/config/emulationstation. On existing cards that
# user copy was created at first boot and does not have the setup system.
#
# Run on the device (SSH):
# sh enable-setup-menu.sh
#
# Alternative: delete the override once and EmulationStation falls back to the
# patched image default:
# rm /storage/.config/emulationstation/es_systems.cfg
set -eu
CFG=/storage/.config/emulationstation/es_systems.cfg
if [ ! -f "$CFG" ]; then
echo "no $CFG - EmulationStation already uses the image default"
exit 0
fi
if grep -q 'setup' "$CFG"; then
echo "setup system already present in $CFG"
exit 0
fi
BK="$CFG.bak.$(date +%s)"
cp -p "$CFG" "$BK"
echo "backup: $BK"
python3 - "$CFG" <<'PY'
import sys
path = sys.argv[1]
xml = open(path, encoding="utf-8").read()
block = '''\t
\t\tsetup
\t\tSetup
\t\tEmuELEC
\t\t2017
\t\tAmlogic
\t\t/usr/bin/scripts/setup
\t\t.sh .SH
\t\temuelecRunEmu.sh %ROM% -P%SYSTEM% --core=%CORE% --emulator=%EMULATOR% --controllers="%CONTROLLERSCONFIG%"
\t\tsetup
\t\tsetup
\t
'''
anchors = [
'\t\n\t\tports',
'\t\r\n\t\tports',
]
for a in anchors:
if a in xml:
xml = xml.replace(a, block + a, 1)
break
else:
raise SystemExit("could not find the ports system to anchor to")
open(path, "w", encoding="utf-8").write(xml)
print("inserted setup system before ports")
PY
echo "done. restart EmulationStation (systemd: systemctl restart emustation)"