#!/bin/bash
# info: update web templates
# options: [RESTART]
#
# This 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.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
branch=$1

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$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 "- main:               work-in-progress code for the next 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://dev.putey.net/bayrepo/hestiacp/raw/branch/master/src/rpm/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                             #
#----------------------------------------------------------#

$BIN/v-log-action "system" "Info" "System" "Update branch changed (Value: $branch)."
exit
