#!/bin/ksh
#
# dosbox.app-jp106.sh for macOS
#
# Written by Taiji Yamada <taiji@aihara.co.jp>
#
dosbox_app="$(find /Applications -maxdepth 2 -name dosbox.app 2> /dev/null | head -n 1)"
DOSBox="$dosbox_app"/Contents/MacOS/DOSBox
DOSBox_Preferences="$(find "$HOME"/Library/Preferences -maxdepth 1 -name 'DOSBox * Preferences' 2> /dev/null | sort | tail -n 1)"

[ "$dosbox_app" != '' ] || { echo 'dosbox.app not found'; exit 1; }
[ "$DOSBox_Preferences" != '' ] || { echo "$HOME/Library/Preferences/DOSBox * Preferences not found"; exit 1; }

cd "$(dirname $0)" &&
[ -f jp.kl ] || curl -ROL -C - https://www.aihara.co.jp/~taiji/lecture/dosbox-jp106/jp.kl 2> /dev/null &&
sed 's/^\(mapperfile *= *\).*$/\1mapper-0.74-3-3.map/;s/^\(keyboardlayout *= *\).*/\1jp/' "$DOSBox_Preferences" > "$(basename "$DOSBox_Preferences")" 2> /dev/null &&
echo "$DOSBox" -conf "$(dirname $0)"/"$(basename "$DOSBox_Preferences")" "$@" &&
exec "$DOSBox" -conf "$(dirname $0)"/"$(basename "$DOSBox_Preferences")" "$@"
