Skip to content

Commit

Permalink
update install script to support config and XDG spec
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari committed Mar 7, 2022
1 parent aa5c4a7 commit 91aee6d
Showing 1 changed file with 95 additions and 14 deletions.
109 changes: 95 additions & 14 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

# This install script does 3 things:
# 1. Create the ~/.stew directory structure
# 1. Create the stew directory structure
# 2. Download the stew binary
# 3. Add ~/.stew/bin to PATH in ~/.zshrc or ~/.bashrc
# 3. Add the stew binary path to PATH in ~/.zshrc or ~/.bashrc

os=""
arch=""
exe=""
defaultStewPath=""
configPath=""

# Detect os
case "$(uname -s)" in
Darwin)
os="darwin"

if [ -z "$XDG_DATA_HOME" ]
then
defaultStewPath="$HOME/.local/share/stew"
else
defaultStewPath="$XDG_DATA_HOME/stew"
fi

if [ -z "$XDG_CONFIG_HOME" ]
then
configPath="$HOME/.config/stew"
else
configPath="$XDG_CONFIG_HOME/stew"
fi
;;
Linux)
os="linux"
if [ -z "$XDG_DATA_HOME" ]
then
defaultStewPath="$HOME/.local/share/stew"
else
defaultStewPath="$XDG_DATA_HOME/stew"
fi

if [ -z "$XDG_CONFIG_HOME" ]
then
configPath="$HOME/.config/stew"
else
configPath="$XDG_CONFIG_HOME/stew"
fi
;;
CYGWIN*|MSYS*|MINGW*)
os="windows"
exe=".exe"
defaultStewPath="$HOME/AppData/Local/stew"
configPath="$HOME/AppData/Local/stew/Config"
;;
esac

Expand All @@ -38,27 +69,77 @@ esac

if [ "$os" = "" ] || [ "$arch" = "" ]
then
echo "\033[31m\033[1mError:\033[0m Your current OS/arch is not supported by stew"
echo ""
echo "|||||||||||||||||||||"
echo "|| Error ||"
echo "|||||||||||||||||||||"
echo ""
echo "Your current OS/arch is not supported by stew"
echo ""
exit 1
fi

# 1. Create the ~/.stew directory structure
mkdir -p "$HOME"/.stew/bin
mkdir -p "$HOME"/.stew/pkg
# 1. Create the stew directory structure
stewPath=""
stewBinPath=""

read -r -t 60 -p "Set the stewPath. This will contain all stew data other than the binaries. (${defaultStewPath}): " stewPathInput
if [ -z "$stewPathInput" ]
then
stewPath="${defaultStewPath}"
else
stewPath="${stewPathInput/#~/$HOME}"
stewPath="${stewPath/#\$HOME/$HOME}"
stewPath="${stewPath/#\$PWD/$PWD}"
if [ -x "$(command -v dirname)" ] && [ -x "$(command -v basename)" ]
then
stewPath="$(cd "$(dirname "$stewPath")" || exit; pwd)/$(basename "$stewPath")"
fi
fi

read -r -t 60 -p "Set the stewBinPath. This is where the binaries will be installed by stew. (${defaultStewPath}/bin): " stewBinPathInput
if [ -z "$stewBinPathInput" ]
then
stewBinPath="${defaultStewPath}/bin"
else
stewBinPath="${stewBinPathInput/#~/$HOME}"
stewBinPath="${stewBinPath/#\$HOME/$HOME}"
stewBinPath="${stewBinPath/#\$PWD/$PWD}"
if [ -x "$(command -v dirname)" ] && [ -x "$(command -v basename)" ]
then
stewBinPath="$(cd "$(dirname "$stewBinPath")" || exit; pwd)/$(basename "$stewBinPath")"
fi
fi

mkdir -p "${stewPath}/bin"
mkdir -p "${stewPath}/pkg"
mkdir -p "${stewBinPath}"
mkdir -p "${configPath}"

echo "{
\"stewPath\": \"${stewPath}\",
\"stewBinPath\": \"${stewBinPath}\"
}" > "${configPath}/config.json"

# 2. Download the stew binary
curl -o "$HOME"/.stew/bin/stew${exe} -fsSL https://github.com/marwanhawari/stew/releases/latest/download/stew-${os}-${arch}${exe}
chmod +x "$HOME"/.stew/bin/stew${exe}
curl -o "${stewBinPath}/stew${exe}" -fsSL https://github.com/marwanhawari/stew/releases/latest/download/stew-${os}-${arch}${exe}
chmod +x "${stewBinPath}/stew${exe}"

# 3. Add ~/.stew/bin to PATH in ~/.zshrc or ~/.bashrc
# 3. Add the stew binary path to PATH in ~/.zshrc or ~/.bashrc
if [ -f "$HOME"/.zshrc ]
then
echo 'export PATH="$HOME/.stew/bin:$PATH"' >> "$HOME"/.zshrc
echo "export PATH=\"${stewBinPath}:\$PATH\"" >> "$HOME"/.zshrc
elif [ -f "$HOME"/.bashrc ]
then
echo 'export PATH="$HOME/.stew/bin:$PATH"' >> "$HOME"/.bashrc
echo "export PATH=\"${stewBinPath}:\$PATH\"" >> "$HOME"/.bashrc
else
echo "Make sure to add $HOME/.stew/bin to PATH"
echo "Make sure to add ${stewBinPath} to PATH"
fi

echo "\033[32m\033[1mSuccess:\033[0m Start a new terminal session to start using stew"
echo ""
echo "|||||||||||||||||||||"
echo "|| Success ||"
echo "|||||||||||||||||||||"
echo ""
echo "Start a new terminal session to start using stew"
echo ""

0 comments on commit 91aee6d

Please sign in to comment.