#!/bin/bash
# info: add web domain
# options: USER DOMAIN [IP] [ALIASES] [PROXY_EXTENSIONS] [RESTART]
#
# The function adds virtual host to a server. In cases when ip is
# undefined in the script, "default" template will be used. The alias of
# www.domain.tld type will be automatically assigned to the domain unless
# "none" is transmited as argument. If ip have associated dns name, this
# domain will also get the alias domain-tpl.$ipname. An alias with the ip
# name is useful during the site testing while dns isn't moved to server yet.


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


# Argument definition
user=$1
domain=$2
domain_idn=$2
ip=$3
restart=$4      # will be moved to the end soon
aliases=$5
proxy_ext=$6

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

# Additional argument formatting
format_domain
format_domain_idn
format_aliases


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

is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
check_args '2' "$#" 'USER DOMAIN [IP] [RESTART] [ALIASES] [PROXY_EXTENSIONS]'
is_format_valid 'user' 'domain' 'aliases' 'ip' 'proxy_ext'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_package_full 'WEB_DOMAINS' 'WEB_ALIASES'
is_domain_new 'web' "$domain,$aliases"
is_dir_symlink $HOMEDIR/$user/web
if [ ! -z "$ip" ]; then
    is_ip_valid "$ip" "$user"
else
    get_user_ip
fi


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

# Reading user values
source $USER_DATA/user.conf

# Creating domain directories
mkdir -p $HOMEDIR/$user/web/$domain \
      $HOMEDIR/$user/web/$domain/public_html \
      $HOMEDIR/$user/web/$domain/public_shtml \
      $HOMEDIR/$user/web/$domain/document_errors \
      $HOMEDIR/$user/web/$domain/cgi-bin \
      $HOMEDIR/$user/web/$domain/private \
      $HOMEDIR/$user/web/$domain/stats \
      $HOMEDIR/$user/web/$domain/logs

# Creating domain logs
touch /var/log/$WEB_SYSTEM/domains/$domain.bytes \
      /var/log/$WEB_SYSTEM/domains/$domain.log \
      /var/log/$WEB_SYSTEM/domains/$domain.error.log
ln -f -s /var/log/$WEB_SYSTEM/domains/$domain.*log \
    $HOMEDIR/$user/web/$domain/logs/

# Adding domain skeleton
cp -r $WEBTPL/skel/* $HOMEDIR/$user/web/$domain/ >/dev/null 2>&1
for file in $(find "$HOMEDIR/$user/web/$domain/" -type f); do
    sed -i "s/%domain%/$domain/g" $file
done

# Changing file owner & permission
chown -R $user:$user $HOMEDIR/$user/web/$domain
chown root:$user /var/log/$WEB_SYSTEM/domains/$domain.* $conf
chmod 640 /var/log/$WEB_SYSTEM/domains/$domain.*
chmod 751 $HOMEDIR/$user/web/$domain $HOMEDIR/$user/web/$domain/*
chmod 551 $HOMEDIR/$user/web/$domain/stats $HOMEDIR/$user/web/$domain/logs
chmod 644 $HOMEDIR/$user/web/$domain/public_*html/*

# Addding PHP-FPM backend
if [ ! -z "$WEB_BACKEND" ]; then
    if [ -z "$BACKEND_TEMPLATE" ]; then
        BACKEND_TEMPLATE='default'
        if [ -z "$(grep BACKEND_TEMPLATE $USER_DATA/user.conf)" ]; then
            sed -i "s/^DNS_TEMPL/BACKEND_TEMPLATE='default'\nDNS_TEMPL/g" \
                $USER_DATA/user.conf
        else
            update_user_value "$user" '$BACKEND_TEMPLATE' "default"
        fi
    fi
    $BIN/v-add-web-domain-backend "$user" "$domain" $BACKEND_TEMPLATE
    check_result $? "Backend error" >/dev/null
fi

# Preparing domain aliases
if [ "$aliases" = 'none' ]; then
    ALIAS=''
else
    ALIAS="www.$domain"
    if [ -z "$aliases" ]; then
        ALIAS="www.$domain"
    else
        ALIAS="$aliases"
    fi
    
    ip_alias=$(get_ip_alias $domain)
    if [ ! -z "$ip_alias" ]; then
        ALIAS="$ALIAS,$ip_alias"
    fi
fi

# Preparing domain variables
prepare_web_domain_values

# Adding web server config
add_web_config "$WEB_SYSTEM" "$WEB_TEMPLATE.tpl"

# Adding proxy config
if [ ! -z "$PROXY_SYSTEM" ]; then
    PROXY_EXT="$proxy_ext"
    if [ -z "$proxy_ext" ]; then
        PROXY_EXT="jpg,jpeg,gif,png,ico,svg,css,zip,tgz,gz,rar,bz2,doc,xls"
        PROXY_EXT="$PROXY_EXT,exe,pdf,ppt,txt,odt,ods,odp,odf,tar,wav,bmp"
        PROXY_EXT="$PROXY_EXT,rtf,js,mp3,avi,mpeg,flv,html,htm"
    fi
    add_web_config "$PROXY_SYSTEM" "$PROXY_TEMPLATE.tpl"
fi


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

# Increasing counters
increase_ip_value "$local_ip"
increase_user_value "$user" '$U_WEB_DOMAINS'
increase_user_value "$user" '$U_WEB_ALIASES' "$alias_number"

# Generating timestamp
time_n_date=$(date +'%T %F')
time=$(echo "$time_n_date" |cut -f 1 -d \ )
date=$(echo "$time_n_date" |cut -f 2 -d \ )

# Adding domain in web.conf
echo "DOMAIN='$domain' IP='$ip' IP6='' ALIAS='$ALIAS' TPL='$WEB_TEMPLATE'\
 SSL='no' SSL_HOME='same' LETSENCRYPT='no' FTP_USER='' FTP_MD5=''\
 BACKEND='$BACKEND_TEMPLATE' PROXY='$PROXY_TEMPLATE' PROXY_EXT='$PROXY_EXT'\
 STATS='' STATS_USER='' STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0'\
 SUSPENDED='no' TIME='$time' DATE='$date'" >> $USER_DATA/web.conf

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

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

# Logging
log_history "added web domain $domain"
log_event "$OK" "$ARGUMENTS"

exit
