test: verify update script

This commit is contained in:
Cametendo
2026-04-23 18:32:36 +02:00
parent 9157b3514a
commit d8b3795b3e
3 changed files with 186 additions and 0 deletions

86
build.sh Executable file
View File

@@ -0,0 +1,86 @@
#!/usr/bin/env bash
# --- Build script for cflash ---
# Compiles sources and bundles jline via Maven into a single fat JAR.
# If Maven is missing, prompts the user to install it using the detected package manager.
set -e
JAR_FILE="cflash.jar"
# --- Detect package manager ---
detect_package_manager() {
if command -v pacman >/dev/null 2>&1; then
echo "pacman"
elif command -v apt >/dev/null 2>&1; then
echo "apt"
elif command -v dnf >/dev/null 2>&1; then
echo "dnf"
elif command -v yum >/dev/null 2>&1; then
echo "yum"
elif command -v zypper >/dev/null 2>&1; then
echo "zypper"
elif command -v brew >/dev/null 2>&1; then
echo "brew"
elif command -v apk >/dev/null 2>&1; then
echo "apk"
elif command -v emerge >/dev/null 2>&1; then
echo "emerge"
else
echo ""
fi
}
# --- Generate Maven install command ---
maven_install_command() {
local pm="$1"
case "$pm" in
pacman) echo "pacman -Sy --noconfirm maven" ;;
apt) echo "apt update && apt install -y maven" ;;
dnf) echo "dnf install -y maven" ;;
yum) echo "yum install -y maven" ;;
zypper) echo "zypper install -y maven" ;;
brew) echo "brew install maven" ;;
apk) echo "apk add maven" ;;
emerge) echo "emerge dev-java/maven-bin" ;;
*) echo "" ;;
esac
}
# --- Check for mvn ---
if ! command -v mvn >/dev/null 2>&1; then
echo "Maven (mvn) not found."
PM=$(detect_package_manager)
if [[ -z "$PM" ]]; then
echo "Please install Maven manually and rerun this script."
exit 1
fi
CMD=$(maven_install_command "$PM")
if [[ $EUID -ne 0 && "$PM" != "brew" ]]; then
echo "Please run this script as root to install Maven, or install it manually."
exit 1
fi
read -rp "Do you want to run the following command to install Maven? [$CMD] (y/n): " ANSWER
case "$ANSWER" in
y|Y)
echo "Installing Maven..."
eval "$CMD"
echo "Maven installed successfully."
;;
*)
echo "Maven installation cancelled. Please install manually and rerun."
exit 1
;;
esac
fi
# --- Build process ---
echo "Building cflash with Maven..."
mvn package -q
echo "Copying fat JAR to ${JAR_FILE}..."
cp target/cflash-*-jar-with-dependencies.jar "$JAR_FILE"
echo "Build completed successfully: ${JAR_FILE}"

84
cflash.sh Executable file
View File

@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# cflash launcher with self-update via git + rebuild (requires sudo for update)
CFLASH_DIR="/usr/local/lib/cflash"
CFLASH_JAR="$CFLASH_DIR/cflash.jar"
GIT_REPO="https://gitea.cametendo.org/cametendo/cflash.git"
update_cflash() {
# Check for sudo/root
if [[ $EUID -ne 0 ]]; then
echo "cflash --update requires root privileges. Please run with sudo."
exit 1
fi
echo "Updating cflash from repository..."
TMP_DIR=$(mktemp -d)
echo "Cloning repository into $TMP_DIR..."
if ! git clone --depth 1 "$GIT_REPO" "$TMP_DIR"; then
echo "Failed to clone repository."
rm -rf "$TMP_DIR"
exit 1
fi
pushd "$TMP_DIR" >/dev/null
# Logic to handle if the repo contents are nested inside a 'cflash' folder
if [[ ! -f "./build.sh" && -d "cflash" ]]; then
echo "Detected nested directory, moving into cflash/..."
cd cflash
fi
# Ensure build.sh is executable (git usually preserves this, but just in case)
if [[ -f "./build.sh" ]]; then
chmod +x ./build.sh
echo "Building cflash..."
if ! ./build.sh; then
echo "Build failed."
popd >/dev/null
rm -rf "$TMP_DIR"
exit 1
fi
else
echo "Error: build.sh not found in the repository root or cflash/ directory."
popd >/dev/null
rm -rf "$TMP_DIR"
exit 1
fi
echo "Installing new version..."
if [[ -f "./install.sh" ]]; then
chmod +x ./install.sh
if ! ./install.sh; then
echo "Install failed."
popd >/dev/null
rm -rf "$TMP_DIR"
exit 1
fi
else
echo "Error: install.sh not found."
popd >/dev/null
rm -rf "$TMP_DIR"
exit 1
fi
popd >/dev/null
rm -rf "$TMP_DIR"
echo "Update completed successfully!"
exit 0
}
# Handle --update argument
if [[ "$1" == "--update" ]]; then
update_cflash
fi
# Run cflash normally
if [[ -f "$CFLASH_JAR" ]]; then
java --enable-native-access=ALL-UNNAMED -jar "$CFLASH_JAR" "$@"
else
echo "Error: $CFLASH_JAR not found. Please run 'sudo cflash --update' to install."
exit 1
fi

16
install.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Install script for system-wide usage
# Ensure running as root
if [[ $EUID -ne 0 ]]; then
echo "Please run as root to install globally."
exit 1
fi
# Create installation directories
mkdir -p /usr/local/lib/cflash
cp cflash.jar /usr/local/lib/cflash/
cp cflash.sh /usr/local/bin/cflash
chmod +x /usr/local/bin/cflash
echo "cflash installed successfully! You can now run 'cflash' from anywhere."