#!/bin/bash
# info: update web templates
#
# The function for changing the currently active system theme.

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

# Argument definition
theme=$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                             #
#----------------------------------------------------------#

# Initialize local directory if it does not exist
if [ ! -d "$HESTIA_THEMES_CUSTOM" ]; then
    mkdir -p $HESTIA_THEMES_CUSTOM
fi

# Theme argument and file detection
if [ -z "$theme" ]; then
    echo "ERROR: No theme specified."
    exit 1
else
    if [ -e "$HESTIA_THEMES/$theme.css" ]; then
        theme_conf="$HESTIA_THEMES/$theme.css"
    elif [ -e "$HESTIA_THEMES_CUSTOM/$theme.css" ]; then
        theme_conf="$HESTIA_THEMES_CUSTOM/$theme.css"
    else
        echo "ERROR: Unable to locate specified theme."
        exit 1
    fi

    # Replace theme override file
    rm -f $HESTIA/web/css/active-theme.css
    cp -f $theme_conf $HESTIA/web/css/active-theme.css

    # Set default theme in configuration file
    $BIN/v-change-sys-config-value 'THEME' $theme
fi

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

exit
