Skip to content

Commit

Permalink
install: add checks
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Apr 26, 2024
1 parent 9b314d5 commit 5ac42c3
Showing 1 changed file with 58 additions and 39 deletions.
97 changes: 58 additions & 39 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,61 @@
#
set -eo pipefail

username="tractordev"
repo="wanix"
binpath="${TARGET:-/usr/local/bin}"

repoURL="https://github.com/${username}/${repo}"
releaseURL="$(curl -sI ${repoURL}/releases/latest | grep 'location:' | awk '{print $2}')"
version="$(basename $releaseURL | cut -c 2- | tr -d '\r')"

os=""
arch=""

# Detect operating system
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
# WSL uses msys or cygwin as OSTYPE
os="windows"
else
echo "Unsupported operating system"
exit 1
fi

# Detect architecture
if [[ "$(uname -m)" == "x86_64" ]]; then
arch="amd64"
elif [[ "$(uname -m)" == "arm64" ]]; then
arch="arm64"
else
echo "Unsupported architecture"
exit 1
fi

filename="${repo}_${version}_${os}_${arch}.zip"

curl -sSLO "${repoURL}/releases/download/v${version}/${filename}"
unzip $filename wanix -d $binpath
rm $filename
required_tools=("curl" "grep" "awk" "unzip")
for tool in "${required_tools[@]}"; do
if ! command -v $tool &>/dev/null; then
echo "Error: $tool is required but not installed. Please install it first."
exit 1
fi
done

main() {
local username="tractordev"
local repo="wanix"
local binpath="${TARGET:-/usr/local/bin}"

# Check if write permission is available
if [[ ! -w "$binpath" ]]; then
echo "Error: No write permission for $binpath. Try running with sudo or choose a different TARGET."
exit 1
fi

local repoURL="https://github.com/${username}/${repo}"
local releaseURL="$(curl -sI ${repoURL}/releases/latest | grep 'location:' | awk '{print $2}')"
local version="$(basename $releaseURL | cut -c 2- | tr -d '\r')"

local os=""
local arch=""

# Detect operating system
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
# WSL uses msys or cygwin as OSTYPE
os="windows"
else
echo "Error: Unsupported operating system"
exit 1
fi

# Detect architecture
if [[ "$(uname -m)" == "x86_64" ]]; then
arch="amd64"
elif [[ "$(uname -m)" == "arm64" ]]; then
arch="arm64"
else
echo "Error: Unsupported architecture"
exit 1
fi

local filename="${repo}_${version}_${os}_${arch}.zip"
curl -sSLO "${repoURL}/releases/download/v${version}/${filename}"
unzip $filename wanix -d $binpath
rm "./$filename"

echo "Executable wanix ${version} installed to ${binpath}"
}

main "$@"

0 comments on commit 5ac42c3

Please sign in to comment.