#!/bin/bash
set -e
error_exit() {
echo "Error: $1" >&2
exit 1
}
get_platform_arch() {
os=$(uname -s)
arch=$(uname -m)
case "$os" in
Linux)
platform="linux"
;;
Darwin)
platform="mac"
;;
*)
error_exit "Unsupported operating system: $os"
;;
esac
case "$arch" in
x86_64)
architecture="x64"
;;
aarch64 | arm64)
architecture="aarch64"
;;
*)
error_exit "Unsupported architecture: $arch"
;;
esac
echo "${platform}-${architecture}"
}
PLATFORM_ARCH=""
DEST_DIR="."
usage() {
echo "Usage: $0 <version> [-p <platform-arch>] [-d <extract-dir>]"
echo "Example: $0 1.0.3.1 -p linux-x64 -d ./output"
exit 1
}
if [ "$#" -lt 1 ] || [[ "$1" == -* ]]; then
echo -n "Version: "
read -r VERSION
else
VERSION="$1"
shift
fi
while getopts ":p:d:" opt; do
case "${opt}" in
p )
PLATFORM_ARCH=$OPTARG
;;
d )
DEST_DIR=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" >&2
usage
;;
: )
echo "Invalid option: -$OPTARG requires an argument" >&2
usage
;;
esac
done
shift $((OPTIND -1))
if [ "$#" -gt 0 ]; then
echo "Error: Unexpected arguments found."
usage
fi
if [ -z "$PLATFORM_ARCH" ]; then
PLATFORM_ARCH=$(get_platform_arch)
fi
case "$PLATFORM_ARCH" in
linux-aarch64|linux-x64|mac-aarch64|mac-x64|ohos-aarch64|ohos-x64|windows-x64)
;;
*)
error_exit "Unsupported platform-architecture: $PLATFORM_ARCH"
;;
esac
REPO_URL="https://gitcode.com/Cangjie/cangjie_stdx"
FILENAME="cangjie-stdx-${PLATFORM_ARCH}-${VERSION}.zip"
DOWNLOAD_URL="${REPO_URL}/releases/download/v${VERSION}/${FILENAME}"
CACHE_DIR="$HOME/.cangjie_stdx/v${VERSION}"
CACHED_FILE="${CACHE_DIR}/${FILENAME}"
EXTRACTED_DIR_NAME="cangjie-stdx-${PLATFORM_ARCH}-${VERSION}"
if [ ! -d "$DEST_DIR" ]; then
error_exit "Destination directory does not exist: $DEST_DIR"
fi
DEST_DIR_ABS=$(cd "$DEST_DIR" && pwd)
mkdir -p "$CACHE_DIR"
if [ -f "$CACHED_FILE" ]; then
echo "Using cached file: $CACHED_FILE"
else
echo "Downloading from: $DOWNLOAD_URL"
if command -v curl &> /dev/null; then
curl -# -L -o "$CACHED_FILE" "$DOWNLOAD_URL"
elif command -v wget &> /dev/null; then
wget -q --show-progress -O "$CACHED_FILE" "$DOWNLOAD_URL"
else
error_exit "Please install 'curl' or 'wget' to download files."
fi
if [ $? -ne 0 ]; then
rm -f "$CACHED_FILE"
error_exit "Download failed."
fi
fi
if ! file --mime-type "$CACHED_FILE" | grep -q "application/zip"; then
rm -f "$CACHED_FILE"
error_exit "Downloaded file is not a zip archive. Please check if the version number is correct."
fi
echo "Extracting to: $DEST_DIR_ABS"
TMP_EXTRACT_DIR=$(mktemp -d)
unzip -q "$CACHED_FILE" -d "$TMP_EXTRACT_DIR" || error_exit "Unzip failed. The file might be corrupted or not a valid zip archive."
TOP_LEVEL_DIR=$(find "$TMP_EXTRACT_DIR" -mindepth 1 -maxdepth 1 -type d)
if [ -z "$TOP_LEVEL_DIR" ] || [ ! -d "$TOP_LEVEL_DIR" ]; then
if [ -d "$TMP_EXTRACT_DIR" ]; then
rm -rf "$TMP_EXTRACT_DIR"
fi
error_exit "Could not find a top-level directory in the zip file."
fi
FINAL_PATH="${DEST_DIR_ABS}/${EXTRACTED_DIR_NAME}"
if [ -d "$FINAL_PATH" ]; then
echo "Warning: Target directory already exists. Overwriting: ${FINAL_PATH}"
rm -rf "$FINAL_PATH"
fi
mv "$TOP_LEVEL_DIR" "$FINAL_PATH"
if [ -d "$TMP_EXTRACT_DIR" ]; then
rm -rf "$TMP_EXTRACT_DIR"
fi
if [[ "$(uname -s)" == "Darwin" && "$PLATFORM_ARCH" == mac-* ]]; then
echo "Removing quarantine attribute for macOS..."
xattr -d -r com.apple.quarantine "$FINAL_PATH" 2>/dev/null || true
fi
echo "Successfully extracted to: $FINAL_PATH"