export VESTA=/usr/local/vesta

regen_ssl_cert()
{
    info "Regenerating SSL certificate"
    $VESTA/bin/v-generate-ssl-cert $(hostname) vesta@vscale.io 'RU' 'Russian Federation' \
     'Saint-Petersburg' 'Vesta Control Panel' 'IT' > /tmp/vst.pem

    # Parsing merged certificate file
    crt_end=$(grep -n "END CERTIFICATE-" /tmp/vst.pem | cut -f 1 -d:)
    key_start=$(grep -n "BEGIN RSA" /tmp/vst.pem | cut -f 1 -d:)
    key_end=$(grep -n  "END RSA" /tmp/vst.pem | cut -f 1 -d:)

    # Adding SSL certificate
    cd $VESTA/ssl
    sed -n "1,${crt_end}p" /tmp/vst.pem > certificate.crt
    sed -n "$key_start,${key_end}p" /tmp/vst.pem > certificate.key
    chown root:mail $VESTA/ssl/*
    chmod 660 $VESTA/ssl/*
    rm /tmp/vst.pem
}

config_mysql()
{
    info "Configuring MySQL"
    if [[ -n $panelpassword ]]; then
    mysql -uroot -p82nQ4kpp -e "UPDATE mysql.user SET authentication_string=PASSWORD('$panelpassword') WHERE User='admin_default' OR User='roundcube' OR User='root' OR User='phpmyadmin';FLUSH PRIVILEGES;"
    echo -e "[client]\npassword='$panelpassword'\n" > /root/.my.cnf
    chmod 600 /root/.my.cnf
    sed -i "s/82nQ4kpp/$panelpassword/g" $VESTA/conf/.mysql.localhost
    sed -i "s/82nQ4kpp/$panelpassword/g" $VESTA/conf/mysql.conf
    mysql -uroot -p$panelpassword -e "DROP DATABASE IF EXISTS admin_default"
    mysql -uroot -p$panelpassword -e "DROP USER IF EXISTS admin_default"
    else
        exit 2
    fi
}

pwd_vesta()
{
    info "Configuring Roundcube Web mail"
    sed -i "s/82nQ4kpp/$panelpassword/g" /etc/roundcube/debian-db-roundcube.php

    info "Configuring PhpMyAdmin"
    sed -e "s/pass=.*/pass='$panelpassword';/g" -i /etc/phpmyadmin/config-db.php
}

admin_usr()
{
    info "Setting admin user"
    email=vesta@vscale.io
    $VESTA/bin/v-add-user admin $panelpassword $email default System Administrator
    $VESTA/bin/v-change-user-shell admin bash
    $VESTA/bin/v-change-user-language admin en
    $VESTA/bin/v-change-user-password admin $panelpassword
}

restart_srv()
{
    info "Restarting services"
    for service in apache2 nginx vesta; do
    service $service restart
    done
}

setting_ip()
{
    info "Setting IP"
    IP_CONFIG=$(ls $VESTA/data/ips)
    IP=$(ip addr show scope global up | awk -F '[ /]' '/inet/ {print $6}' | xargs)
    sed -i /^NAT/d $VESTA/data/ips/$IP_CONFIG
    $VESTA/bin/v-update-sys-ip
    sed -i -e "s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/$IP/g" $VESTA/data/users/admin/web.conf
    $VESTA/bin/v-change-dns-domain-ip admin defaultdomain $IP
    $VESTA/bin/v-restart-web
}

disable_clamav()
{
    info "Stop and disable clamav-daemon service"
    systemctl stop clamav-daemon
    systemctl reset-failed clamav-daemon
    systemctl disable clamav-daemon
}

change_dsm_pwd()
{
  info "Change password for debian-sys-maint user"
  dsm_usr="$(awk '/user/{print $NF; exit;}' /etc/mysql/debian.cnf)"
  old_dsm_usr_pwd="$(awk '/password/{print $NF; exit;}' /etc/mysql/debian.cnf)"
  rnd_dsm_pwd="$(< /dev/urandom tr -dc A-Za-z0-9 | head -c 32;echo)"
  mysql --user=${dsm_usr} --password=${old_dsm_usr_pwd} --execute="ALTER USER '${dsm_usr}'@'localhost' IDENTIFIED BY '${rnd_dsm_pwd}';"

  cat > /etc/mysql/debian.cnf <<EOF
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = ${dsm_usr}
password = ${rnd_dsm_pwd}
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = ${dsm_usr}
password = ${rnd_dsm_pwd}
socket   = /var/run/mysqld/mysqld.sock
EOF

  systemctl restart mysql.service
}

app_setup()
{
    regen_ssl_cert
    if [ $? != 0 ]; then
        error "ERROR: Failed to regenerate SSL certificate..."
        exit 3
    fi
    config_mysql
    if [ $? != 0 ]; then
        error "ERROR: Failed to configure MySQL..."
        exit 3
    fi
    pwd_vesta
    if [ $? != 0 ]; then
        error "ERROR: Failed to set password for Roundcube and PhpMyAdmin..."
        exit 3
    fi
    admin_usr
    if [ $? != 0 ]; then
        error "ERROR: Failed to configuring admin user for Vesta CP..."
        exit 3
    fi
    setting_ip
    if [ $? != 0 ]; then
        error "ERROR: Failed to set IP for Vesta CP..."
        exit 3
    fi
    restart_srv
    if [ $? != 0 ]; then
        error "ERROR: Failed to restart services..."
        exit 3
    fi
    disable_clamav
    if [ $? != 0 ]; then
        error "ERROR: Failed to disable ClamAV..."
        exit 3
    fi
    change_dsm_pwd
    if [ $? != 0 ]; then
        error "ERROR: change debian-system-maint failed..."
        exit 3
    fi
}
