diff options
author | Kris Scott <kris@syntosis.net> | 2019-04-22 17:37:01 +0900 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-14 16:54:24 -0400 |
commit | 304d89cd193671942fd96b2eaca31ab18f9304f1 (patch) | |
tree | f36279ec97baf69eb66d9f4297ed2bd78be9a1e9 /src/SMAPI.Installer | |
parent | 6521df7b131924835eb797251c1e956fae0d6e13 (diff) | |
download | SMAPI-304d89cd193671942fd96b2eaca31ab18f9304f1.tar.gz SMAPI-304d89cd193671942fd96b2eaca31ab18f9304f1.tar.bz2 SMAPI-304d89cd193671942fd96b2eaca31ab18f9304f1.zip |
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
Diffstat (limited to 'src/SMAPI.Installer')
-rw-r--r-- | src/SMAPI.Installer/unix-launcher.sh | 95 |
1 files changed, 60 insertions, 35 deletions
diff --git a/src/SMAPI.Installer/unix-launcher.sh b/src/SMAPI.Installer/unix-launcher.sh index a98d527d..d6c3a004 100644 --- a/src/SMAPI.Installer/unix-launcher.sh +++ b/src/SMAPI.Installer/unix-launcher.sh @@ -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'" - else - x-terminal-emulator -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 + 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 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 + done + + 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 - # 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 [ $? -eq 127 ]; then - $LAUNCHER --no-terminal - 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 |