#!/bin/sh ################################################################################ # chapose - Installation Script # # Supported Platforms and Installation Methods: # # 1. macOS (Darwin) # - ARM64 (Apple Silicon M1/M2/M3): downloads chapose-macos-arm64-.zip # - x86_64 (Intel): downloads chapose-macos-x64-.zip # - Install directory: $HOME/.local/bin # - Installation method: sh install.sh (this script) # # 2. Linux # - x86_64: downloads chapose-linux-x64-.zip # - ARM64 (aarch64): downloads chapose-linux-arm64-.zip # - ARM32 (armv7l): downloads chapose-linux-arm-.zip # - RISCV64: downloads chapose-linux-riscv64-.zip # - Install directory: $HOME/.local/bin # - Installation method: sh install.sh (this script) # # 3. Windows (compatible shells only) # - Git for Windows (MINGW64_NT) # - MSYS2 (MSYS_NT) # - Cygwin (CYGWIN) # - x86_64: downloads chapose-windows-x64-.zip # - Install directory: $HOME/AppData/Local/bin # - Installation method: sh install.sh (this script) # # Prerequisites: # - curl: for downloading the binary # - unzip: for extracting the archive # - find: for locating extracted executable # - sh-compatible shell (bash, zsh, dash, etc.) # # Installation supports version selection in three compatible ways: # 1. Pass as positional argument: # sh install.sh 1.0.0 # 2. Set as environment variable: # VERSION=1.0.0 sh install.sh # 3. Pass argument via remote pipe execution: # curl -fsSL https://raw.githubusercontent.com/huanguan1978/chacrypt/main/chapose/install.sh | sh -s -- 1.0.0 # # Backward compatibility: # - Legacy flag is supported and has highest priority: # sh install.sh --version 1.0.0 # # Troubleshooting - Manual Installation: # If automatic installation fails, download the binary manually: # 1. Visit: https://github.com/huanguan1978/chacrypt/releases # 2. Download the appropriate zip file for your platform # 3. Extract the zip file (executable is in build/ subdirectory) # 4. Copy executable to your preferred location (e.g., ~/.local/bin/chapose) # 5. Make sure the directory is in your PATH environment variable # 6. Verify installation by running: chapose --version # # Notes: # - Release tag format: ${TAG_PREFIX} # - Default TAG_PREFIX is "chapose-v" (for tags like "chapose-v1.0.0") # - If version starts with "v" or "chapose-v", it will be normalized # - Release page: https://github.com/huanguan1978/chacrypt/releases ################################################################################ set -e # Configuration APP_NAME="chapose" REPO="huanguan1978/chacrypt" TAG_PREFIX="chapose-v" DEFAULT_VERSION="1.0.0" RELEASE_PAGE="https://github.com/${REPO}/releases" CLI_VERSION="" POSITIONAL_VERSION="" usage() { echo "Usage: sh install.sh [version] [--version ]" echo "" echo "Version input compatibility (priority high to low):" echo " 1) --version " echo " 2) positional [version]" echo " 3) env var VERSION" echo " 4) built-in default (${DEFAULT_VERSION})" echo "" echo "Examples:" echo " sh install.sh --version 1.0.0" echo " sh install.sh 1.0.0" echo " VERSION=1.0.0 sh install.sh" echo " curl -fsSL https://raw.githubusercontent.com/huanguan1978/chacrypt/main/chapose/install.sh | sh -s -- 1.0.0" } while [ "$#" -gt 0 ]; do case "$1" in --version) shift if [ -z "$1" ]; then echo "Error: --version requires a value." usage exit 1 fi CLI_VERSION="$1" ;; -h|--help) usage exit 0 ;; *) if [ -z "$POSITIONAL_VERSION" ]; then POSITIONAL_VERSION="$1" else echo "Error: Unknown or duplicate argument: $1" usage exit 1 fi ;; esac shift done # Determine version using compatible priority order. if [ -n "$CLI_VERSION" ]; then VERSION="$CLI_VERSION" else VERSION="${POSITIONAL_VERSION:-$VERSION}" fi VERSION="${VERSION:-$DEFAULT_VERSION}" # Normalize accepted version forms. VERSION="${VERSION#${TAG_PREFIX}}" VERSION="${VERSION#v}" TAG="${TAG_PREFIX}${VERSION}" # Detect OS and architecture UNAME_OS=$(uname -s) ARCH=$(uname -m) # Convert OS name case "$UNAME_OS" in Darwin) OS_NAME="macos" ;; Linux) OS_NAME="linux" ;; MINGW64_NT* | MSYS_NT* | CYGWIN*) OS_NAME="windows" ;; *) echo "Error: Unsupported OS: $UNAME_OS"; exit 1 ;; esac # Set install directory based on OS if [ "$OS_NAME" = "windows" ]; then INSTALL_DIR="$HOME/AppData/Local/bin" else INSTALL_DIR="$HOME/.local/bin" fi # Convert architecture name case "$ARCH" in x86_64) ARCH_NAME="x64" ;; arm64) ARCH_NAME="arm64" ;; aarch64) ARCH_NAME="arm64" ;; armv7l) ARCH_NAME="arm" ;; riscv64) ARCH_NAME="riscv64" ;; *) echo "Error: Unsupported architecture: $ARCH"; exit 1 ;; esac # Build filename and download URL FILE_NAME="${APP_NAME}-${OS_NAME}-${ARCH_NAME}-${VERSION}.zip" URL="https://github.com/${REPO}/releases/download/${TAG}/${FILE_NAME}" if [ "$OS_NAME" = "windows" ]; then TARGET_BIN="$INSTALL_DIR/${APP_NAME}.exe" else TARGET_BIN="$INSTALL_DIR/${APP_NAME}" fi echo "========================================" echo "${APP_NAME} Installation Script v${VERSION}" echo "========================================" echo "Detected: OS=$OS_NAME, Architecture=$ARCH_NAME" echo "Install directory: $INSTALL_DIR" echo "" echo "Downloading: $URL" echo "" # Create install directory and download mkdir -p "$INSTALL_DIR" || { echo "Error: Failed to create directory $INSTALL_DIR"; exit 1; } TMP_DIR=$(mktemp -d) || { echo "Error: Failed to create temporary directory"; exit 1; } cd "$TMP_DIR" if ! curl -LO "$URL" 2>/dev/null; then echo "Error: Download failed. This may be due to:" echo " - Network connectivity issues" echo " - Incorrect platform/architecture detection" echo "" echo "Please download the binary manually from:" echo "$RELEASE_PAGE" echo "" echo "Then follow the manual installation steps in the script comments." rm -rf "$TMP_DIR" exit 1 fi # Extract and install if ! unzip -q -o "$FILE_NAME"; then echo "Error: Failed to extract the archive. Ensure unzip is installed." rm -rf "$TMP_DIR" exit 1 fi # Find the executable file (in build/ subdirectory) EXECUTABLE="" # Try exact filename based on OS (Windows has .exe extension) if [ "$OS_NAME" = "windows" ]; then if [ -f "build/${APP_NAME}-${VERSION}-${OS_NAME}-${ARCH_NAME}.exe" ]; then EXECUTABLE="build/${APP_NAME}-${VERSION}-${OS_NAME}-${ARCH_NAME}.exe" fi else if [ -f "build/${APP_NAME}-${VERSION}-${OS_NAME}-${ARCH_NAME}" ]; then EXECUTABLE="build/${APP_NAME}-${VERSION}-${OS_NAME}-${ARCH_NAME}" fi fi # Fallback: search for any executable matching "chapose*" if [ -z "$EXECUTABLE" ]; then EXECUTABLE=$(find build -name "${APP_NAME}*" -type f 2>/dev/null | head -1) fi if [ -z "$EXECUTABLE" ] || [ ! -f "$EXECUTABLE" ]; then echo "Error: Could not find '${APP_NAME}' executable in the archive." echo "The archive structure may have changed. Please download manually:" echo "$RELEASE_PAGE" echo "" echo "Manual Installation Steps:" echo "1. Download the zip file from: $RELEASE_PAGE" echo "2. Extract the archive" echo "3. Find the '${APP_NAME}' executable (in build/ subdirectory)" echo "4. Copy it to: $TARGET_BIN" echo "5. Make it executable: chmod +x $TARGET_BIN" rm -rf "$TMP_DIR" exit 1 fi if ! mv "$EXECUTABLE" "$TARGET_BIN"; then echo "Error: Failed to move ${APP_NAME} to $INSTALL_DIR" echo "You may need elevated permissions. Try: sudo mv $EXECUTABLE $TARGET_BIN" rm -rf "$TMP_DIR" exit 1 fi chmod +x "$TARGET_BIN" 2>/dev/null || true # Cleanup rm -rf "$TMP_DIR" # Verify installation if ! [ -x "$TARGET_BIN" ]; then echo "Error: Installation verification failed. ${APP_NAME} executable not found or not executable." exit 1 fi # Prompt user to update PATH if needed echo "========================================" echo "✓ Installation completed!" echo "========================================" echo "${APP_NAME} has been installed to: $TARGET_BIN" echo "" if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then echo "⚠ Action required: Add $INSTALL_DIR to your PATH" echo "" echo "Run:" echo " export PATH=\"\$PATH:$INSTALL_DIR\"" echo "" if [ "$OS_NAME" = "windows" ]; then echo "To persist this, add the above line to your .bashrc or .profile" else echo "To persist this, add the above line to ~/.bashrc, ~/.zshrc, or ~/.profile" fi echo "" else echo "✓ $INSTALL_DIR is already in your PATH" echo "" fi echo "Verify installation:" echo " ${APP_NAME} --version" echo "" echo "For help:" echo " ${APP_NAME} --help"