#!/bin/bash
# info: updating ssl certificate for domain
# options: USER DOMAIN SSL_DIR [RESTART]
#
# example: v-update-web-domain-ssl admin domain.com /home/admin/tmp
#
# This function updates the SSL certificate for a domain. Parameter ssl_dir is a path
# to directory where 2 or 3 ssl files can be found. Certificate file
# domain.tld.crt and its key domain.tld.key are mandatory. Certificate
# authority domain.tld.ca file is optional.

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

# Argument definition
user=$1
domain=$2
domain_idn=$2
ssl_dir=$3
restart=$4

# 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
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Additional argument formatting
format_domain
format_domain_idn
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?

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

check_args '3' "$#" 'USER DOMAIN SSL_DIR [RESTART]'
is_format_valid 'user' 'domain' 'ssl_dir'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_system_enabled "$WEB_SSL" 'SSL_SUPPORT'
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_value_exist 'web' 'DOMAIN' "$domain" '$SSL'
is_web_domain_cert_valid

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

# Deleting old certificate
rm -f $HOMEDIR/$user/conf/web/$domain/ssl/$domain.*
rm -f $USER_DATA/ssl/$domain.*

# Adding certificate to user data directory
cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.crt
cp -f $ssl_dir/$domain.key $USER_DATA/ssl/$domain.key
cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.pem
if [ -e "$ssl_dir/$domain.ca" ]; then
	cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/$domain.ca
	echo >> $USER_DATA/ssl/$domain.pem
	cat $USER_DATA/ssl/$domain.ca >> $USER_DATA/ssl/$domain.pem
fi
chmod 660 $USER_DATA/ssl/$domain.*

# Adding certificate to user dir
cp -f $USER_DATA/ssl/$domain.crt $HOMEDIR/$user/conf/web/$domain/ssl/$domain.crt
cp -f $USER_DATA/ssl/$domain.key $HOMEDIR/$user/conf/web/$domain/ssl/$domain.key
cp -f $USER_DATA/ssl/$domain.pem $HOMEDIR/$user/conf/web/$domain/ssl/$domain.pem
if [ -e "$USER_DATA/ssl/$domain.ca" ]; then
	cp -f $USER_DATA/ssl/$domain.ca $HOMEDIR/$user/conf/web/$domain/ssl/$domain.ca
fi

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

# Restarting web server
$BIN/v-restart-web "$restart"
check_result $? "Web restart failed" > /dev/null

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

# Execute bunkerweb_module if it's enabled
module_state=$($BIN/v-ext-modules state bunkerweb_module json | jq -r '.[0].STATE')
if [ "$module_state" = "enabled" ] && [ "$BUNKERWEB" = "yes" ]; then
    BUNKW_DIR=$HOMEDIR/$user/conf/web/$domain/ssl/bunkerweb
    if [ ! -d "$BUNKW_DIR" ]; then
        mkdir -p "$BUNKW_DIR"
        chmod 0755 "$BUNKW_DIR"
    fi
    cp -f "$HOMEDIR/$user/conf/web/$domain/ssl/$domain.crt" "$BUNKW_DIR/"
    cp -f "$HOMEDIR/$user/conf/web/$domain/ssl/$domain.key" "$BUNKW_DIR/"
    cp -f "$HOMEDIR/$user/conf/web/$domain/ssl/$domain.pem" "$BUNKW_DIR/"
    if [ -e "$HOMEDIR/$user/conf/web/$domain/ssl/$domain.ca" ]; then
        cp -f "$HOMEDIR/$user/conf/web/$domain/ssl/$domain.ca" "$BUNKW_DIR/"
    fi
    chown root:nginx "$BUNKW_DIR"/*
    chmod 0640 "$BUNKW_DIR"/*
    $BIN/v-ext-modules-run bunkerweb_module updssl "$domain" "$BUNKW_DIR/$domain.pem" "$BUNKW_DIR/$domain.key"
fi

# Logging
$BIN/v-log-action "$user" "Info" "Web" "SSL certificate updated (Domain: $domain)."
log_event "$OK" "$EVENT"

exit
