#! /bin/bash
# afrihost - script to update wordpress, themes and plugins
# autho: p
# last modified: 20220117

redhatreleasever=$(/usr/local/afribin/configs/centos_version_check.sh)
# cpanel check
if ! [ -f "/usr/local/cpanel/cpanel" ]; then
       echo "No cpanel"
       exit
fi

# choose the correct php binary
if [[ $redhatreleasever = "6" ]] ; then
 phpbin=usr/bin/php
else 
if [ -f  /opt/cpanel/ea-php84/root/usr/bin/php ] ; then 
 phpbin=/usr/bin/php
else 
 phpbin=/bin/php
fi
fi

if ! [ -f /bin/wp-cli.phar ]; then 
 curl -o /bin/wp-cli.phar   -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 
 chmod +x /bin/wp-cli.phar
fi

wPcliphar=/bin/wp-cli.phar

# run updates as the user and not root
echo " su -s /bin/bash -c '/opt/cpanel/ea-php84/root/"$phpbin" -d memory_limit=-1 $wPcliphar plugin update --all ; /opt/cpanel/ea-php84/root/"$phpbin" -d memory_limit=-1 $wPcliphar theme  update --all  ; frob upgrade' `pwd | cut -d "/" -f3` " | bash

# set sites php version to a stable one 
cpuser=`pwd | cut -d "/" -f3` 
cpdomain=`echo "awk -F ":"  '/"$cpuser"/ {print \$1}' /etc/trueuserdomains" |bash|awk '{print $1}'|sed 's/://'`
echo  "whmapi1 php_set_vhost_versions vhost-0="$cpdomain" version=ea-php84"  | bash
