diff --git a/install.ps1 b/install.ps1 index e6f6ab3..1b35dfd 100644 --- a/install.ps1 +++ b/install.ps1 @@ -1,96 +1,88 @@ -param( - [string]$repoowner = "ibigio", - [string]$reponame = "shell-ai", - [string]$toolname = "shell-ai", - [string]$toolsymlink = "q", - [switch]$help -) - -if ($help) { - Write-Host "shell-ai Installer Help!" - Write-Host " Usage: " - Write-Host " shell-ai -help " - Write-Host " shell-ai -repoowner " - Write-Host " shell-ai -reponame " - Write-Host " shell-ai -toolname " - Write-Host " shell-ai -toolsymlink " - +# Script parameters +REPOOWNER="ibigio" +REPONAME="shell-ai" +TOOLNAME="shell-ai" +TOOLSYMLINK="q" +HELP=false + +# Print help message if requested +if [ "$HELP" = true ]; then + echo "shell-ai Installer Help!" + echo "Usage: " + echo " -help " + echo " -repoowner " + echo " -reponame " + echo " -toolname " + echo " -toolsymlink " exit 0 -} +fi -# if user isnt admin then quit -function IsUserAdministrator { - $user = [Security.Principal.WindowsIdentity]::GetCurrent() - $principal = New-Object Security.Principal.WindowsPrincipal($user) - return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -} +# Detect if running as root +if [ "$EUID" -ne 0 ]; then + echo "Not running as root. Will attempt user-level installation..." + INSTALL_DIR="$HOME/.local/bin" +else + INSTALL_DIR="/usr/local/bin" +fi -if (-not (IsUserAdministrator)) { - Write-Host "Please run as administrator" - exit 1 -} +# Check for required tools +command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required but not installed. Aborting."; exit 1; } +command -v unzip >/dev/null 2>&1 || { echo >&2 "unzip is required but not installed. Aborting."; exit 1; } # Detect the platform (architecture and OS) -$ARCH = $null -$OS = "Windows" +ARCH="" +OS="linux" - -if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") { - $ARCH = "x86_64" -} elseif ($env:PROCESSOR_ARCHITECTURE -eq "arm64") { - $ARCH = "arm64" -} else { - $ARCH = "i386" -} - -if ($env:OS -notmatch "Windows") { - Write-Host "You are running the powershell script on a non-windows platform. Please use the install.sh script instead." -} +if [ "$(uname -m)" = "x86_64" ]; then + ARCH="x86_64" +elif [ "$(uname -m)" = "aarch64" ]; then + ARCH="arm64" +else + ARCH="i386" +fi # Fetch the latest release tag from GitHub API -$API_URL = "https://api.github.com/repos/$repoowner/$reponame/releases/latest" -$LATEST_TAG = (Invoke-RestMethod -Uri $API_URL).tag_name +API_URL="https://api.github.com/repos/$REPOOWNER/$REPONAME/releases/latest" +LATEST_TAG=$(curl -s $API_URL | grep -oP '"tag_name": "\K(.*)(?=")') # Set the download URL based on the platform and latest release tag -$DOWNLOAD_URL = "https://github.com/$repoowner/$reponame/releases/download/$LATEST_TAG/${toolname}_${OS}_${ARCH}.zip" +DOWNLOAD_URL="https://github.com/$REPOOWNER/$REPONAME/releases/download/$LATEST_TAG/${TOOLNAME}_${OS}_${ARCH}.zip" -Write-Host $DOWNLOAD_URL +echo "Downloading from: $DOWNLOAD_URL" # Download the ZIP file -Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile "${toolname}.zip" +curl -L "$DOWNLOAD_URL" -o "${TOOLNAME}.zip" +if [ $? -ne 0 ]; then + echo "Download failed. Please check the URL or your internet connection." + exit 1 +fi # Extract the ZIP file -$extractedDir = "${toolname}-temp" -Expand-Archive -Path "${toolname}.zip" -DestinationPath $extractedDir -Force - -# check if the file already exists -$toolPath = "C:\Program Files\shell-ai\${toolsymlink}.exe" -if (Test-Path $toolPath) { - Remove-Item $toolPath -} else { - New-Item -ItemType Directory -Path "C:\Program Files\shell-ai\" -} - -# Add the file to path -$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "User") - -# Append the desired path to the current PATH value if it's not already present -if (-not ($currentPath -split ";" | Select-String -SimpleMatch "C:\Program Files\shell-ai\")) { - $updatedPath = $currentPath + ";" + "C:\Program Files\shell-ai\" - - # Set the updated PATH value - [System.Environment]::SetEnvironmentVariable("PATH", $updatedPath, "User") # Use "User" instead of "Machine" for user-level PATH +EXTRACTED_DIR="${TOOLNAME}-temp" +unzip -o "${TOOLNAME}.zip" -d "$EXTRACTED_DIR" +if [ $? -ne 0 ]; then + echo "Failed to extract zip file." + exit 1 +fi - Write-Host "The path has been added to the PATH variable. You may need to restart applications to see the changes." -ForegroundColor Red -} +# Move the binary to the installation directory +if [ ! -d "$INSTALL_DIR" ]; then + mkdir -p "$INSTALL_DIR" +fi -# Make the binary executable -Move-Item "${extractedDir}/${toolname}.exe" $toolPath -Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted +mv "${EXTRACTED_DIR}/${TOOLNAME}" "$INSTALL_DIR/$TOOLSYMLINK" +chmod +x "$INSTALL_DIR/$TOOLSYMLINK" # Clean up -Remove-Item -Recurse -Force "${extractedDir}" -Remove-Item -Force "${toolname}.zip" +rm -rf "${EXTRACTED_DIR}" +rm -f "${TOOLNAME}.zip" + +# Add the installation directory to PATH if not already present +if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then + echo "export PATH=\$PATH:$INSTALL_DIR" >> ~/.bashrc + source ~/.bashrc + echo "Added $INSTALL_DIR to PATH. Restart your terminal or run 'source ~/.bashrc' to use the tool." +fi # Print success message -Write-Host "The $toolname has been installed successfully (version: $LATEST_TAG)." \ No newline at end of file +echo "$TOOLNAME has been installed successfully (version: $LATEST_TAG)!" diff --git a/install.sh b/install.sh index 5346d57..2f61cf8 100644 --- a/install.sh +++ b/install.sh @@ -1,15 +1,49 @@ #!/bin/bash -# Replace these values with your tool's information: +# Script parameters REPO_OWNER="ibigio" REPO_NAME="shell-ai" TOOL_NAME="shell-ai" TOOL_SYMLINK="q" +INSTALL_DIR="$HOME/.local/bin" +HELP=false -# Detect the platform (architecture and OS) +# Check for help flag +if [ "$HELP" = true ]; then + echo "shell-ai Installer Help!" + echo "Usage: " + echo " -help " + echo " -repoowner " + echo " -reponame " + echo " -toolname " + echo " -toolsymlink " + exit 0 +fi + +# Detect if running as root +if [ "$EUID" -eq 0 ]; then + INSTALL_DIR="/usr/local/bin" +fi + +# Ensure required tools are installed +if ! command -v curl &>/dev/null || ! command -v tar &>/dev/null; then + echo "Error: curl and tar must be installed." + exit 1 +fi + +# Detect system architecture ARCH="$(uname -m)" OS="$(uname -s | tr '[:upper:]' '[:lower:]')" +# Adjust architecture name for compatibility +if [ "$ARCH" = "x86_64" ]; then + ARCH="x86_64" +elif [ "$ARCH" = "aarch64" ]; then + ARCH="arm64" +else + ARCH="i386" +fi + # Fetch the latest release tag from GitHub API API_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest" LATEST_TAG=$(curl --silent $API_URL | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') @@ -17,20 +51,28 @@ LATEST_TAG=$(curl --silent $API_URL | grep '"tag_name":' | sed -E 's/.*"([^"]+)" # Set the download URL based on the platform and latest release tag DOWNLOAD_URL="https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$LATEST_TAG/${TOOL_NAME}_${OS}_${ARCH}.tar.gz" -echo $DOWNLOAD_URL +echo "Downloading from: $DOWNLOAD_URL" -# # Download and extract the binary -curl -L "$DOWNLOAD_URL" -o "${TOOL_NAME}.tar.gz" +# Download and extract the binary +curl -L "$DOWNLOAD_URL" -o "${TOOL_NAME}.tar.gz" || { echo "Download failed"; exit 1; } mkdir -p "${TOOL_NAME}-temp" -tar xzf "${TOOL_NAME}.tar.gz" -C "${TOOL_NAME}-temp" +tar xzf "${TOOL_NAME}.tar.gz" -C "${TOOL_NAME}-temp" || { echo "Extraction failed"; exit 1; } -# Make the binary executable -mv "${TOOL_NAME}-temp/${TOOL_NAME}" "/usr/local/bin/${TOOL_SYMLINK}" -chmod +x /usr/local/bin/"${TOOL_SYMLINK}" +# Move the binary to the installation directory +mkdir -p "$INSTALL_DIR" +mv "${TOOL_NAME}-temp/${TOOL_NAME}" "$INSTALL_DIR/$TOOL_SYMLINK" +chmod +x "$INSTALL_DIR/$TOOL_SYMLINK" -# # Clean up +# Clean up rm -rf "${TOOL_NAME}-temp" -rm "${TOOL_NAME}.tar.gz" +rm -f "${TOOL_NAME}.tar.gz" + +# Add the installation directory to PATH if needed +if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then + echo "export PATH=\$PATH:$INSTALL_DIR" >> ~/.bashrc + source ~/.bashrc + echo "Added $INSTALL_DIR to PATH. Restart terminal or run 'source ~/.bashrc'." +fi # Print success message -echo "The $TOOL_NAME has been installed successfully (version: $LATEST_TAG)." \ No newline at end of file +echo "$TOOL_NAME has been installed successfully (version: $LATEST_TAG)!"