Skip to content

Commit 99a3375

Browse files
author
Jacob Hrbek
committed
steam-check: Refactor
Refactored steam-check to be more managable, could be improved with a proper abstracting
1 parent 2c1afc4 commit 99a3375

1 file changed

Lines changed: 330 additions & 11 deletions

File tree

config/steam-check.sh

Lines changed: 330 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,331 @@
1-
#!/bin/bash
2-
if [ -z ${STEAM_RUNTIME} ]; then
3-
echo "WARNING: ${FERAL_GAME_NAME_FULL} not launched within the steam runtime"
4-
echo " This is likely incorrect and is not officially supported"
5-
echo " Launching steam in 3 seconds with steam://rungameid/${FERAL_GAME_STEAMID}"
6-
sleep 3
7-
steam "steam://rungameid/${FERAL_GAME_STEAMID}"
8-
exit
9-
elif [ ${STEAM_RUNTIME} == 0 ]; then
10-
echo "WARNING: ${FERAL_GAME_NAME_FULL} launched with STEAM_RUNTIME=0"
11-
echo " We recommend using the steam runtime if possible"
1+
#!/bin/sh
2+
# Created by Feral Interactive somewhere around 2017 under MIT license <https://mit-license.org/>
3+
# Refactored by Jacob Hrbek <kreyren@rixotstudio.cz> under GPLv3 license <https://www.gnu.org/licenses/gpl-3.0.en.html> in 23.06.2020 19:48:54 CEST
4+
5+
# shellcheck shell=sh # Written as POSIX-compatible
6+
7+
###! Script to check if steam runtime is provided
8+
###! Requires:
9+
###! - Standard toolchain
10+
###! Exit codes:
11+
###! - FIXME-DOCS(Krey): Defined in die()
12+
###! Platforms:
13+
###! - [ ] Linux
14+
###! - [ ] Debian
15+
###! - [ ] Ubuntu
16+
###! - [ ] Fedora
17+
###! - [ ] NixOS
18+
###! - [ ] Archlinux
19+
###! - [ ] Alpine
20+
###! - [ ] FreeBSD
21+
###! - [ ] Darwin
22+
###! - [ ] Redox
23+
###! - [ ] ReactOS
24+
###! - [ ] Windows
25+
###! - [ ] Windows/Cygwin
26+
27+
# Command overrides
28+
# NOTICE(Krey): These might be required on Windows/Cygwin or systems without standardized toolchain
29+
[ -z "$PRINTF" ] && PRINTF="printf"
30+
[ -z "$WGET" ] && WGET="wget"
31+
[ -z "$CURL" ] && CURL="curl"
32+
[ -z "$ARIA2C" ] && ARIA2C="aria2c"
33+
[ -z "$CHMOD" ] && CHMOD="chmod"
34+
[ -z "$UNAME" ] && UNAME="uname"
35+
[ -z "$TR" ] && TR="tr"
36+
[ -z "$SED" ] && SED="sed"
37+
[ -z "$GREP" ] && GREP="grep"
38+
[ -z "$EXIT" ] && EXIT="exit"
39+
[ -z "$SLEEP" ] && SLEEP="sleep"
40+
[ -z "$STEAM" ] && STEAM="steam"
41+
[ -z "$MKDIR" ] && MKDIR="mkdir"
42+
[ -z "$DATE" ] && DATE="date"
43+
44+
45+
# Customization of the output
46+
## efixme
47+
[ -z "$EFIXME_FORMAT_STRING" ] && EFIXME_FORMAT_STRING="FIXME: %s\n"
48+
[ -z "$EFIXME_FORMAT_STRING_LOG" ] && EFIXME_FORMAT_STRING="${logPrefix}FIXME: %s\n"
49+
[ -z "$EFIXME_FORMAT_STRING_DEBUG" ] && EFIXME_FORMAT_STRING_DEBUG="FIXME($myName:$0): %s\n"
50+
[ -z "$EFIXME_FORMAT_STRING_DEBUG_LOG" ] && EFIXME_FORMAT_STRING_DEBUG_LOG="${logPrefix}FIXME($myName:$0): %s\n"
51+
## eerror
52+
[ -z "$EERROR_FORMAT_STRING" ] && EERROR_FORMAT_STRING="ERROR: %s\n"
53+
[ -z "$EERROR_FORMAT_STRING_LOG" ] && EERROR_FORMAT_STRING_LOG="${logPrefix}ERROR: %s\n"
54+
[ -z "$EERROR_FORMAT_STRING_DEBUG" ] && EERROR_FORMAT_STRING_DEBUG="ERROR($myName:$0): %s\n"
55+
[ -z "$EERROR_FORMAT_STRING_DEBUG_LOG" ] && EERROR_FORMAT_STRING_DEBUG_LOG="${logPrefix}ERROR($myName:$0): %s\n"
56+
## edebug
57+
[ -z "$EERROR_FORMAT_STRING" ] && EERROR_FORMAT_STRING="ERROR: %s\n"
58+
[ -z "$EERROR_FORMAT_STRING_LOG" ] && EERROR_FORMAT_STRING_LOG="${logPrefix}ERROR: %s\n"
59+
[ -z "$EERROR_FORMAT_STRING_DEBUG" ] && EERROR_FORMAT_STRING_DEBUG="ERROR($myName:$0): %s\n"
60+
[ -z "$EERROR_FORMAT_STRING_DEBUG_LOG" ] && EERROR_FORMAT_STRING_DEBUG_LOG="${logPrefix}ERROR($myName:$0): %s\n"
61+
## einfo
62+
[ -z "$EINFO_FORMAT_STRING" ] && EINFO_FORMAT_STRING="INFO: %s\n"
63+
[ -z "$EINFO_FORMAT_STRING_LOG" ] && EINFO_FORMAT_STRING_LOG="${logPrefix}INFO: %s\n"
64+
[ -z "$EINFO_FORMAT_STRING_DEBUG" ] && EINFO_FORMAT_STRING_DEBUG="INFO($myName:$0): %s\n"
65+
[ -z "$EINFO_FORMAT_STRING_DEBUG_LOG" ] && EINFO_FORMAT_STRING_DEBUG_LOG="${logPrefix}INFO($myName:$0): %s\n"
66+
## die
67+
[ -z "$DIE_FORMAT_STRING" ] && DIE_FORMAT_STRING="FATAL: %s in script '$myName' located at '$0'\\n"
68+
[ -z "$DIE_FORMAT_STRING_LOG" ] && DIE_FORMAT_STRING_LOG="${logPath}FATAL: %s in script '$myName' located at '$0'\\n"
69+
[ -z "$DIE_FORMAT_STRING_DEBUG" ] && DIE_FORMAT_STRING_DEBUG="FATAL($myName:$1): %s\n"
70+
[ -z "$DIE_FORMAT_STRING_DEBUG_LOG" ] && DIE_FORMAT_STRING_DEBUG_LOG="${logPrefix}FATAL($myName:$1): %s\\n"
71+
### Fixme trap
72+
[ -z "$DIE_FORMAT_STRING_FIXME" ] && DIE_FORMAT_STRING_FIXME="FATAL: %s in script '$myName' located at '$0', fixme?\n"
73+
[ -z "$DIE_FORMAT_STRING_FIXME_LOG" ] && DIE_FORMAT_STRING_FIXME_LOG="${logPrefix}FATAL: %s, fixme?\n"
74+
[ -z "$DIE_FORMAT_STRING_FIXME_DEBUG" ] && DIE_FORMAT_STRING_FIXME_DEBUG="FATAL($myName:$1): %s, fixme?\n"
75+
[ -z "$DIE_FORMAT_STRING_FIXME_DEBUG_LOG" ] && DIE_FORMAT_STRING_FIXME_DEBUG_LOG="${logPrefix}FATAL($myName:$1): %s, fixme?\\n"
76+
### Unexpected trap
77+
[ -z "$DIE_FORMAT_STRING_UNEXPECTED" ] && DIE_FORMAT_STRING_UNEXPECTED="FATAL: Unexpected happend while %s in $myName located at $0\\n"
78+
[ -z "$DIE_FORMAT_STRING_UNEXPECTED_LOG" ] && DIE_FORMAT_STRING_UNEXPECTED_LOG="${logPrefix}FATAL: Unexpected happend while %s\\n"
79+
[ -z "$DIE_FORMAT_STRING_UNEXPECTED_DEBUG" ] && DIE_FORMAT_STRING_UNEXPECTED_DEBUG="FATAL($myName:$1): Unexpected happend while %s in $myName located at $0\\n"
80+
[ -z "$DIE_FORMAT_STRING_UNEXPECTED_DEBUG_LOG" ] && DIE_FORMAT_STRING_UNEXPECTED_DEBUG="${logPrefix}FATAL($myName:$1): Unexpected happend while %s\\n"
81+
82+
# Exit on anything unexpected
83+
set -e
84+
85+
# NOTICE(Krey): By default busybox outputs a full path in '$0' this is used to strip it
86+
myName="${0##*/}"
87+
88+
# To identify upstream for issues/contribs
89+
UPSTREAM="https://github.com/FeralInteractive/ferallinuxscripts"
90+
91+
# Used to prefix logs with timestemps, uses ISO 8601 by default
92+
logPrefix="[ $("$DATE" -u +"%Y-%m-%dT%H:%M:%SZ") ] "
93+
94+
# Path to which we will save logs
95+
# NOTICE(Krey): To avoid storing file '$HOME/.some-name.sh.log' we are stripping the '.sh' here
96+
# FIXME: This may fail if '$HOME/.local/share' is not present
97+
if [ -d "$HOME/.local/share" ]; then
98+
logPath="${XDG_DATA_HOME:-$HOME/.local/share}/${myName%%.sh}.log"
99+
elif [ ! -d "$HOME/.local/share" ]; then
100+
eerror "Standard directory '$HOME/.local/share' is not available on this system, trying to resolve.."
101+
logPath="${XDG_DATA_HOME:-$HOME/.local/share}/${myName%%.sh}.log"
102+
"$MKDIR" -p "$HOME/.local/share" || "$PRINTF" 'ERROR: %s\n' "Unable to create the required directory '$HOME/.local/share' for logs, using '$HOME' instead" && logPath="$HOME/${myName%%.sh}.log"
103+
104+
# Self-check
105+
if [ -d "$HOME/.local/share" ]; then
106+
"$PRINTF" 'INFO: %s\n' "Directory '$HOME/.local/share' has been confirmed to be present for logs"
107+
elif [ ! -d "$HOME/.local/share" ]; then
108+
"$PRINTF" 'FATAL: %s\n' "Self-check for directory '$HOME/.local/share' failed"
109+
"$EXIT" 1
110+
else
111+
"$PRINTF" 'FATAL: Unexpected happend while %s\n' "self-checking log path"
112+
"$EXIT" 255
113+
fi
114+
else
115+
"$PRINTF" 'FATAL: Unexpected happend while %s\n' "checking log path"
116+
"$EXIT" 255
12117
fi
118+
119+
# inicialize the script in logs
120+
"$PRINTF" '%s\n' "Started $myName on $("$UNAME" -s || true) at $(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$logPath"
121+
122+
# Simple assertion wrapper with message
123+
# NOTICE(Krey): Aliases are required for posix-compatible line output (https://gist.github.com/Kreyren/4fc76d929efbea1bc874760e7f78c810)
124+
# NOTICE(Krey): Aliases are required for posix-compatible line output (https://gist.github.com/Kreyren/4fc76d929efbea1bc874760e7f78c810)
125+
die() { funcname=die
126+
case "$2" in
127+
38|fixme) # FIXME
128+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
129+
"$PRINTF" "$DIE_FORMAT_STRING_FIXME" "$3"
130+
"$PRINTF" "$DIE_FORMAT_STRING_FIXME_LOG" "$3" >> "$logPath"
131+
funcname="$myName"
132+
elif [ "$DEBUG" = 1 ]; then
133+
"$PRINTF" "$DIE_FORMAT_STRING_FIXME_DEBUG" "$3"
134+
"$PRINTF" "$DIE_FORMAT_STRING_FIXME_DEBUG_LOG" "$3" >> "$logPath"
135+
funcname="$myName"
136+
else
137+
# NOTICE(Krey): Do not use die() here
138+
"$PRINTF" 'FATAL: %s\n' "Unexpected happend while processing variable DEBUG with value '$DEBUG' in $funcname"
139+
fi
140+
141+
"$EXIT" 38
142+
;;
143+
255) # Unexpected trap
144+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
145+
"$PRINTF" "$DIE_FORMAT_STRING_UNEXPECTED" "$3"
146+
"$PRINTF" "$DIE_FORMAT_STRING_UNEXPECTED_LOG" "$3" >> "$logPath"
147+
funcname="$myName"
148+
elif [ "$DEBUG" = 1 ]; then
149+
"$PRINTF" "$DIE_FORMAT_STRING_UNEXPECTED_DEBUG" "$3"
150+
"$PRINTF" "$DIE_FORMAT_STRING_UNEXPECTED_DEBUG_LOG" "$3" >> "$logPath"
151+
funcname="$myName"
152+
else
153+
# NOTICE(Krey): Do not use die() here
154+
"$PRINTF" "$DIE_FORMAT_STRING" "Unexpected happend while processing variable DEBUG with value '$DEBUG' in $funcname"
155+
fi
156+
;;
157+
*)
158+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
159+
"$PRINTF" "$DIE_FORMAT_STRING" "$3"
160+
"$PRINTF" "$DIE_FORMAT_STRING_LOG" "$3" >> "$logPath"
161+
funcname="$myName"
162+
elif [ "$DEBUG" = 1 ]; then
163+
"$PRINTF" "$DIE_FORMAT_STRING_DEBUG" "$3"
164+
"$PRINTF" "$DIE_FORMAT_STRING_DEBUG_LOG" "$3" >> "$logPath"
165+
funcname="$myName"
166+
else
167+
# NOTICE(Krey): Do not use die() here
168+
"$PRINTF" 'FATAL: %s\n' "Unexpected happend while processing variable DEBUG with value '$DEBUG' in $funcname"
169+
fi
170+
esac
171+
172+
"$EXIT" "$2"
173+
174+
# In case invalid argument has been parsed in $2
175+
"$EXIT" 255
176+
}; alias die='die "$LINENO"'
177+
178+
einfo() { funcname=einfo
179+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
180+
"$PRINTF" "$EINFO_FORMAT_STRING" "$2"
181+
"$PRINTF" "$EINFO_FORMAT_STRING_LOG" "$2" >> "$logPath"
182+
funcname="$myName"
183+
return 0
184+
elif [ "$DEBUG" = 1 ]; then
185+
"$PRINTF" "$EINFO_FORMAT_STRING_DEBUG" "$2"
186+
"$PRINTF" "$EINFO_FORMAT_STRING_DEBUG_LOG" "$2" >> "$logPath"
187+
funcname="$myName"
188+
return 0
189+
else
190+
die 255 "processing variable DEBUG with value '$DEBUG' in $funcname"
191+
fi
192+
}; alias einfo='einfo "$LINENO"'
193+
194+
ewarn() { funcname=ewarn
195+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
196+
"$PRINTF" "$EWARN_FORMAT_STRING" "$2"
197+
"$PRINTF" "$EWARN_FORMAT_STRING_LOG" "$2" >> "$logPath"
198+
funcname="$myName"
199+
return 0
200+
elif [ "$DEBUG" = 1 ]; then
201+
"$PRINTF" "$EWARN_FORMAT_STRING_DEBUG" "$2"
202+
"$PRINTF" "$EWARN_FORMAT_STRING_DEBUG_LOG" "$2" >> "$logPath"
203+
funcname="$myName"
204+
return 0
205+
else
206+
die 255 "processing variable DEBUG with value '$DEBUG' in $funcname"
207+
fi
208+
}; alias ewarn='ewarn "$LINENO"'
209+
210+
eerror() { funcname=eerror
211+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
212+
"$PRINTF" "$EERROR_FORMAT_STRING" "$2"
213+
"$PRINTF" "$EERROR_FORMAT_STRING_LOG" "$2" >> "$logPath"
214+
funcname="$myName"
215+
return 0
216+
elif [ "$DEBUG" = 1 ]; then
217+
"$PRINTF" "$EERROR_FORMAT_STRING_DEBUG" "$2"
218+
"$PRINTF" "$EERROR_FORMAT_STRING_DEBUG_LOG" "$2" >> "$logPath"
219+
funcname="$myName"
220+
return 0
221+
else
222+
die 255 "processing variable DEBUG with value '$DEBUG' in $funcname"
223+
fi
224+
}; alias eerror='eerror "$LINENO"'
225+
226+
edebug() { funcname=edebug
227+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
228+
"$PRINTF" "$EDEBUG_FORMAT_STRING" "$2"
229+
"$PRINTF" "$EDEBUG_FORMAT_STRING_LOG" "$2" >> "$logPath"
230+
funcname="$myName"
231+
return 0
232+
elif [ "$DEBUG" = 1 ]; then
233+
"$PRINTF" "$EDEBUG_FORMAT_STRING_DEBUG" "$2"
234+
"$PRINTF" "$EDEBUG_FORMAT_STRING_DEBUG_LOG" "$2" >> "$logPath"
235+
funcname="$myName"
236+
return 0
237+
else
238+
die 255 "processing variable DEBUG with value '$DEBUG' in $funcname"
239+
fi
240+
}; alias eerror='eerror "$LINENO"'
241+
242+
efixme() { funcname=efixme
243+
if [ "$IGNORE_FIXME" = 1 ]; then
244+
true
245+
elif [ "$IGNORE_FIXME" = 0 ] || [ -z "$IGNORE_FIXME" ]; then
246+
if [ "$DEBUG" = 0 ] || [ -z "$DEBUG" ]; then
247+
"$PRINTF" "$EFIXME_FORMAT_STRING" "$2"
248+
"$PRINTF" "$EFIXME_FORMAT_STRING" "$2" >> "$logPath"
249+
funcname="$myName"
250+
return 0
251+
elif [ "$DEBUG" = 1 ]; then
252+
"$PRINTF" "$EFIXME_FORMAT_STRING_DEBUG" "$2"
253+
"$PRINTF" "$EFIXME_FORMAT_STRING_DEBUG_LOG" "$2" >> "$logPath"
254+
funcname="$myName"
255+
return 0
256+
else
257+
die 255 "processing DEBUG variable with value '$DEBUG' in $funcname"
258+
fi
259+
else
260+
die 255 "processing variable IGNORE_FIXME with value '$IGNORE_FIXME' in $0"
261+
fi
262+
}; alias efixme='efixme "$LINENO"'
263+
264+
# Identify system
265+
if "$COMMAND" -v "$UNAME" 1>/dev/null; then
266+
unameKernel="$("$UNAME" -s)"
267+
edebug "Identified the kernel as '$unameKernel"
268+
case "$unameKernel" in
269+
Linux)
270+
KERNEL="$unameKernel"
271+
272+
# Assume Linux Distro and release
273+
# NOTICE(Krey): We are expecting this to return a lowercase value
274+
if "$COMMAND" -v "$LSB_RELEASE" 1>/dev/null; then
275+
assumedDistro="$("$LSB_RELEASE" -si | "$TR" :[upper]: :[lower]:)"
276+
assumedRelease="$("$LSB_RELEASE" -cs | "$TR" :[upper]: :[lower]:)"
277+
elif ! "$COMMAND" -v "$LSB_RELEASE" 1>/dev/null && [ -f /etc/os-release ]; then
278+
assumedDistro="$("$GREP" -o "^ID\=.*" /etc/os-release | "$SED" "s/ID=//gm")"
279+
assumedRelease="$("$GREP" -o "^VERSION_CODENAME\=.*" /etc/os-release | "$SED" "s/^VERSION_CODENAME=//gm")"
280+
elif ! "$COMMAND" -v "$LSB_RELEASE" 1>/dev/null && [ ! -f /etc/os-release ]; then
281+
die 1 "Unable to identify linux distribution using command 'lsb_release' nor file '/etc/os-release'"
282+
else
283+
die 255 "attempting to assume linux distro and release"
284+
fi
285+
286+
edebug "Identified distribution as '$assumedDistro'"
287+
edebug "Identified distribution release as '$assumedRelease'"
288+
289+
# Verify Linux Distro
290+
efixme "Add sanitization logic for other linux distributions"
291+
case "$assumedDistro" in
292+
ubuntu | debian | fedora | nixos | opensuse | gentoo | exherbo)
293+
DISTRO="$assumedDistro"
294+
;;
295+
*) die fixme "Unexpected Linux distribution '$assumedDistro' has been detected."
296+
esac
297+
298+
# Verify Linux Distro Release
299+
efixme "Sanitize verification of linux distro release"
300+
RELEASE="$assumedRelease"
301+
;;
302+
FreeBSD | Redox | Darwin | Windows)
303+
KERNEL="$unameKernel"
304+
;;
305+
*) die 255 "Unexpected kernel '$unameKernel'"
306+
esac
307+
elif ! "$COMMAND" -v "$UNAME" 1>/dev/null; then
308+
die 1 "Standard command '$UNAME' is not available on this system, unable to identify kernel"
309+
else
310+
die 255 "Identifying system"
311+
fi
312+
313+
case "$KERNEL" in
314+
linux)
315+
# FIXME-QA(Krey): Are other distros and releases a concern?
316+
if [ -z "$STEAM_RUNTIME" ]; then
317+
"$PRINTF" '%s\n' \
318+
"WARNING: $FERAL_GAME_NAME_FULL not launched within the steam runtime" \
319+
" This is likely incorrect and is not officially supported" \
320+
" Launching steam in 3 seconds with steam://rungameid/$FERAL_GAME_STEAMID" \
321+
"$SLEEP" 3
322+
"$STEAM" "steam://rungameid/$FERAL_GAME_STEAMID" || die 1 "Steam failed to start steam game '$FERAL_GAME_NAME_FULL' with ID '$FERAL_GAME_STEAMID"
323+
"$EXIT" 0
324+
elif [ "$STEAM_RUNTIME" = 0 ]; then
325+
"$PRINTF" '%s\n' \
326+
"WARNING: $FERAL_GAME_NAME_FULL launched with STEAM_RUNTIME=0" \
327+
" We recommend using the steam runtime if possible"
328+
fi
329+
;;
330+
*) die fixme "Kernel '$KERNEL' is not supported, see $UPSTREAM for contribution or issue tracking"
331+
esac

0 commit comments

Comments
 (0)