#!/bin/bash
# info: WordPress installer in one command line
# options: DOMAIN USER
#
# Credits to Luka Paunović for wp-cli implememtation

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

whoami=$(whoami)
if [ "$whoami" != "root" ]; then
    echo "You must be root to execute this script"
    exit 1
fi

# Argument definition
domain=$1

database="wp";
if [ $# -gt 1 ]; then
    database=$2
fi

email="info@$domain";
if [ $# -gt 2 ]; then
    email=$3
fi

user=$(/usr/local/vesta/bin/v-search-domain-owner $domain)
if [ -z "$user" ]; then
    check_result $E_NOTEXIST "domain $domain doesn't exist"
fi

# Importing system environment
source /etc/profile

# Includes
source /usr/local/vesta/func/main.sh
source /usr/local/vesta/func/db.sh

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

check_args '1' "$#" 'DOMAIN [DB_NAME] [EMAIL]'
is_format_valid 'domain' 'database' 'email'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

if [ ! -d "/home/$user" ]; then
    echo "User doesn't exist";
    exit 1;
fi

if [ ! -d "/home/$user/web/$domain/public_html" ]; then
    echo "Domain doesn't exist";
    exit 1;
fi

DBUSERSUF="$database";
DBUSERSUFB="$database";
DBUSER=$user\_$DBUSERSUFB;
DB_EXISTS=$(check_if_database_exists "$user" "$DBUSER")

if [ "$DB_EXISTS" = "yes" ]; then
    i=1;
    while [ $i -lt 99 ]; do
        i=$((i+1));
        DBUSERSUF="${DBUSERSUFB}${i}";
        DBUSER=$user\_$DBUSERSUF;
        DB_EXISTS=$(check_if_database_exists "$user" "$DBUSER")
        if [ "$DB_EXISTS" = "no" ]; then
            break;
        fi
    done
fi

PASSWDDB=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)

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

PROTOCOL='http'
if [ ! -f "/home/$user/conf/web/ssl.$domain.ca" ]; then
    /usr/local/vesta/bin/v-add-letsencrypt-domain "$user" "$domain" "www.$domain" "yes"
fi

if [ -f "/home/$user/conf/web/ssl.$domain.ca" ]; then
    PROTOCOL='https'
    if [ -f "/usr/local/vesta/data/templates/web/nginx/force-https.stpl" ]; then
        /usr/local/vesta/bin/v-change-web-domain-proxy-tpl  "$user" "$domain" "force-https" "jpeg,jpg,png,gif,bmp,ico,svg,tif,tiff,css,js,ttf,otf,webp,txt,csv,rtf,doc,docx,xls,xlsx,ppt,pptx,odf,odp,ods,odt,pdf,psd,ai,eot,eps,ps,zip,tar,tgz,gz,rar,bz2,7z,aac,m4a,mp3,mp4,ogg,wav,wma,3gp,avi,flv,m4v,mkv,mov,mpeg,mpg,wmv,exe,iso,dmg,swf,woff,woff2" "yes"
    fi
fi

/usr/local/vesta/bin/v-add-database "$user" "$DBUSERSUF" "$DBUSERSUF" "$PASSWDDB" "mysql"

if [ ! -f "/usr/local/bin/wp" ]; then
    echo "=== Downloading latest wp-cli"
    wget -nv https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/local/bin/wp
    chmod +x /usr/local/bin/wp
fi

WORKINGDIR="/home/$user/web/$domain/public_html"
rm -rf $WORKINGDIR/*
cd $WORKINGDIR

sudo -H -u$user wp core download
sudo -H -u$user wp core config --dbname=$DBUSER --dbuser=$DBUSER --dbpass=$PASSWDDB

password=$(LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 12)

sudo -H -u$user wp core install --url="$domain" --title="$domain" --admin_user="admin" --admin_password="$password" --admin_email="$email" --path=$WORKINGDIR

mysql -u$DBUSER -p$PASSWDDB -e "USE $DBUSER; update wp_options set option_value = '$PROTOCOL://$domain' where option_name = 'siteurl'; update wp_options set option_value = '$PROTOCOL://$domain' where option_name = 'home';"

echo "================================================================="
echo "Installation is complete. Your username/password is listed below."
echo ""
echo "Site: $PROTOCOL://$domain/"
echo ""
echo "Login: $PROTOCOL://$domain/wp-admin/"
echo "Username: admin"
echo "Password: $password"
echo ""
echo "================================================================="

chown -R $user:$user $WORKINGDIR

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

echo "v-install-wordpress: Done."

log_event "$OK" "$ARGUMENTS"

exit