aboutsummaryrefslogtreecommitdiff
path: root/games/sms_sdl/sms_sdl.sh
blob: cccfce356301a4c051608e03e209843140cdf104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash

# 20091011 bkw: Wrapper script for sms_sdl, does the following:

# - Savestates and screenshots will be saved in ~/.sms_sdl/
# - A ~/.sms_sdl/config file will be read if present, and converted to
#   command-line options for the real sms_sdl binary.

# To make things work properly, we have to run the real emulator binary
# with its cwd set to ~/.sms_sdl/, and create a symlink to the ROM file
# in the same directory. After the emu exits, we remove the symlink.

# 20160804 bkw: various fixes and enhancements.
# - Make --filter <mode> and --fskip <n> options work from
#   the command line. They work already in a config file, which is why
#   I didn't notice they were broken for *7 years*.
# - Fix the usage message so it doesn't show the /usr/libexec/ path.
# - Make the return value of the script match the return value of the emulator.
# - Add support for single-file zipped ROMs, since I have so many of them.

sms_exe="/usr/libexec/sms_sdl"
sms_userdir=~/.sms_sdl
conf_file="$sms_userdir/config"

romfile=""
conf_args=""

set -e

usage() {
	$sms_exe | sed "/^Usage:/s,$sms_exe,sms_sdl,"
}

unzip_rom() {
	# extract one file (hopefully a ROM image) from a zip file. if there are
	# more files (ROMs or otherwise), they will be ignored.

	# look for known filename extensions first, in case there are readme or
	# file_id or whatever inside the zipfile.
	romfile="$( zipinfo -1 "$1" | egrep -i '\.(sms|gg|rom|bin)$' | head -1 )"

	# if none found, just grab the first filename and hope for the best.
	if [ -z "$romfile" ]; then
		romfile="$( zipinfo -1 "$1" | head -1 )"
	fi

	unzip "$arg" "$romfile" -d $sms_userdir || exit 1
}

mkdir -p $sms_userdir

# 20211025 bkw: if there's no config file, create one.
if [ ! -e "$conf_file" ]; then
   cat > "$conf_file" <<EOF
joystick
fm
filter 2x
EOF
fi

if [ -e "$conf_file" ]; then
	while read line; do
		# remove comments
		line="${line/\#*/}"
		case "$line" in
			"") ;;   # ignore empty lines
			-*)
				conf_args="$conf_args $line"
				;;
			*)
				conf_args="$conf_args --$line"
				;;
		esac
	done < "$conf_file"
fi

for arg; do
	if [ "$needparam" = "1" ]; then
		conf_args="$conf_args $arg"
		needparam=0
	else
		case "$arg" in
			-h|-help|--help|-\?)
				usage
				exit 0
				;;

			--filter|--fskip)
				conf_args="$conf_args $arg"
				needparam=1
				;;

			--*)
				conf_args="$conf_args $arg"
				;;

			*)
				if [ -z "$romfile" ]; then
					case "$arg" in
						*.zip|*.ZIP*|*.Zip)
							unzip_rom "$arg" # sets romfile
						;;
						*)
							arg="$( readlink -f "$arg" )"
							romfile="$( basename "$arg" )"
							( cd $sms_userdir ; rm -f "$romfile" ; ln -s "$arg" . )
					esac
				fi
				;;
		esac
	fi
done

set +e
if [ -z "$romfile" ]; then
	usage
	exit 1
else
	cd $sms_userdir
	#echo $sms_exe $conf_args "$romfile"
	$sms_exe $conf_args "$romfile"
	rv="$?"
	rm -f "$romfile"
	exit $rv
fi