#!/bin/bash
#Service to auto update gitea installation
#Make sure curl and jq packages are installed

# Define PATH so cron can find commands
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

GITEA_HOME=/opt/gitea
DATE=$(date +%F-%H%M)
LOG_PATH="$GITEA_HOME/log"
LOG_FILE="$LOG_PATH/gitea-$DATE.log"
OLD_VERSION_PATH="$GITEA_HOME/old"

# Ensure directories exist
if [ ! -d "$LOG_PATH" ]; then
  mkdir -p "$LOG_PATH"
fi

if [ ! -d "$OLD_VERSION_PATH" ]; then
  mkdir -p "$OLD_VERSION_PATH"
fi

# Redirect all standard output (1) and standard error (2) to the log file
exec >> "$LOG_FILE" 2>&1

echo "GITEA UPDATER STARTED AT $DATE"

# Get Current Gitea Information
previousversion=$(gitea -v | awk '{print $3}')

# Get Version information from Git (Using -sS to hide progress but log errors)
newversion=$(curl -sS https://dl.gitea.com/gitea/version.json | jq -r .latest.version)

# Prevent silent failures if version checks fail
if [ -z "$previousversion" ] || [ -z "$newversion" ]; then
    echo "Error checking versions: Current ['$previousversion'], New ['$newversion']"
    exit 1
fi

if [ "$newversion" != "$previousversion" ]; then

    # Go to $GITEA_HOME
    cd "$GITEA_HOME" || exit 1

    # Make Old Version Unexecutable
    chmod -x gitea

    # Move Old Executable
    mv gitea "$OLD_VERSION_PATH/gitea_old_$previousversion"

    # Get System Information
    sys=$(uname -s)
    system=$(echo "$sys" | tr '[:upper:]' '[:lower:]')
    archictecture=$(dpkg --print-architecture)

    # Form URL Based on system information
    url="https://dl.gitea.com/gitea/$newversion/gitea-$newversion-$system-$archictecture"

    # Download update
    curl -sS "$url" -o gitea

    # Make new version executable
    chmod +x gitea

    # Restart Gitea service 
    sudo systemctl restart gitea.service
    echo "Update completed from $previousversion to $newversion"
else
    echo "No update available"
fi#!/bin/bash
#Service to auto update gitea installation
#Make sure curl and jq packages are installed

# Define PATH so cron can find commands
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

GITEA_HOME=/opt/gitea
DATE=$(date +%F-%H%M)
LOG_PATH="$GITEA_HOME/log"
LOG_FILE="$LOG_PATH/gitea-$DATE.log"
OLD_VERSION_PATH="$GITEA_HOME/old"

# Ensure directories exist
if [ ! -d "$LOG_PATH" ]; then
  mkdir -p "$LOG_PATH"
fi

if [ ! -d "$OLD_VERSION_PATH" ]; then
  mkdir -p "$OLD_VERSION_PATH"
fi

# Redirect all standard output (1) and standard error (2) to the log file
exec >> "$LOG_FILE" 2>&1

echo "GITEA UPDATER STARTED AT $DATE"

# Get Current Gitea Information
previousversion=$(gitea -v | awk '{print $3}')

# Get Version information from Git (Using -sS to hide progress but log errors)
newversion=$(curl -sS https://dl.gitea.com/gitea/version.json | jq -r .latest.version)

# Prevent silent failures if version checks fail
if [ -z "$previousversion" ] || [ -z "$newversion" ]; then
    echo "Error checking versions: Current ['$previousversion'], New ['$newversion']"
    exit 1
fi

if [ "$newversion" != "$previousversion" ]; then

    # Go to $GITEA_HOME
    cd "$GITEA_HOME" || exit 1

    # Make Old Version Unexecutable
    chmod -x gitea

    # Move Old Executable
    mv gitea "$OLD_VERSION_PATH/gitea_old_$previousversion"

    # Get System Information
    sys=$(uname -s)
    system=$(echo "$sys" | tr '[:upper:]' '[:lower:]')
    archictecture=$(dpkg --print-architecture)

    # Form URL Based on system information
    url="https://dl.gitea.com/gitea/$newversion/gitea-$newversion-$system-$archictecture"

    # Download update
    curl -sS "$url" -o gitea

    # Make new version executable
    chmod +x gitea

    # Restart Gitea service 
    sudo systemctl restart gitea.service
    echo "Update completed from $previousversion to $newversion"
else
    echo "No update available"
fi