#!/bin/bash
# info: update web templates
# options: [RESTART]
#
# The function for changing the release branch for the
# Hestia Control Panel. This allows the user to switch between
# stable and pre-release builds which will automaticlly update
# based on the appropriate release schedule if auto-update is
# turned on.

#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
branch=$1

# Includes
source $HESTIA/func/main.sh
source $HESTIA/conf/hestia.conf


#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

# Perform verification if read-only mode is enabled
check_hestia_demo_mode


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#
if [ -z "$branch" ]; then
    echo "Error: no release branch specified."
    echo "Usage: v-change-sys-release branchname"
    echo ""
    echo "Release branches:"
    echo "- release:            the latest stable release"
    echo "- beta:               beta and release candidate test releases"
    echo "- develop:            unstable development builds"
    echo ""
    echo "Integration branches (not tied to a specific release/version):"
    echo "- staging/fixes:      contains new fixes since the last release."
    echo "- staging/features:   contains new features since the last release."
    echo ""
    echo "You can also specify another branch name from the"
    echo "GitHub repository to install the code from that branch."
    echo ""
    exit
else
    # Check that requested branch exists
    echo "Checking for existence of $branch branch..."
    branch_check=$(curl -s --head -w %{http_code} https://raw.githubusercontent.com/hestiacp/hestiacp/$branch/src/deb/hestia/control -o /dev/null)
    if [ $branch_check -ne "200" ]; then
        echo "Error: invalid branch name specified."
        exit 1
    fi

    # Remove old branch variable
    sed -i "/RELEASE_BRANCH/d" $HESTIA/conf/hestia.conf
    
    # Set new branch variable
    echo "RELEASE_BRANCH='$branch'" >> $HESTIA/conf/hestia.conf
    echo "Updated system to update from Git using branch: $branch"
fi

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
