#!/bin/sh # update-cpd-cli.sh — Download and install the latest (or specified) cpd-cli release. # Compatible with sh, bash, and zsh. # # Usage: # sudo ./update-cpd-cli.sh # auto-detect latest release # sudo ./update-cpd-cli.sh 14.3.1 # install a specific version # # Assumes: curl, tar, and write access to INSTALL_DIR (run with sudo). set -eu # ---------- configuration ---------- INSTALL_DIR="/usr/local/bin" WORK_DIR="${TMPDIR:-/tmp}/cpd-cli-update-$$" GITHUB_REPO="IBM/cpd-cli" SYMLINKS="cpd-cli LICENSES plugins" # ------------------------------------ cleanup() { rm -rf "$WORK_DIR"; } trap cleanup EXIT die() { printf "ERROR: %s\n" "$1" >&2; exit 1; } # ---------- resolve version ---------- if [ $# -ge 1 ]; then VERSION="$1" else printf "Detecting latest cpd-cli release... " VERSION=$(curl -fsSL -o /dev/null -w '%{url_effective}' \ "https://github.com/${GITHUB_REPO}/releases/latest" \ | grep -oE '[0-9]+\.[0-9]+\.[0-9]+$') \ || die "Could not determine latest version. Specify one manually: $0 " printf "%s\n" "$VERSION" fi TARBALL="cpd-cli-linux-EE-${VERSION}.tgz" URL="https://github.com/${GITHUB_REPO}/releases/download/v${VERSION}/${TARBALL}" # ---------- download ---------- mkdir -p "$WORK_DIR" printf "Downloading %s ...\n" "$URL" curl -fSL -o "${WORK_DIR}/${TARBALL}" "$URL" || die "Download failed. Check the version number." # ---------- extract ---------- printf "Extracting... " tar xzf "${WORK_DIR}/${TARBALL}" -C "$WORK_DIR" EXTRACTED=$(find "$WORK_DIR" -maxdepth 1 -type d -name "cpd-cli-linux-EE-*" | head -1) [ -n "$EXTRACTED" ] || die "Could not find extracted directory in ${WORK_DIR}." DIR_NAME=$(basename "$EXTRACTED") printf "%s\n" "$DIR_NAME" # ---------- check for no-op ---------- if [ -d "${INSTALL_DIR}/${DIR_NAME}" ]; then printf "Directory %s/%s already exists. " "$INSTALL_DIR" "$DIR_NAME" printf "Re-linking anyway.\n" else printf "Installing to %s/%s ...\n" "$INSTALL_DIR" "$DIR_NAME" mv "$EXTRACTED" "$INSTALL_DIR/" fi # ---------- update symlinks ---------- printf "Updating symlinks in %s ...\n" "$INSTALL_DIR" for link in $SYMLINKS; do target="${INSTALL_DIR}/${link}" if [ -L "$target" ]; then rm "$target" elif [ -e "$target" ]; then die "${target} exists and is not a symlink — refusing to overwrite." fi ln -s "${DIR_NAME}/${link}" "$target" printf " %s -> %s/%s\n" "$link" "$DIR_NAME" "$link" done # ---------- verify ---------- printf "\nInstalled:\n" cpd-cli version printf "\nDone.\n"