#!/bin/bash
# info: restart operating system
# options: RESTART [DELAY]
#
# example: v-restart-system yes
#
# This function restarts operating system.

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

# Argument definition
restart=$1
delay=$2

# 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' "$#" 'RESTART [DELAY]'
is_format_valid 'restart'
if [ -n "$delay" ]; then
	is_format_valid "$delay" 'delay'
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

if [ "$restart" = 'yes' ]; then
	$BIN/v-log-action "system" "Warning" "System" "System restart initiated."
	if [ "$delay" ]; then
		echo "The server will restart in $delay seconds..."
		sleep "$delay"
	fi
	/sbin/reboot
fi

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

exit
