#!/bin/bash set -euo pipefail # download and install dropshell # 1. Check prerequisites # ----------------------------------------------------------------------------- if ! command -v curl >/dev/null 2>&1; then echo "Error: curl is required but not installed" >&2 echo "Please install curl: apt-get install curl (Debian/Ubuntu) or yum install curl (RedHat/CentOS)" >&2 exit 1 fi # 2. Determine architecture # ----------------------------------------------------------------------------- ARCH=$(uname -m) case "$ARCH" in x86_64|amd64) ARCH="x86_64" ;; aarch64|arm64) ARCH="aarch64" ;; *) echo "Warning: Unsupported architecture: $ARCH" >&2 echo "Attempting to continue with detected architecture..." >&2 ;; esac TARGET_PATH="${HOME}/.local/bin/dropshell" TARGET_DIR="$(dirname "${TARGET_PATH}")" # 3. Backup existing installation # ----------------------------------------------------------------------------- if [ -f "${TARGET_PATH}" ]; then BACKUP_PATH="${TARGET_PATH}.backup.$(date +%Y%m%d_%H%M%S)" echo "Backing up existing installation to ${BACKUP_PATH}" mv "${TARGET_PATH}" "${BACKUP_PATH}" fi mkdir -p "${TARGET_DIR}" # 4. Download with retries # ----------------------------------------------------------------------------- DOWNLOAD_URL="https://getbin.xyz/dropshell:latest-${ARCH}" TEMP_FILE="${TARGET_PATH}.tmp" MAX_RETRIES=3 RETRY_DELAY=2 echo "Downloading dropshell for ${ARCH}..." for i in $(seq 1 $MAX_RETRIES); do if curl -L --fail --connect-timeout 10 --max-time 60 \ -o "${TEMP_FILE}" "${DOWNLOAD_URL}" 2>/dev/null; then mv "${TEMP_FILE}" "${TARGET_PATH}" break else if [ $i -eq $MAX_RETRIES ]; then echo "Failed to download dropshell after $MAX_RETRIES attempts" >&2 [ -f "${TEMP_FILE}" ] && rm -f "${TEMP_FILE}" # Restore backup if available if [ -n "${BACKUP_PATH:-}" ] && [ -f "${BACKUP_PATH}" ]; then echo "Restoring previous installation from backup" mv "${BACKUP_PATH}" "${TARGET_PATH}" fi exit 1 fi echo "Download failed, retrying in ${RETRY_DELAY} seconds... (attempt $i/$MAX_RETRIES)" sleep $RETRY_DELAY fi done # 5. Verify and set permissions # ----------------------------------------------------------------------------- if [ ! -f "${TARGET_PATH}" ]; then echo "Error: Download succeeded but file not found at ${TARGET_PATH}" >&2 exit 1 fi # Verify it's a binary (basic check) if ! file "${TARGET_PATH}" | grep -q "executable\|ELF"; then echo "Warning: Downloaded file may not be a valid executable" >&2 fi chmod +x "${TARGET_PATH}" # Remove backup on successful installation [ -n "${BACKUP_PATH:-}" ] && [ -f "${BACKUP_PATH}" ] && rm -f "${BACKUP_PATH}" echo "dropshell installed successfully to $TARGET_PATH" echo " " # 6. Update PATH if needed # ----------------------------------------------------------------------------- if [[ ":$PATH:" != *":${TARGET_DIR}:"* ]]; then echo "Note: ${TARGET_DIR} is not in your PATH" # Check which shell config file to update SHELL_RC="" if [ -n "${BASH_VERSION:-}" ]; then SHELL_RC="${HOME}/.bashrc" elif [ -n "${ZSH_VERSION:-}" ]; then SHELL_RC="${HOME}/.zshrc" fi if [ -n "$SHELL_RC" ] && [ -f "$SHELL_RC" ]; then if ! grep -q "export PATH.*${TARGET_DIR}" "$SHELL_RC"; then echo "Consider adding to your $SHELL_RC:" echo " export PATH=\"\$PATH:${TARGET_DIR}\"" fi fi fi echo "Please:" echo "1. run '${TARGET_PATH} edit' to edit the configuration." echo "2. run '${TARGET_PATH} install' to install dropshell components on this computer." echo "3. run 'source ~/.bashrc' (or ~/.zshrc) to update PATH for the current shell."