Skip to content

Commit

Permalink
Added autosetup script
Browse files Browse the repository at this point in the history
  • Loading branch information
baalimago committed Jun 2, 2024
1 parent 0d54eb7 commit 32d934b
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# Function to get the latest release download URL for the specified OS and architecture
get_latest_release_url() {
repo="baalimago/clai"
os="$1"
arch="$2"

# Fetch the latest release data from GitHub API
release_data=$(curl -s "https://api.github.com/repos/$repo/releases/latest")

# Extract the asset URL for the specified OS and architecture
download_url=$(echo "$release_data" | grep "browser_download_url" | grep "$os" | grep "$arch" | cut -d '"' -f 4)

echo "$download_url"
}

# Detect the OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
else
echo "Unsupported OS: $OSTYPE"
exit 1
fi

# Detect the architecture
arch=$(uname -m)
case "$arch" in
x86_64)
arch="amd64"
;;
armv7*)
arch="arm"
;;
aarch64)
arch="arm64"
;;
i?86)
arch="386"
;;
*)
echo "Unsupported architecture: $arch"
exit 1
;;
esac

printf "detected os: '$os', arch: '$arch'\n"

# Get the download URL for the latest release
printf "finding asset url..."
download_url=$(get_latest_release_url "$os" "$arch")
printf "OK!\n"

# Download the binary
tmp_file=$(mktemp)

printf "downloading binary..."
if ! curl -s -L -o "$tmp_file" "$download_url"; then
echo
echo "Failed to download the binary."
exit 1
fi
printf "OK!\n"

printf "setting file executable file permissions..."
# Make the binary executable

if ! chmod +x "$tmp_file"; then
echo
echo "Failed to make the binary executable. Try running the script with sudo."
exit 1
fi
printf "OK!\n"

# Move the binary to /usr/local/bin and handle permission errors
if ! mv "$tmp_file" /usr/local/bin/clai; then
echo "Failed to move the binary to /usr/local/bin/clai, see error above. Try running the script with sudo, or run 'mv $tmp_file <desired-position>'."
exit 1
fi

echo "clai installed successfully in /usr/local/bin, try it out with 'clai h'"

0 comments on commit 32d934b

Please sign in to comment.