#!/bin/bash
# info: enable or disable demo mode
# options: ACTIVE
#
# This function will set the demo mode variable,
# which will prevent usage of certain v-scripts in the backend
# and prevent modification of objects in the control panel.
# It will also disable virtual hosts for Apache and NGINX
# for domains which have been created.

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

active=$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                         #
#----------------------------------------------------------#

check_args '1' "$#" 'ACTIVE'
is_format_valid 'active'

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

if [ "$active" = "yes" ]; then
	#TO-DO: Instructions for disabling vhosts
	# Disable API for Demo server
	$BIN/v-change-sys-api 'disable'
	$BIN/v-change-sys-config-value 'DEMO_MODE' 'yes'
elif [ "$active" = "no" ]; then
	# Note even v-change-sys-demo-mode can be called v-change-sys-config-value is protected to disable demo mode call
	# Use sed -i "s|^DEMO_MODE=.*'|DEMO_MODE='no'|g" $HESTIA/conf/hestia.conf
	#TODO: Instructions for enabling vhosts
	$BIN/v-change-sys-config-value 'DEMO_MODE' 'no'
fi

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

# Restarting web server
$BIN/v-restart-web "yes"
check_result $? "restart" > /dev/null 2>&1

$BIN/v-restart-proxy "yes"
check_result $? "restart" > /dev/null 2>&1

# Logging
if [ "$active" = "yes" ]; then
	$BIN/v-log-action "system" "Warning" "System" "Demonstration mode (restricted access) enabled."
else
	$BIN/v-log-action "system" "Warning" "Info" "Demonstration mode (restricted access) disabled."
fi

log_event "$OK" "$ARGUMENTS"

exit
