#!/bin/bash
# info: remove ssl force from domain
# options: USER DOMAIN [RESTART]
#
# The function removes force SSL configurations.


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

# Argument definition
user=$1
domain=$2
restart=$3

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

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

check_args '2' "$#" 'USER DOMAIN'
is_format_valid 'user' 'domain'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_object_valid 'web' 'DOMAIN' "$domain" "$SSL_FORCE"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode


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

# Load domain data
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)

# Remove forcessl configs
if [ -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.forcessl.conf ]; then
    rm -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.forcessl.conf
fi
if [ -f $HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.forcessl.conf ]; then
    rm -f $HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.forcessl.conf
fi

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

if [ -z "$SSL_FORCE" ]; then
    add_object_key "web" 'DOMAIN' "$domain" 'SSL_FORCE' 'SSL_HOME'
fi

update_object_value 'web' 'DOMAIN' "$domain" '$SSL_FORCE' 'no'

# Restart services if requested
if [ ! -z "$restart" ]; then
    $BIN/v-restart-web
    check_result $? "Web restart failed" >/dev/null

    $BIN/v-restart-proxy
    check_result $? "Proxy restart failed" >/dev/null
fi

# Logging
log_history "disabled automatic HTTP-to-HTTPS redirection for $domain"
log_event "$OK" "$ARGUMENTS"

exit