#!/bin/bash
# info: add system user
# options: USER PASSWORD EMAIL [PACKAGE] [FNAME] [LNAME]
#
# The function creates new user account.


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

# Argument defenition
user=$1
password=$2
email=$3
package=${4-default}
fname=$5
lname=$6

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

# Hiding password
A2='******'
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"

is_user_free() {
    check_sysuser=$(cut -f 1 -d : /etc/passwd | grep -w "$user" )
    if [ ! -z "$check_sysuser" ] || [ -e "$USER_DATA" ]; then
        echo "Error: user $user exists"
        log_event "$E_EXISTS" "$EVENT"
        exit $E_EXISTS
    fi
}


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

check_args '3' "$#" 'USER PASSWORD EMAIL [PACKAGE] [FNAME] [LNAME]'
validate_format 'user' 'password' 'email' 'package'
if [ ! -z "$fname" ]; then
    validate_format 'fname' 'lname'
fi

is_user_free "$user"
is_package_valid


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

# Parsing package data
pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |grep -v DATE)

# Checking shell
shell_conf=$(echo "$pkg_data" | grep 'SHELL' | cut -f 2 -d \')
shell=$(grep -w "$shell_conf" /etc/shells |head -n1)

# Adding user
/usr/sbin/useradd "$user" -s "$shell" -c "$email" -m -d "$HOMEDIR/$user"
if [ $? -ne 0 ]; then
    echo "Error: user creation failed"
    log_event "$E_INVALID" "$EVENT"
    exit $E_INVALID
fi

# Adding password
echo "$user:$password" | /usr/sbin/chpasswd

# Building directory tree
mkdir $HOMEDIR/$user/conf

if [ ! -z "$WEB_SYSTEM" ]; then
    mkdir $HOMEDIR/$user/conf/web
    mkdir $HOMEDIR/$user/web
    mkdir $HOMEDIR/$user/tmp
    chmod 751 $HOMEDIR/$user/conf/web
    chmod 751 $HOMEDIR/$user/web
    chmod 771 $HOMEDIR/$user/tmp
    chown $user:$user $HOMEDIR/$user/web
    chown $user:$user $HOMEDIR/$user/tmp
fi

if [ ! -z "$MAIL_SYSTEM" ]; then
    mkdir $HOMEDIR/$user/conf/mail
    mkdir $HOMEDIR/$user/mail
    chmod 751 $HOMEDIR/$user/mail
    chmod 751 $HOMEDIR/$user/conf/mail
fi

if [ ! -z "$DNS_SYSTEM" ]; then
    mkdir $HOMEDIR/$user/conf/dns
    chmod 751 $HOMEDIR/$user/conf/dns
fi 

# Set permissions
chmod a+x $HOMEDIR/$user
chattr +i $HOMEDIR/$user/conf


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

# Adding user dir
mkdir $USER_DATA
chmod 770 $USER_DATA

# Creating configuration files and pipes
touch $USER_DATA/backup.conf
chmod 660 $USER_DATA/backup.conf
touch $USER_DATA/history.log
chmod 660 $USER_DATA/history.log
touch $USER_DATA/stats.log
chmod 660 $USER_DATA/stats.log

echo "$BIN/v-update-user-disk $user" >> $VESTA/data/queue/disk.pipe

if [ ! -z "$WEB_SYSTEM" ]; then
    mkdir $USER_DATA/ssl
    chmod 770 $USER_DATA/ssl
    touch $USER_DATA/web.conf
    chmod 660 $USER_DATA/web.conf
    echo "$BIN/v-update-web-domains-traff $user" \
        >> $VESTA/data/queue/traffic.pipe
    echo "$BIN/v-update-web-domains-disk $user" >> $VESTA/data/queue/disk.pipe
fi

if [ ! -z "$DNS_SYSTEM" ]; then
    mkdir $USER_DATA/dns
    chmod 770 $USER_DATA/dns
    touch $USER_DATA/dns.conf
    chmod 660 $USER_DATA/dns.conf
fi

if [ ! -z "$MAIL_SYSTEM" ]; then
    mkdir $USER_DATA/mail
    chmod 770 $USER_DATA/mail
    touch $USER_DATA/mail.conf
    chmod 660 $USER_DATA/mail.conf
    echo "$BIN/v-update-mail-domains-disk $user" >> $VESTA/data/queue/disk.pipe
fi

if [ ! -z "$DB_SYSTEM" ]; then
    touch $USER_DATA/db.conf
    chmod 660 $USER_DATA/db.conf
    echo "$BIN/v-update-databases-disk $user" >> $VESTA/data/queue/disk.pipe
fi

if [ ! -z "$CRON_SYSTEM" ]; then
    touch $USER_DATA/cron.conf
    chmod 660 $USER_DATA/cron.conf
fi

# Filling user config
if [ "$user" != 'admin' ]; then
    ip_avail=$($BIN/v-list-user-ips admin plain |grep -w shared|wc -l)
    u_users=0
else
    ip_avail=0
    u_users=1
fi

echo "FNAME='$fname'
LNAME='$lname'
PACKAGE='$package'
$pkg_data
CONTACT='$email'
CRON_REPORTS='yes'
MD5='$(awk -v user=$user -F : 'user == $1 {print $2}' /etc/shadow)'
RKEY='$(gen_password)'
SUSPENDED='no'
SUSPENDED_USERS='0'
SUSPENDED_WEB='0'
SUSPENDED_DNS='0'
SUSPENDED_MAIL='0'
SUSPENDED_DB='0'
SUSPENDED_CRON='0'
IP_AVAIL='$ip_avail'
IP_OWNED='0'
U_USERS='$u_users'
U_DISK='0'
U_DISK_DIRS='0'
U_DISK_WEB='0'
U_DISK_MAIL='0'
U_DISK_DB='0'
U_BANDWIDTH='0'
U_WEB_DOMAINS='0'
U_WEB_SSL='0'
U_WEB_ALIASES='0'
U_DNS_DOMAINS='0'
U_DNS_RECORDS='0'
U_MAIL_DOMAINS='0'
U_MAIL_DKIM='0'
U_MAIL_ACCOUNTS='0'
U_DATABASES='0'
U_CRON_JOBS='0'
U_BACKUPS='0'
LANGUAGE=''
TIME='$TIME'
DATE='$DATE'" > $USER_DATA/user.conf
chmod 660 $USER_DATA/user.conf

# Updating quota
if [ "$DISK_QUOTA" = 'yes' ]; then
    $BIN/v-update-user-quota "$user"
fi

# Updating admin counter
if [ "$user" != 'admin' ]; then
    increase_user_value 'admin' '$U_USERS'
fi

# Run template trigger
if [ -x "$VESTA/data/packages/$package.sh" ]; then
    $VESTA/data/packages/$package.sh "$user" "$email" "$fname" "$lname"
fi

# Logging
log_history "added system user $user" '' 'admin'
log_event "$OK" "$EVENT"

exit
