Reworked the unix launcher

- More posix compliance and less prone to failing on bad variables
- Replaced depreciated backquotes (`) with the 'which' command
- Reworked the entire terminal detection code for easier editing and testing
This commit is contained in:
Kris Scott 2019-04-22 17:37:01 +09:00 committed by Jesse Plamondon-Willard
parent 6521df7b13
commit 304d89cd19
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 60 additions and 35 deletions

View File

@ -1,14 +1,14 @@
#!/bin/bash
#!/usr/bin/env bash
# MonoKickstart Shell Script
# Written by Ethan "flibitijibibo" Lee
# Modified for StardewModdingAPI by Viz and Pathoschild
# Move to script's directory
cd "`dirname "$0"`"
cd "$(dirname "$0")" || exit $?
# Get the system architecture
UNAME=`uname`
ARCH=`uname -m`
UNAME=$(uname)
ARCH=$(uname -m)
# MonoKickstart picks the right libfolder, so just execute the right binary.
if [ "$UNAME" == "Darwin" ]; then
@ -39,18 +39,18 @@ if [ "$UNAME" == "Darwin" ]; then
# launch SMAPI
cp StardewValley.bin.osx StardewModdingAPI.bin.osx
open -a Terminal ./StardewModdingAPI.bin.osx $@
open -a Terminal ./StardewModdingAPI.bin.osx "$@"
else
# choose launcher
LAUNCHER=""
if [ "$ARCH" == "x86_64" ]; then
ln -sf mcs.bin.x86_64 mcs
cp StardewValley.bin.x86_64 StardewModdingAPI.bin.x86_64
LAUNCHER="./StardewModdingAPI.bin.x86_64 $@"
LAUNCHER="./StardewModdingAPI.bin.x86_64 $*"
else
ln -sf mcs.bin.x86 mcs
cp StardewValley.bin.x86 StardewModdingAPI.bin.x86
LAUNCHER="./StardewModdingAPI.bin.x86 $@"
LAUNCHER="./StardewModdingAPI.bin.x86 $*"
fi
# get cross-distro version of POSIX command
@ -62,36 +62,61 @@ else
fi
# open SMAPI in terminal
if $COMMAND xterm 2>/dev/null; then
xterm -e "$LAUNCHER"
elif $COMMAND x-terminal-emulator 2>/dev/null; then
# Terminator converts -e to -x when used through x-terminal-emulator for some reason (per
# `man terminator`), which causes an "unable to find shell" error. If x-terminal-emulator
# is mapped to Terminator, invoke it directly instead.
if [[ "$(readlink -e $(which x-terminal-emulator))" == *"/terminator" ]]; then
terminator -e "sh -c 'TERM=xterm $LAUNCHER'"
# First let's try xterm (best compatiblity) or find a sensible default
# Setting TERMINAL should let you override this at the commandline for easier testing of other terminals.
for terminal in "$TERMINAL" xterm x-terminal-emulator kitty terminator xfce4-terminal gnome-terminal konsole terminal termite; do
if $COMMAND "$terminal" 2>/dev/null; then
# Find the true shell behind x-terminal-emulator
if [ "$(basename "$(readlink -ef which "$terminal")")" != "x-terminal-emulator" ]; then
export LAUNCHTERM=$terminal
break;
else
x-terminal-emulator -e "sh -c 'TERM=xterm $LAUNCHER'"
export LAUNCHTERM="$(basename "$(readlink -ef which x-terminal-emulator)")"
# Remember that we are using x-terminal-emulator just in case it points outside the $PATH
export XTE=1
break;
fi
elif $COMMAND xfce4-terminal 2>/dev/null; then
xfce4-terminal -e "sh -c 'TERM=xterm $LAUNCHER'"
elif $COMMAND gnome-terminal 2>/dev/null; then
gnome-terminal -e "sh -c 'TERM=xterm $LAUNCHER'"
elif $COMMAND konsole 2>/dev/null; then
konsole -p Environment=TERM=xterm -e "$LAUNCHER"
elif $COMMAND terminal 2>/dev/null; then
terminal -e "sh -c 'TERM=xterm $LAUNCHER'"
elif $COMMAND termite 2>/dev/null; then
termite -e "sh -c 'TERM=xterm $LAUNCHER'"
else
sh -c 'TERM=xterm $LAUNCHER'
fi
done
# some Linux users get error 127 (command not found) from the above block, even though
# `command -v` indicates the command is valid. As a fallback, launch SMAPI without a terminal when
# that happens and pass in an argument indicating SMAPI shouldn't try writing to the terminal
# (which can be slow if there is none).
if [ -z "$LAUNCHTERM" ]; then
# We failed to detect a terminal, run in current shell, or with no output
sh -c 'TERM=xterm $LAUNCHER'
if [ $? -eq 127 ]; then
$LAUNCHER --no-terminal
fi
exit
fi
# Now let's run the terminal and account for quirks, or if no terminal was found, run in the current process.
case $LAUNCHTERM in
terminator)
# Terminator converts -e to -x when used through x-terminal-emulator for some reason
if $XTE; then
terminator -e "sh -c 'TERM=xterm $LAUNCHER'"
else
terminator -x "sh -c 'TERM=xterm $LAUNCHER'"
fi
;;
kitty)
# Kitty overrides the TERM varible unless you set it explicitly
kitty -o term=xterm $LAUNCHER
;;
xterm)
$LAUNCHTERM -e "$LAUNCHER"
;;
xfce4-terminal|gnome-terminal|terminal|termite)
$LAUNCHTERM -e "sh -c 'TERM=xterm $LAUNCHER'"
;;
konsole)
konsole -p Environment=TERM=xterm -e "$LAUNCHER"
;;
*)
# If we don't know the terminal, just try to run it in the current shell.
sh -c 'TERM=xterm $LAUNCHER'
# if THAT fails, launch with no output
if [ $? -eq 127 ]; then
$LAUNCHER --no-terminal
fi
esac
fi