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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#!/usr/bin/env sh
# BASEDIR is used to treat this script as a portable application
BASEDIR="$(dirname "$0")"
SCRIPTNAME="$(basename "$0" .sh)"
DESKTOP_ENTRY_OUTPUT_DIR="$BASEDIR/output"
DESKTOP_ENTRY_INSTALL_DIR="$HOME/.local/share/applications"
ICON_SOURCE_DIR="$BASEDIR/icon"
ICON_INSTALL_DIR="$HOME/.local/share/icons/hicolor"
# Build an individual .desktop entry given a name, rompath, and system
# Usage: build_desktop_file NAME ROMPATH SYSTEM
build_desktop_file() {
NAME="$1"
ROMPATH="$2"
SYSTEM="$3"
OUTPUT="$DESKTOP_ENTRY_OUTPUT_DIR/$SYSTEM/${SCRIPTNAME}_${SYSTEM}_${NAME}.desktop"
echo "Building $NAME"
# Check validity
if [ -z "$NAME" ]; then
echo "Skipping: missing NAME"
return
fi
if [ -z "$ROMPATH" ]; then
echo "Skipping: missing PATH"
return
fi
if [ -z "$SYSTEM" ]; then
echo "Skipping: missing SYSTEM"
return
fi
if [ ! -r "$BASEDIR/config/systems/$SYSTEM" ]; then
echo "Skipping: No configuration exists for SYSTEM"
return
fi
# FIXME this warning can have false positives
if [ ! -e "$ROMPATH" ]; then
echo "Warning: PATH does not exist"
fi
unset launcher
unset flags
# shellcheck source=/dev/null
. "$BASEDIR/config/systems/$SYSTEM"
mkdir -p "$DESKTOP_ENTRY_OUTPUT_DIR/$SYSTEM"
echo "[Desktop Entry]" > "$OUTPUT"
echo "Type=Application" >> "$OUTPUT"
echo "Name=$NAME" >> "$OUTPUT"
echo "Icon=${SCRIPTNAME}_${SYSTEM}_${NAME}" >> "$OUTPUT"
echo "Exec=$launcher $flags $ROMPATH" >> "$OUTPUT"
echo "Categories=Game" >> "$OUTPUT"
#echo "Installing $NAME"
#install_icon "$NAME" "$SYSTEM"
#install_desktop_file "$OUTPUT"
}
# Wrapper for a call to build
# Usage: build_wrapper [TARGET_SYSTEMS]...
build_wrapper() {
# find all romlist files in config if --system is not specified
if [ -z "$1" ]; then
find "$BASEDIR/config/" -name 'romlist_*' | while read -r file; do
parse_config "$file"
done
else
while [ -n "$1" ]; do
if [ -r "$BASEDIR/config/romlist_$1" ]; then
parse_config "$BASEDIR/config/romlist_$1"
fi
shift
done
fi
}
# empty the output directory
# Usage: clean_wrapper [TARGET_SYSTEMS]
clean_wrapper() {
while [ -n "$1" ]; do
rm -rf "$DESKTOP_ENTRY_OUTPUT_DIR/$1"
shift
done
rmdir --ignore-fail-on-non-empty "$DESKTOP_ENTRY_OUTPUT_DIR"
}
# help message
# Usage: help
help() {
echo "Usage: $0 [OPTION]... [TARGET]..."
echo "Generate desktop entries for video game roms"
echo
echo "Targets:"
echo " build build desktop entries into $DESKTOP_ENTRY_OUTPUT_DIR"
echo " clean remove $DESKTOP_ENTRY_OUTPUT_DIR and its contents (also calls uninstall target)"
echo " install install desktop entries into $DESKTOP_ENTRY_INSTALL_DIR"
echo " uninstall uninstall desktop entries from $DESKTOP_ENTRY_INSTALL_DIR"
echo
echo "Options"
echo " -h, --help print this help message and exit"
echo " --icon-dir [DIR] Specify an icon source directory (default is $ICON_SOURCE_DIR)"
echo " -s, --system [SYSTEM] Specify a system, or comma-separated list of systems. Default is all systems"
}
# Usage: install_icon NAME SYSTEM
install_icon() {
NAME="$1"
SYSTEM="$2"
ICON_SOURCE="$ICON_SOURCE_DIR/$SYSTEM/$NAME"
ICON_EXTENSION=""
# Try a number of file extensions for the icon source
for extension in ".png" ".svg" ".svgz" ".xpm"; do
if [ -r "${ICON_SOURCE}${extension}" ]; then
ICON_EXTENSION="$extension"
break
fi
done
if [ -z "$ICON_EXTENSION" ]; then
echo "Icon does not exist or is not readable. Skipping icon installation"
return
fi
# Append the extension to the source variable to avoid too much typing
ICON_SOURCE="${ICON_SOURCE}${ICON_EXTENSION}"
for size in "16" "24" "32" "48" "64" "96" "128" "256"; do
convert "$ICON_SOURCE" -resize "${size}x${size}"\! "$ICON_INSTALL_DIR/${size}x${size}/apps/${SCRIPTNAME}_${SYSTEM}_$(basename "$ICON_SOURCE")"
done
}
# Wrapper for a call to install
# Usage: install_wrapper [TARGET_SYSTEMS]...
install_wrapper() {
while [ -n "$1" ]; do
find "$DESKTOP_ENTRY_OUTPUT_DIR/$1" -type f | while read -r file; do
echo "Installing $file"
# Install desktop entry
# TODO look into using xdg-desktop-menu for install and uninstall
install "$file" "$DESKTOP_ENTRY_INSTALL_DIR"
# Install icon
NAME="$(grep "^Name=" "$file" | cut -d "=" -f 2)"
SYSTEM="$(basename "$(dirname "$file")")"
install_icon "$NAME" "$SYSTEM"
done
shift
done
}
parse_config() {
CONFIG_PATH="$1"
while read -r line; do
# Skip comments and empty lines
if echo "$line" | grep -q ^#; then
continue
fi
if [ -z "$line" ]; then
continue
fi
eval "build_desktop_file $line"
done < "$CONFIG_PATH"
}
# Usage: uninstall_wrapper [TARGET_SYSTEMS]...
uninstall_wrapper() {
while [ -n "$1" ]; do
find "$DESKTOP_ENTRY_OUTPUT_DIR/$1" -type f | while read -r file; do
echo "Uninstalling $(basename "$file")"
# Remove icons
ICON="$(grep "^Icon=" "$file" | cut -d "=" -f 2)"
for size in "16" "24" "32" "48" "64" "96" "128" "256"; do
rm -f "$ICON_INSTALL_DIR/${size}x${size}/apps/$ICON"*
done
# Remove the desktop entry
rm -f "$DESKTOP_ENTRY_INSTALL_DIR/$(basename "$file")"
done
shift
done
}
# Read arguments
GETOPT=$(getopt -o 'hs:' --long 'help,icon-dir:,system:' -n "$(basename "$0")" -- "$@")
# Terminate if getopt goes wrong
if [ $? -ne 0 ]; then
echo "Terminating..." >&2
exit 1
fi
eval set -- "$GETOPT"
unset GETOPT
while true; do
echo "$1"
case "$1" in
'-h'|'--help')
help
exit
;;
'--icon-dir')
shift
ICON_SOURCE_DIR="$1"
shift
continue
;;
'-s'|'--system')
shift
TARGET_SYSTEMS="$1"
shift
continue
;;
'--')
shift
break
;;
*)
echo "Internal error!" >&2
exit 1
;;
esac
done
# By default, build, but do not install
if [ -z "$1" ]; then
build_wrapper
fi
while [ -n "$1" ]; do
case "$1" in
build)
if [ -z "$TARGET_SYSTEMS" ]; then
build_wrapper
else
build_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ')
fi
shift
continue
;;
clean)
if [ -z "$TARGET_SYSTEMS" ]; then
uninstall_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR")
clean_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR")
else
uninstall_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ')
clean_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ')
fi
shift
continue
;;
install)
if [ -z "$TARGET_SYSTEMS" ]; then
install_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR")
else
install_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ')
fi
# Workaround to refresh icons
touch "$ICON_INSTALL_DIR"
shift
continue
;;
uninstall)
if [ -z "$TARGET_SYSTEMS" ]; then
uninstall_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR")
else
uninstall_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ')
fi
shift
continue
;;
esac
done
|