#!/bin/bash
# info: rebuild dns domains
# options: USER [RESTART]
#
# The function rebuilds web configuration files.


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

# Argument defenition
user=$1
restart=$2

# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/ip.sh
source $VESTA/func/rebuild.sh
source $VESTA/conf/vesta.conf


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

check_args '1' "$#" 'USER [RESTART]'
validate_format 'user'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"


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

user_domains=0
user_ssl=0
user_aliases=0
suspended_web=0
conf=$USER_DATA/web.conf
fields='$DOMAIN'
nohead=1
domain_counter=0

# Clean up old config
rm -f $HOMEDIR/$user/conf/tmp_*.conf

# Starting loop
for domain in $(shell_list); do
    ((++ domain_counter))
    rebuild_web_domain_conf
done

# Config path
web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"

# Touch vesta configs
touch $web_conf
if [ ! -z "$PROXY_SYSTEM" ]; then
    touch $proxy_conf
fi

# Checking if there is at least 1 domain
if [ "$domain_counter" -lt 1 ]; then
    # Deleting web configs
    rm -f $HOMEDIR/$user/conf/web/*
    sed -i "/.*\/$user\/.*$WEB_SYSTEM.conf/d" $web_conf
    if [ ! -z "$PROXY_SYSTEM" ]; then
        sed -i "/.*\/$user\/.*$PROXY_SYSTEM.conf/d" $proxy_conf
    fi

else
    # Clean web configs
    sed -i "/.*\/$user\/.*.conf/d" $web_conf
    if [ ! -z  "$PROXY_SYSTEM" ]; then
        sed -i "/.*\/$user\/.*.conf/d" $proxy_conf
    fi

    # Renaming tmp config
    tmp_conf="$HOMEDIR/$user/conf/web/tmp_$WEB_SYSTEM.conf"
    conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
    mv $tmp_conf $conf

    # Checking include
    web_include=$(grep "$conf" $web_conf )
    if [ -z "$web_include" ]; then
        echo "Include $conf" >> $web_conf
    fi

    # Checking SSL
    if [ "$ssl_change" = 'yes' ]; then
        tmp_conf="$HOMEDIR/$user/conf/web/tmp_s$WEB_SYSTEM.conf"
        conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
        mv $tmp_conf $conf
    fi
    ssl_include=$(grep "$conf" $web_conf )
    if [ -z "$ssl_include" ]; then
        echo "Include $conf" >> $web_conf
    fi

    # Checking proxy
    if [ ! -z "$PROXY_SYSTEM" ]; then
        if [ "$proxy_change" = 'yes' ]; then
            tmp_conf="$HOMEDIR/$user/conf/web/tmp_$PROXY_SYSTEM.conf"
            conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
            mv $tmp_conf $conf
            proxy_include=$(grep "$conf" $proxy_conf )
            if [ -z "$proxy_include" ]; then
                echo "include $conf;" >> $proxy_conf
            fi
        else
            conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
            sed -i "/.*\/$user\/.*$PROXY_SYSTEM.conf/d" $proxy_conf
            rm -f $HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf
        fi

        # Checking SSL proxy
        if [ "$proxy_change" = 'yes' ] && [ "$ssl_change" = 'yes' ]; then
            tmp_conf="$HOMEDIR/$user/conf/web/tmp_s$PROXY_SYSTEM.conf"
            conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
            mv $tmp_conf $conf
            proxy_include=$(grep "$conf" $proxy_conf )
            if [ -z "$proxy_include" ]; then
                echo "include $conf;" >> $proxy_conf
            fi
        else
            conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
            sed -i "/.*\/$user\/.*s$PROXY_SYSTEM.conf/d" $proxy_conf
            rm -f $HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf
        fi
    fi
fi


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Updating counters
update_user_value "$user" '$SUSPENDED_WEB' "$suspended_web"
update_user_value "$user" '$U_WEB_DOMAINS' "$user_domains"
update_user_value "$user" '$U_WEB_SSL' "$user_ssl"
update_user_value "$user" '$U_WEB_ALIASES' "$user_aliases"

# Restart web server
if [ "$restart" != 'no' ]; then
    $BIN/v-restart-web
    if [ $? -ne 0 ]; then
        exit $E_RESTART
    fi

    $BIN/v-restart-proxy
    if [ $? -ne 0 ]; then
        exit $E_RESTART
    fi
fi

# Logging
log_event "$OK" "$EVENT"

exit
