#!/bin/bash
# info: add web domain
# options: USER DOMAIN IP [RESTART]
#
# The function adds virtual host to a server. In cases when a template is
# undefined in the script, the template "default" will be used. The alias of
# www.domain.tld type will be automatically assigned to the domain. If ip have
# assocated 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 a server yet.


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

# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
ip=$3
restart=$4

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


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

check_args '3' "$#" 'USER DOMAIN IP [RESTART]'
validate_format 'user' 'domain' 'ip'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_domain_new 'web'
is_ip_valid
is_ip_avalable
is_package_full 'WEB_DOMAINS'

template=$(get_user_value '$WEB_TEMPLATE')
is_web_template_valid


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

# Define real ip in case of NAT
IP=$ip
ip=$(get_real_ip $ip)

# Defining domain aliases
ip_name=$(get_ip_name)
ip_name_idn=$(idn -t --quiet -a "$ip_name")
domain_alias="www.$domain"
domain_alias_idn="www.$domain_idn"
if [ ! -z "$ip_name" ]; then
    domain_alias_dash="${domain//./-}.$ip_name"
    domain_alias_dash_idn="${domain_idn//./-}.$ip_name_idn"
    aliases="$domain_alias,$domain_alias_dash"
    aliases_idn="$domain_alias_idn,$domain_alias_dash_idn"
    alias_string="ServerAlias $domain_alias_idn $domain_alias_dash_idn"
else
    aliases="$domain_alias"
    aliases_idn="$domain_alias_idn"
    alias_string="ServerAlias $domain_alias_idn"
fi

# Defining vars for add_config function
group="$user"
email="info@$domain"
docroot="$HOMEDIR/$user/web/$domain/public_html"
tpl_file="$WEBTPL/$WEB_SYSTEM/$template.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"

# Adding web config
add_web_config

# Building directory tree
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

# Adding 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

# Adding symlink for logs
ln -f -s /var/log/$WEB_SYSTEM/domains/$domain.*log \
    $HOMEDIR/$user/web/$domain/logs/

# Adding domain skeleton
if [ -e "$WEBTPL/skel/public_html/" ]; then
    cp -r $WEBTPL/skel/public_html/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/public_shtml/" ]; then
    cp -r $WEBTPL/skel/public_shtml/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/document_errors/" ]; then
    cp -r $WEBTPL/skel/document_errors/ $HOMEDIR/$user/web/$domain/
fi
if [ -e "$WEBTPL/skel/cgi-bin/" ]; then
    cp -r $WEBTPL/skel/cgi-bin/ $HOMEDIR/$user/web/$domain/
fi

# Changing tpl values
for file in $(find "$HOMEDIR/$user/web/$domain/" -type f); do
    sed -i "s/%domain%/$domain/g" $file
done

# Changing file owner
chown -R $user:$user $HOMEDIR/$user/web/$domain
chown root:$user /var/log/$WEB_SYSTEM/domains/$domain.*
chown root:$user $conf

# Changing file permissions
chmod 640 $conf
chmod 551 $HOMEDIR/$user/web/$domain
chmod 751 $HOMEDIR/$user/web/$domain/private
chmod 751 $HOMEDIR/$user/web/$domain/cgi-bin
chmod 751 $HOMEDIR/$user/web/$domain/public_html
chmod 751 $HOMEDIR/$user/web/$domain/public_shtml
chmod 751 $HOMEDIR/$user/web/$domain/document_errors
chmod -f -R 665 $HOMEDIR/$user/web/$domain/cgi-bin/*
chmod -f -R 665 $HOMEDIR/$user/web/$domain/public_html/*
chmod -f -R 665 $HOMEDIR/$user/web/$domain/document_errors/*
chmod 551 $HOMEDIR/$user/web/$domain/stats
chmod 551 $HOMEDIR/$user/web/$domain/logs
chmod 640 /var/log/$WEB_SYSTEM/domains/$domain.*

# Running template trigger
if [ -x $WEBTPL/$WEB_SYSTEM/$template.sh ]; then
    $WEBTPL/$WEB_SYSTEM/$template.sh $user $domain $ip $HOMEDIR $docroot
fi

# Checking web config
web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
if [ -z "$(grep $conf $web_conf)" ]; then
    echo "Include $conf" >> $web_conf
fi


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

# Increasing counters
increase_ip_value "$ip"
increase_user_value "$user" '$U_WEB_DOMAINS'
increase_user_value "$user" '$U_WEB_ALIASES'

# Defining domain variables
str="DOMAIN='$domain' IP='$IP' IP6='' ALIAS='$aliases' TPL='$template'"
str="$str SSL='no' SSL_HOME='same' FTP_USER='' FTP_MD5=''"
str="$str PROXY='' PROXY_EXT='' STATS='' STATS_USER=''"
str="$str STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0' SUSPENDED='no'"
str="$str TIME='$TIME' DATE='$DATE'"

# Registering domain
echo "$str" >> $USER_DATA/web.conf
chmod 660 $USER_DATA/web.conf

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

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

exit
