File libtorrent-rasterbar-custom-update_check.sh of Package libtorrent-rasterbar-custom

#!/bin/bash

# ==============================================================================
# LibTorrent Rasterbar (Custom) Auto-Updater for OBS
# ==============================================================================
#
# 1. Interrogates upstream Git repository for the latest v2.0.x stable tag.
# 2. Updates the 'Version' and 'Release' fields in the .spec file.
# 3. Triggers 'osc service manualrun -v download_files' to procure sources.
# 4. Verifies the downloaded source tarball and CHANGELOG integrity.
# 5. Executes cleanup of obsolete source archives.
#
# ==============================================================================

# --- Configuration Definitions ---
PACKAGE_NAME="libtorrent-rasterbar-custom"
SOURCE_NAME="libtorrent-rasterbar"
SPEC_FILE="${PACKAGE_NAME}.spec"

# Target Upstream Repository
GIT_REPO="https://github.com/arvidn/libtorrent.git"

# Terminal Output Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# ------------------------------------------------------------------
# Step 1: Resolve the Latest Version via Git Tags
# ------------------------------------------------------------------
echo -e "${BLUE}๐Ÿ” Interrogating upstream repository for the latest v2.0.x release...${NC}"

# We utilize 'git ls-remote' to securely fetch tags without hitting GitHub API rate limits.
# The grep strictly limits extraction to the 'v2.0.x' series to prevent grabbing v1.2.x updates.
NEW_VERSION=$(git ls-remote --tags "$GIT_REPO" \
    | awk -F/ '{print $3}' \
    | grep -E '^v2\.0\.[0-9]+$' \
    | sed 's/^v//' \
    | sort -V \
    | tail -n1)

# Validation: Verify the response conforms to expected standard versioning.
if [[ ! "$NEW_VERSION" =~ ^2\.0\.[0-9]+$ ]]; then
    echo -e "${RED}โŒ Error: Failed to parse a valid version string from the upstream repository.${NC}"
    exit 1
fi

echo -e "${GREEN}โœ… Latest LibTorrent Rasterbar version detected: $NEW_VERSION${NC}"

# ------------------------------------------------------------------
# Step 2: Validate Local Spec File
# ------------------------------------------------------------------
if [ ! -f "$SPEC_FILE" ]; then
    echo -e "${RED}โŒ Error: Spec file '$SPEC_FILE' not found in the current directory.${NC}"
    exit 1
fi

# Extract the current version directly from the Spec file's 'Version:' directive.
CURRENT_VERSION=$(grep "^Version:" "$SPEC_FILE" | awk '{print $2}' | tr -d '[:space:]')
echo -e "${BLUE}โ„น๏ธ  Current Spec version: $CURRENT_VERSION${NC}"

if [ "$NEW_VERSION" == "$CURRENT_VERSION" ]; then
    echo -e "${GREEN}๐ŸŽ‰ The system is already up-to-date ($NEW_VERSION). No further action is required.${NC}"
    exit 0
fi

# ------------------------------------------------------------------
# Step 3: Update Spec Configuration
# ------------------------------------------------------------------
echo -e "${BLUE}๐Ÿš€ Update required. Initiating upgrade process from $CURRENT_VERSION to $NEW_VERSION...${NC}"

# 1. Update the 'Version' directive to the newly acquired stable version.
sed -i "s/^Version:[[:space:]]*.*/Version:        $NEW_VERSION/" "$SPEC_FILE"

# 2. Reset the 'Release' iteration to 0 to indicate a fresh build.
sed -i "s/^Release:[[:space:]]*.*/Release:        0/" "$SPEC_FILE"

echo -e "${GREEN}โœ… Spec file successfully updated.${NC}"

# ------------------------------------------------------------------
# Step 4: Execute OSC Service to Procure Source Files
# ------------------------------------------------------------------
echo -e "${BLUE}โฌ‡๏ธ  Executing 'osc service manualrun -v download_files'...${NC}"

osc service manualrun -v download_files

if [ $? -ne 0 ]; then
    echo -e "${RED}โŒ OSC Service execution failed. Please verify your internet connection and the validity of the Source URLs.${NC}"
    exit 1
fi

# ------------------------------------------------------------------
# Step 5: Post-Download Verification and Cleanup
# ------------------------------------------------------------------

# Defined expected filenames based on the .spec Source configurations.
EXPECTED_TAR="${SOURCE_NAME}-${NEW_VERSION}.tar.gz"
EXPECTED_CHANGELOG="CHANGELOG"

MISSING_FILES=0

# Validate Archive Integrity
if [ ! -f "$EXPECTED_TAR" ]; then
    echo -e "${RED}โŒ Error: Expected source archive '$EXPECTED_TAR' is missing.${NC}"
    MISSING_FILES=1
else
    # Simple diagnostic check using tar
    if ! tar -tzf "$EXPECTED_TAR" > /dev/null 2>&1; then
         echo -e "${RED}โŒ Error: The downloaded archive '$EXPECTED_TAR' appears to be corrupted or invalid.${NC}"
         MISSING_FILES=1
    else
         echo -e "${GREEN}โœ… Source archive '$EXPECTED_TAR' verified successfully.${NC}"
    fi
fi

# Validate ChangeLog
if [ ! -f "$EXPECTED_CHANGELOG" ]; then
    echo -e "${RED}โŒ Error: Expected file '$EXPECTED_CHANGELOG' is missing.${NC}"
    MISSING_FILES=1
else
    echo -e "${GREEN}โœ… File '$EXPECTED_CHANGELOG' verified successfully.${NC}"
fi

# Halt if critical files are missing or corrupted
if [ $MISSING_FILES -eq 1 ]; then
    exit 1
fi

# --- Cleanup Architecture ---
echo -e "${BLUE}๐Ÿงน Executing cleanup of obsolete source archives...${NC}"
cleanup_count=0

# Target old tar.gz files
for file in ${SOURCE_NAME}-*.tar.gz; do
    if [ "$file" != "$EXPECTED_TAR" ] && [ -f "$file" ]; then
        echo -e "${YELLOW}   ๐Ÿ—‘๏ธ  Removing obsolete archive: $file${NC}"
        rm -f "$file"
        cleanup_count=$((cleanup_count + 1))
    fi
done

if [ $cleanup_count -eq 0 ]; then
    echo -e "${BLUE}โ„น๏ธ  No obsolete source files required cleanup.${NC}"
else
    echo -e "${GREEN}โœ… Cleanup complete.${NC}"
fi

# ------------------------------------------------------------------
# Step 6: Final OBS Submission Instructions
# ------------------------------------------------------------------
echo ""
echo -e "${GREEN}โœจ Process complete! The build environment is fully synchronized.${NC}"
echo -e "${CYAN}To finalize the application and submit into the OBS repositories, please execute:${NC}"
echo ""
echo -e "${YELLOW}osc addremove && osc vc && osc commit -v${NC}"
echo ""
openSUSE Build Service is sponsored by