File discord-update_check.sh of Package discord
#!/bin/bash
# ==============================================================================
# Discord Auto-Updater for OBS
# ==============================================================================
#
# 1. Checks Discord API for the latest version via redirect URL.
# 2. Updates the .spec file version.
# 3. Triggers 'osc service manualrun download_files' to fetch sources.
# 4. Verifies the downloaded source tarball.
# 5. Cleans up old source tarballs.
#
# ==============================================================================
# --- Configuration ---
PACKAGE_NAME="discord"
SPEC_FILE="${PACKAGE_NAME}.spec"
API_URL="https://discord.com/api/download?platform=linux&format=tar.gz"
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# ------------------------------------------------------------------
# Step 1: Find the latest version from Discord API
# ------------------------------------------------------------------
echo -e "${BLUE}đ Checking Discord API for the latest version...${NC}"
# Get the Redirect URL (HEAD request only)
# We use curl to fetch the headers (-I) silently (-s) and grab the 'Location' header.
REDIRECT_URL=$(curl -s -I "$API_URL" | grep -i "location:" | awk '{print $2}' | tr -d '\r')
if [ -z "$REDIRECT_URL" ]; then
echo -e "${RED}â Error: Could not retrieve download URL from Discord API.${NC}"
exit 1
fi
# Extract Version Number
# URL format: https://dl.discordapp.net/apps/linux/0.0.115/discord-0.0.115.tar.gz
# We use sed to capture the numbers between 'discord-' and '.tar.gz'
NEW_VERSION=$(echo "$REDIRECT_URL" | sed -n 's/.*discord-\([0-9.]*\)\.tar\.gz/\1/p')
if [ -z "$NEW_VERSION" ]; then
echo -e "${RED}â Error: Could not parse version number from URL: $REDIRECT_URL${NC}"
exit 1
fi
echo -e "${GREEN}â
Latest version available: $NEW_VERSION${NC}"
# ------------------------------------------------------------------
# Step 2: Check Local Spec File
# ------------------------------------------------------------------
if [ ! -f "$SPEC_FILE" ]; then
echo -e "${RED}â ī¸ Warning: $SPEC_FILE not found. (Expected file: $SPEC_FILE)${NC}"
CURRENT_VERSION="none"
else
# Extract the current version from the spec file (Assumes "Version: 0.0.X")
CURRENT_VERSION=$(grep "^Version:" "$SPEC_FILE" | awk '{print $2}' | tr -d '[:space:]')
echo -e "${BLUE}âšī¸ Current OBS version: $CURRENT_VERSION${NC}"
fi
if [ "$NEW_VERSION" == "$CURRENT_VERSION" ]; then
echo -e "${GREEN}đ System is up-to-date ($NEW_VERSION). No action needed.${NC}"
exit 0
fi
# ------------------------------------------------------------------
# Step 3: Update Spec and Fetch Sources
# ------------------------------------------------------------------
echo -e "${BLUE}đ Update required! Upgrading from $CURRENT_VERSION to $NEW_VERSION...${NC}"
if [ -f "$SPEC_FILE" ]; then
echo -e "${BLUE}đ Updating $SPEC_FILE...${NC}"
# 1. Update Version in spec
sed -i "s/^Version:[[:space:]]*.*/Version: $NEW_VERSION/" "$SPEC_FILE"
# 2. Reset Release to 0
sed -i "s/^Release:[[:space:]]*.*/Release: 0/" "$SPEC_FILE"
echo -e "${GREEN}â
Spec file updated.${NC}"
fi
# ------------------------------------------------------------------
# Step 4: Run OSC Service to Download Files
# ------------------------------------------------------------------
echo -e "${BLUE}âŦī¸ Running 'osc service manualrun download_files -v'...${NC}"
# This command parses the updated spec file and downloads the new Source tarball
osc service manualrun download_files -v
if [ $? -ne 0 ]; then
echo -e "${RED}â OSC Service run failed! Check your internet connection or URL validity.${NC}"
exit 1
fi
# ------------------------------------------------------------------
# Step 5: Verify and Cleanup
# ------------------------------------------------------------------
# Discord downloads usually look like: discord-0.0.115.tar.gz
FILENAME="${PACKAGE_NAME}-${NEW_VERSION}.tar.gz"
if [ ! -f "$FILENAME" ]; then
# Fallback check: sometimes it might just be discord.tar.gz depending on Spec configuration
if [ -f "discord.tar.gz" ]; then
FILENAME="discord.tar.gz"
else
echo -e "${RED}â Error: Expected source file $FILENAME was not found after download_files run.${NC}"
exit 1
fi
fi
# Verify archive integrity
if ! tar -tf "$FILENAME" > /dev/null 2>&1; then
echo -e "${RED}â Error: The downloaded file $FILENAME appears corrupted or invalid.${NC}"
exit 1
fi
echo -e "${GREEN}â
Source file $FILENAME verified.${NC}"
# Cleanup old source files (discord-*.tar.gz)
count=0
for f in ${PACKAGE_NAME}-*.tar.gz; do
if [ "$f" != "$FILENAME" ] && [ -f "$f" ]; then
echo -e "${BLUE}đī¸ Removing old source: $f${NC}"
rm "$f"
count=$((count+1))
fi
done
if [ $count -eq 0 ]; then
echo -e "${BLUE}âšī¸ No old source files found to clean up.${NC}"
fi
echo -e "${GREEN}⨠Process complete! You can now run: osc addremove && osc ci${NC}"