#!/usr/bin/env sh set -eu OWNER="beautifulrem" REPO="tdl-next" BIN_NAME="tdln" CHECKSUM_FILE="tdln_checksums.txt" DEFAULT_PROXY="https://mirror.ghproxy.com/" DOWNLOAD_BASE_URL="${TDLN_DOWNLOAD_BASE_URL:-https://github.com}" API_BASE_URL="${TDLN_API_BASE_URL:-https://api.github.com}" PROXY="${TDLN_GITHUB_PROXY:-}" VERSION="" CUSTOM_DIR="${TDLN_INSTALL_DIR:-}" if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then C_GREEN='\033[32m' C_RED='\033[31m' C_BLUE='\033[34m' C_YELLOW='\033[33m' C_RESET='\033[0m' else C_GREEN='' C_RED='' C_BLUE='' C_YELLOW='' C_RESET='' fi echo_green() { printf '%b%s%b\n' "$C_GREEN" "$1" "$C_RESET" } echo_red() { printf '%b%s%b\n' "$C_RED" "$1" "$C_RESET" >&2 } echo_blue() { printf '%b%s%b\n' "$C_BLUE" "$1" "$C_RESET" } echo_yellow() { printf '%b%s%b\n' "$C_YELLOW" "$1" "$C_RESET" } usage() { cat <<'EOF' Install tdln (TDL Next) from verified GitHub Releases (beautifulrem/tdl-next). Usage: install.sh [--version ] [--dir ] [--proxy [URL]] [--help] Options: --version Install a specific release tag (default: latest stable release, or the newest pre-release if no stable release exists) --dir Target installation directory (default: /usr/local/bin if writable/root, otherwise $XDG_BIN_HOME or $HOME/.local/bin) --proxy [URL] Prefix GitHub URLs with a proxy URL (default: https://mirror.ghproxy.com/) -h, --help Show this help message Environment: TDLN_INSTALL_DIR Default target installation directory TDLN_GITHUB_PROXY Default GitHub proxy prefix URL TDLN_DOWNLOAD_BASE_URL Override download base URL (default: https://github.com) TDLN_API_BASE_URL Override GitHub API base URL (default: https://api.github.com) EOF } while [ $# -gt 0 ]; do case "$1" in --proxy) if [ $# -ge 2 ]; then case "$2" in --*|-h) PROXY="$DEFAULT_PROXY" shift ;; *) PROXY="$2" shift 2 ;; esac else PROXY="$DEFAULT_PROXY" shift fi ;; --version) if [ $# -lt 2 ] || [ -z "$2" ]; then echo_red "Missing value for --version" exit 1 fi VERSION="$2" shift 2 ;; --dir) if [ $# -lt 2 ] || [ -z "$2" ]; then echo_red "Missing value for --dir" exit 1 fi CUSTOM_DIR="$2" shift 2 ;; -h|--help) usage exit 0 ;; *) echo_red "Unknown flag: $1" usage >&2 exit 1 ;; esac done if [ -n "$PROXY" ]; then case "$PROXY" in */) ;; *) PROXY="${PROXY}/" ;; esac echo_blue "Using GitHub proxy: $PROXY" fi # Detect available downloader: prefer curl, fall back to wget if command -v curl >/dev/null 2>&1; then DOWNLOADER="curl" elif command -v wget >/dev/null 2>&1; then DOWNLOADER="wget" else echo_red "Neither 'curl' nor 'wget' is installed." echo_red "Please install one of them and run this script again." exit 1 fi echo_blue "Using downloader: $DOWNLOADER" # Detect SHA-256 verification tool (fail closed if none present) if command -v sha256sum >/dev/null 2>&1; then SHA256_TOOL="sha256sum" elif command -v shasum >/dev/null 2>&1; then SHA256_TOOL="shasum" elif command -v openssl >/dev/null 2>&1; then SHA256_TOOL="openssl" else echo_red "No SHA-256 verification tool found ('sha256sum', 'shasum', or 'openssl')." exit 1 fi # Writes the response body of $1 to file $2 and prints the final HTTP status code # (empty or 000 when no response was received). fetch_with_status() { case "$DOWNLOADER" in curl) curl -sSL -o "$2" -w '%{http_code}' "$1" 2>/dev/null || true ;; wget) wget -S -O "$2" "$1" 2>&1 | awk '$1 ~ /^HTTP\// { code = $2 } END { print code }' || true ;; esac } download_file() { case "$DOWNLOADER" in curl) curl -fsSL -o "$2" "$1" ;; wget) wget -q -O "$2" "$1" ;; esac } compute_sha256() { case "$SHA256_TOOL" in sha256sum) sha256sum "$1" | awk '{print tolower($1)}' ;; shasum) shasum -a 256 "$1" | awk '{print tolower($1)}' ;; openssl) openssl dgst -sha256 "$1" | awk '{print tolower($NF)}' ;; esac } # Resolve target directory without forcing root if [ -n "$CUSTOM_DIR" ]; then LOCATION="$CUSTOM_DIR" elif [ "$(id -u)" -eq 0 ] || { [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; }; then LOCATION="/usr/local/bin" elif [ -n "${XDG_BIN_HOME:-}" ]; then LOCATION="$XDG_BIN_HOME" elif [ -n "${HOME:-}" ]; then LOCATION="${HOME}/.local/bin" else echo_red "Neither HOME nor XDG_BIN_HOME is set, and /usr/local/bin is not writable." echo_red "Specify a user-writable target directory with '--dir ' or set TDLN_INSTALL_DIR." exit 1 fi if ! mkdir -p "$LOCATION" 2>/dev/null || [ ! -w "$LOCATION" ]; then echo_red "Target directory '$LOCATION' is not writable." echo_red "Specify a user-writable directory with '--dir ' or re-run with elevated privileges." exit 1 fi # Map OS matching crates/tdl-engine/src/services/update/ RAW_OS=$(uname -s) case "$RAW_OS" in Linux) OS="Linux" ;; Darwin) OS="MacOS" ;; *) echo_red "Unsupported OS: $RAW_OS" exit 1 ;; esac # Map architecture matching crates/tdl-engine/src/services/update/ RAW_ARCH=$(uname -m) case "$RAW_ARCH" in x86_64|amd64) ARCH="64bit" ;; arm64|aarch64*) ARCH="arm64" ;; *) echo_red "Unsupported architecture: $RAW_ARCH" exit 1 ;; esac DOWNLOAD_BASE_URL="${DOWNLOAD_BASE_URL%/}" API_BASE_URL="${API_BASE_URL%/}" extract_tag_name() { sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 } if [ -z "$VERSION" ]; then RELEASE_JSON=$(mktemp) LATEST_URL="${API_BASE_URL}/repos/${OWNER}/${REPO}/releases/latest" STATUS=$(fetch_with_status "$LATEST_URL" "$RELEASE_JSON") case "$STATUS" in 200) VERSION=$(extract_tag_name < "$RELEASE_JSON") ;; 404) # /releases/latest skips pre-releases and 404s until a stable release exists. # Fall back to the newest published release; anonymous API calls never list drafts. echo_yellow "No stable release found; falling back to the newest pre-release." LIST_URL="${API_BASE_URL}/repos/${OWNER}/${REPO}/releases?per_page=1" STATUS=$(fetch_with_status "$LIST_URL" "$RELEASE_JSON") if [ "$STATUS" = "200" ]; then VERSION=$(extract_tag_name < "$RELEASE_JSON") else echo_red "GitHub API request to ${LIST_URL} failed (HTTP ${STATUS:-no response})." fi ;; *) echo_red "GitHub API request to ${LATEST_URL} failed (HTTP ${STATUS:-no response})." ;; esac rm -f "$RELEASE_JSON" fi if [ -z "$VERSION" ]; then echo_red "Failed to resolve target release version from ${OWNER}/${REPO}." exit 1 fi echo_blue "Target version: $VERSION" ARCHIVE_NAME="${BIN_NAME}_${OS}_${ARCH}.tar.gz" ARCHIVE_URL="${PROXY}${DOWNLOAD_BASE_URL}/${OWNER}/${REPO}/releases/download/${VERSION}/${ARCHIVE_NAME}" CHECKSUM_URL="${PROXY}${DOWNLOAD_BASE_URL}/${OWNER}/${REPO}/releases/download/${VERSION}/${CHECKSUM_FILE}" TMP_DIR=$(mktemp -d) cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT trap 'cleanup; exit 130' INT trap 'cleanup; exit 143' TERM CHECKSUM_PATH="${TMP_DIR}/${CHECKSUM_FILE}" ARCHIVE_PATH="${TMP_DIR}/${ARCHIVE_NAME}" EXTRACT_DIR="${TMP_DIR}/extract" echo_blue "Downloading ${CHECKSUM_FILE} from ${CHECKSUM_URL}" if ! download_file "$CHECKSUM_URL" "$CHECKSUM_PATH" || [ ! -s "$CHECKSUM_PATH" ]; then echo_red "Failed to download ${CHECKSUM_FILE} from ${CHECKSUM_URL}" exit 1 fi EXPECTED_SHA256=$(awk -v target="$ARCHIVE_NAME" ' { sub(/\r$/, "", $0) } /^[[:space:]]*#/ || /^[[:space:]]*$/ { next } { digest = tolower($1) fname = $2 sub(/\r$/, "", fname) sub(/^\*/, "", fname) sub(/^\.\//, "", fname) if (fname == target && digest ~ /^[0-9a-f]{64}$/) { print digest exit } } ' "$CHECKSUM_PATH") if [ -z "$EXPECTED_SHA256" ]; then echo_red "Checksum entry for '${ARCHIVE_NAME}' missing or invalid in ${CHECKSUM_FILE}." exit 1 fi echo_blue "Downloading ${ARCHIVE_NAME} from ${ARCHIVE_URL}" if ! download_file "$ARCHIVE_URL" "$ARCHIVE_PATH" || [ ! -s "$ARCHIVE_PATH" ]; then echo_red "Failed to download ${ARCHIVE_NAME} from ${ARCHIVE_URL}" exit 1 fi ACTUAL_SHA256=$(compute_sha256 "$ARCHIVE_PATH") if [ -z "$ACTUAL_SHA256" ] || [ "$ACTUAL_SHA256" != "$EXPECTED_SHA256" ]; then echo_red "SHA-256 verification failed for ${ARCHIVE_NAME}!" echo_red " expected: ${EXPECTED_SHA256}" echo_red " actual: ${ACTUAL_SHA256:-}" exit 1 fi echo_green "SHA-256 verified (${ACTUAL_SHA256})" mkdir -p "$EXTRACT_DIR" tar -xzf "$ARCHIVE_PATH" -C "$EXTRACT_DIR" EXTRACTED_BIN="${EXTRACT_DIR}/${BIN_NAME}" if [ ! -f "$EXTRACTED_BIN" ] || [ -L "$EXTRACTED_BIN" ]; then echo_red "Expected binary '${BIN_NAME}' not found as regular file at archive root." exit 1 fi STAGE_DEST="${LOCATION}/.${BIN_NAME}.install.$$" cp "$EXTRACTED_BIN" "$STAGE_DEST" chmod 0755 "$STAGE_DEST" mv -f "$STAGE_DEST" "${LOCATION}/${BIN_NAME}" echo_green "${BIN_NAME} installed successfully! Location: ${LOCATION}/${BIN_NAME}" case ":${PATH}:" in *":${LOCATION}:"*) ;; *) echo_yellow "Note: '${LOCATION}' is not in your PATH. Add it with:" echo_yellow " export PATH=\"${LOCATION}:\$PATH\"" ;; esac echo_green "Run '${BIN_NAME} --help' to get started" echo_green "Documentation: https://github.com/beautifulrem/tdl-next"