#!/usr/bin/bash
# shellcheck disable=SC2317  # Don't warn about unreachable commands in this file
# WP Optimize script v2.030723
#begin variables
Red=$'\e[1;31m';
Green=$'\e[1;32m';
Clear=$'\e[0m';
CPUSER="$(whoami)"
pwd
read  -r -p "Press enter to continue";
# shellcheck source=/dev/null
source ./way-pass-variables.txt
# test -d /home/"$CPUSER"/.wp-opt || mkdir /home/"$CPUSER"/.wp-opt
# test -d /home/"$CPUSER"/.wp-opt/"$DOMAIN" || mkdir /home/"$CPUSER"/.wp-opt/"$DOMAIN"
touch /home/"$CPUSER"/.redis.on;
cd "$HOMEDIR"/ 
wp plugin status | tee /home/"$CPUSER"/"$DOMAIN".plugin.txt;
wp theme status | tee /home/"$CPUSER"/"$DOMAIN".theme.txt;
wp option get home | tee /home/"$CPUSER"/"$DOMAIN".home.txt;
wp option get siteurl | tee /home/"$CPUSER"/"$DOMAIN".siteurl.txt;
wphome="$(< /home/"$CPUSER"/"$DOMAIN".home.txt)";
wpsite="$(< /home/"$CPUSER"/"$DOMAIN".siteurl.txt)";
touch /home/"$CPUSER"/"$DOMAIN".cp.log.txt
touch /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
#end variables
clear -x;
#backup time
echo "$Green Yes we need to create a backup of the database and site
or No a backup has one already been made?" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt; 
select yn in "Yes" "No"; do
  case $yn in
    Yes ) RQBKUP=1
	echo "$RQBKUP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt
	touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.bu-n
	break;;
    No ) RQBKUP=0
	read -r -p "Enter in other Backup details: " WHYNOBKUP
	touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.bu-d
	break;;
  esac
done;
cd "$HOMEDIR"/
if [ "$RQBKUP" = 1 ];
then
    read  -r -p "Press enter to backup the database"
    wp db export
    wait $!
    read -r -p "Press enter to backup the site";
    touch sitebackup.gz
    tar --exclude=sitebackup.gz -zcvf sitebackup.gz .
    echo $?
    echo "sitebackup.gz. Both DB backup and the site will be in the archive." | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    mv /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.bu-n /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.bu-d
fi;
if [ "$RQBKUP" = 0 ]; then
  echo "No sript backup was made because $WHYNOBKUP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
fi;
#end backup time
read -r -p "Press enter to continue";
clear -x
#check wp directory structure
echo "Check for proper directory structure";
echo "WordPress direcotry structure";
if [ "$wphome" == "$wpsite" ]; then
  wpconfigCHANGE=0
  echo "SAME"
  echo "The home url for WordPress is $wphome and is the same as the site URL for WordPress which is $wpsite. 
  This means the default wp-config.php is all that is needed." | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  else  wpconfigCHANGE=1
  echo "not SAME"
fi;
read -r -p "Press enter to continue";
if (("$(grep -q "litespeed-cache" /home/"$CPUSER"/"$DOMAIN".plugin.txt)")) && ( [ "$wphome" != "$wpsite" ] ); then
  wp litespeed-option export
  wp plugin uninstall litespeed-cache --deactivate 
fi;
if [ "$wphome" != "$wpsite" ]; then
  echo "$Red The home url for WordPress is $wphome and is NOT the same as the site URL for WordPress which is $wpsite. 
  This means the wp-config.php will need to be edited.  It need to be configured beofre you can go any further. $Clear"
  echo "INSTRUCTIONS ARE HERE: https://docs.google.com/document/d/1FLe_rlX1z72yu6P3VfrXR6Uh45zWPCnqIQWt54Iz9zk/edit#bookmark=id.3gh7eo68uimv "
  read -r -p "Press enter to continue";
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.cngd-wp-config; 
  echo "The home url for WordPress is $wphome and is NOT the same as the site URL for WordPress which is $wpsite. 
  This means the wp-config.php was edited accordingly."  | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
fi;
#end check wp directory structure
#check wpoptimize cache
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "wp-optimize")" -gt 0 ]; then
  CSPLUG_wp_optimize=1;
  echo "Need to change fully deactivate wp-optimize"
  echo "$Red But before deleting go into the settings and disable
  the page cache and clear the cache https://getwpo.com/?s=uninstall
  https://getwpo.com/documentation/#Caching-your-site $Clear";
  read -r -p "ONCE THAT IS DONE Press enter to continue";
  wp plugin uninstall wp-optimize --deactivate 
  echo "uninstalled wp_optimize" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.wp-optimize-d-r;
  else CSPLUG_wp_optimize=0;
fi;
#end check wpoptimize cache
#check for w-3 total
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "w3-total-cache")" -gt 0 ]; then
    echo "$Red Need to fully deactivate w3-total-cache
    https://www.thewebmaster.com/how-to-uninstall-w3-total-cache-manually
    Turn off all Caching under General Settings and clear cache
        Preview mode - Disable
        Page Cache - Disable
        Minify - Disable
        Database Cache - Disable
        Object Cache - Disable
        Browser Cache - Disable
        CDN - Disable
    MAKE SURE TO SAVE ALL SETTINGS $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp w3-total-cache flush all
    wp plugin uninstall w3-total-cache --deactivate
    test -d "$HOMEDIR"/wp-content/cache && rm -r "$HOMEDIR"/wp-content/cache;
    test -d "$HOMEDIR"/wp-content/w3tc-config && rm -r "$HOMEDIR"/wp-content/w3tc-config;
    test -f "$HOMEDIR"/wp-content/object-cache.php && rm "$HOMEDIR"/wp-content/object-cache.php;
    test -f "$HOMEDIR"/wp-content/advanced-cache.php && rm "$HOMEDIR"/wp-content/advanced-cache.php;
    test -f "$HOMEDIR"/wp-content/dbcache.php && rm "$HOMEDIR"/wp-content/dbcache.php;
    test -d  "$HOMEDIR"/wp-content/upgrade && rm -r "$HOMEDIR"/wp-content/upgrade
    test -d "$HOMEDIR"/wp-content/plugins/w3-total-cache && rm -r "$HOMEDIR"/wp-content/plugins/w3-total-cache;
    echo "w3-total-cache did a clean uninstall"  | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.w3-total-cache-d-r;
    else CSPLUG_w3_total_cache=0;
fi;
#end check for w-3 total
# check for WP Fastest Cache
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "wp-fastest-cache")" -gt 0 ]; then
    CSPLUG_wp_fastest_cache=1 
    echo "$Red https://bloggingcommerce.com/en/how-to-completely-uninstall-cache-plugins-in-wordpress-updated/#Como_desinstalar_completamente_WP_Fastest_Cache
    First of all, if you activated the minimization of 
    HTML and CSS files in its settings, before deactivating 
    the plugin deactivate those options. $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp fastest-cache clear all;
    wp plugin uninstall wp-fastest-cache --deactivate 
    test -d "$HOMEDIR"/wp-content/cache && rm -r "$HOMEDIR"/wp-content/cache;
    test -d "$HOMEDIR"/wp-content/plugins/wp-fastest-cache && rm -r "$HOMEDIR"/wp-content/plugins/wp-fastest-cache;
    echo "$Red verify that it has been completely uninstalled:
        A - Deleted everything from # BEGIN WpFastestCache 
        to # END WpFastestCache in the htaccess file, both lines included.
        B - Delete everything from # BEGIN GzipWpFastestCache 
        to # END GzipWpFastestCache in the htaccess file, both lines included.
        C - Delete everything from # BEGIN LBCWpFastestCache 
        to # END LBCWpFastestCache in the htaccess file, both lines included. $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";   
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.wp-fastest-cache-d-r;
     echo "wp fastest cache uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
    else CSPLUG_wp_fastest_cache=0
fi;
#end check for WP Fastest Cache
# check for wp-super-cache
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "wp-super-cache")" -gt 0 ]; then
    wp plugin uninstall wp-fastest-cache --deactivate
    test -f "$HOMEDIR"/wp-content/advanced-cache.php && rm "$HOMEDIR"/wp-content/advanced-cache.php;
    test -f "$HOMEDIR"/wp-content/wp-cache-config.php && rm "$HOMEDIR"/wp-content/wp-cache-config.php;
    test -d "$HOMEDIR"/wp-content/cache && rm -r "$HOMEDIR"/wp-content/cache;
    test -d "$HOMEDIR"/wp-content/plugins/wp-super-cache && rm -r "$HOMEDIR"/wp-content/plugins/wp-super-cache;
    echo "$Red https://bloggingcommerce.com/en/how-to-completely-uninstall-cache-plugins-in-wordpress-updated/#Como_desinstalar_completamente_WP_Super_Cache
    Verify the following:
        htacees file Deleted everything from #BEGIN WPSuperCache to #END WPSuperCache
        wpconfig.php file lines deleted that end in // Added by WP-Cache Manager $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.wp-super-cache-d-r;
     echo "wp super cache uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_wp_super_cache=0;
fi;
#end check for wp-super-cache
# check for hummingbird-performance
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "hummingbird-performance")" -gt 0 ]; then
  echo "$Red https://wordpress.org/support/topic/need-directions-on-how-to-completely-remove-all-hummingbird-files/#:~:text=To%20uninstall%20Humminbird%20complete%2C%20please,entries%20while%20uninstalling%20the%20plugin.
  To uninstall Humminbird complete, please make sure to enable 
  the Reset option for Settings and Remove option for Data at 
  WP Dashboard >> Hummingbird >> Settings >> Data & Settings >> Uninstallation. 
  This will make sure to clear all the settings and database entries 
  while uninstalling the plugin. $Clear";
  read -r -p "ONCE THAT IS DONE Press enter to continue";
  wp plugin uninstall hummingbird-performance --deactivate 
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.hummingbird-d-r;
  echo "hummingbird uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  CSPLUG_hummingbird_performance=1;
  else CSPLUG_hummingbird_performance=0;
fi;
#end check for hummingbird-performance
# check for wp-rocket
if [ "$CSPLUG_wp_rocket" = 1 ]; then
    echo "$Red Make sure WP-Rocket is the most current version, new uninstall routine $Clear"
    read -r -p "Press enter to continue";
    wp plugin uninstall wp-rocket --deactivate
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.comet-cache-d-r
     echo "wp rocket uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_wp_rocket=0;
fi;
#end check for wp-rocket
# check for comet-cache
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "comet-cache")" -gt 0 ]; then
  CSPLUG_comet_cache=1;
    echo "$Red https://cometcache.com/kb-article/how-do-i-uninstall-comet-cache/
    Disable Plugin Deletion Safeguards in
    WP Dashboard ----> Comet Cache ---> Plugin Options 
    ---> Plugin Deletion Safeguards $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall comet-cache --deactivate
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.comet-cache-d-r
     echo "comet cache uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  else CSPLUG_comet_cache=0;
fi;
#end check for comet-cache
#check for autoptimize
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "autoptimize")" -gt 0 ]; then
  CSPLUG_autoptimize=1;
    wp plugin uninstall autoptimize --deactivate
    read -r -p "Press enter to continue";
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.autoptimize-d-r
    echo "autoptimize uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_autoptimize=0;
fi;
#end check for autoptimize
# check for perfmatters
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "perfmatters")" -gt 0 ]; then
  CSPLUG_perfmatters=1;
    echo "$Red https://perfmatters.io/docs/clean-uninstall/ ;
    Click into the Perfmatters plugin settings. Tools. Plugin tab. clean unistall $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall perfmatters --deactivate
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.perfmatters-d-r;
    echo "perfmatters uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_perfmatters=0;
fi;
# check for perfmatters
# check for JetPack
if grep -q "jetpack" /home/"$CPUSER"/"$DOMAIN".plugin.txt; then
  CSPLUG_jetpack=1;
    wp jetpack module deactivate lazy-images 
    wp jetpack module deactivate custom-css
    wp jetpack module deactivate google-fonts
    wp jetpack module deactivate photon-cdn
    wp jetpack module deactivate photon
    wait
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.jetpack-config;
    echo "Changed JetPack settings that conflict" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  else CSPLUG_jetpack=0;
fi;
#end jetpack
#check for jetpack-boost
if grep -q "jetpack-boost" /home/"$CPUSER"/"$DOMAIN".plugin.txt; then
  CSPLUG_jetpack_boost=1;
  echo "Need to deactivate JetPack Boost plugin" 
   wp plugin uninstall jetpack-boost --deactivate 
  echo "jetpack boost  disable and deleted" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.jetpack-boost-d-r;
  else CSPLUG_jetpack_boost=0;
fi;
#end check for jetpack-boost
#begin LSCWP section
UP_PLG_litespeed_cache=0
#Check if LSCWP already istalled, if so get backup of current settings Install LSCWP.
if grep -q "litespeed-cache" /home/"$CPUSER"/"$DOMAIN".plugin.txt; then
  PLG_IA_litespeed_cache=0
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-i
  echo "litespeed-cache is already installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
  wp litespeed-option export 
  echo "The backup of the LSCWP settings was made since it was already installed."  | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
  elif  (grep -q "UA litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt") || (grep -q "UI litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt"); then
    UP_PLG_litespeed_cache=1
    wp plugin update litespeed-cache
    mv /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-i /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i
  elif (grep -q "UI litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt") || (grep -q "I litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt"); then
    wp plugin activate litespeed-cache
    mv /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i-a
  elif (grep -q "UA litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt") || (grep -q "A litespeed-cache" "/home/$CPUSER/$DOMAIN.plugin.txt"); then
    mv /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i-a
  else PLG_IA_litespeed_cache=1;
    echo "litespeed-cache needs to be installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
    wp plugin install litespeed-cache --activate
    echo "litespeed-cache is installed" |  tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-c-i-a;
fi
#import default settings
wp litespeed-option import /home/"$CPUSER"/wp-opt-lscwp.data
wait
#end install LSCWP
#set ip adress
wp litespeed-option set server_ip "$IPADDRESS"
#end ip address
clear -x;
#setting accordning to server type
echo "======Server Type======" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt; 
PS3="Choose the server type: "
sertype=("Mach 8 VPS" "Mach 16 VPS" "Mach 32 VPS" "Warp 2 Turbo AMD Dedi" "Warp 2 Turbo Intel Dedi" "Warp 3 Turbo AMD Dedi" "Shared")
select ser in "${sertype[@]}"; do
    case $ser in
        "Mach 8 VPS")
            wp litespeed-option set crawler-load_limit 1.6
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 28800
            wp litespeed-option set optm-ccss_per_url 1    
            echo "Profile upated to Mach 8 VPS" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
            STYPE=8; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-8; break;;
        "Mach 16 VPS")
            wp litespeed-option set crawler-load_limit 4.8
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 28800
            wp litespeed-option set optm-ccss_per_url 1 
            echo "Profile upated to Mach 16 VPS" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt 
            STYPE=16; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-16; break;;
        "Mach 32 VPS")
            wp litespeed-option set crawler-load_limit 6.4
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 7200
            wp litespeed-option set optm-ccss_per_url 1  
            echo "Profile upated to Mach 32 VPS" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt 
            STYPE=32; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-32; break;;
        "Warp 2 Turbo AMD Dedi")
            wp litespeed-option set crawler-load_limit 6.4
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 7200
            wp litespeed-option set optm-ccss_per_url 1  
            echo "Profile upated to Warp 2 Turbo AMD Dedi" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt 
            STYPE=1; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-1; break;;
        "Warp 2 Turbo Intel Dedi")
            wp litespeed-option set crawler-load_limit 6.4
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 7200
            wp litespeed-option set optm-ccss_per_url 1  
            echo "Profile upated to Warp 2 Turbo Intel Dedi" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt 
            STYPE=2; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-2; break;;
        "Warp 3 Turbo AMD Dedi")
            wp litespeed-option set crawler-load_limit 6.4
            wp litespeed-option set crawler-threads 3
            wp litespeed-option set purge-stale 0
            wp litespeed-option set util-instant_click 1
            wp litespeed-option set crawler-crawl_interval 7200
            wp litespeed-option set optm-ccss_per_url 1  
            echo "Profile upated to Warp 3 Turbo AMD Dedi" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt 
            STYPE=3; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-3; break;;
        "Shared")
            wp litespeed-option set object-host "/home/$CPUSER/.redis/redis.sock"
            wp litespeed-option set object-port 0
            echo "Profile upated to shared server" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt
            STYPE=0; touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.stype-0; break;;
        *) echo "invalid option $REPLY";;
    esac
done;
echo "$STYPE" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
PS3="Choose: "
#end setting accordning to server type
# check for cloudflare
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "cloudflare")" -gt 0 ]; then
PLGcloudflareDisDel=1
wp plugin uninstall cloudflare --deactivate 
touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.cloudfalre-d-r;
fi;
if ((CFgotAPI=1)); then
  wp litespeed-option set cdn-cloudflare 1;
  wp litespeed-option set cdn-cloudflare_email "$CFAPIemail";
  wp litespeed-option set cdn-cloudflare_key "$CFAPIkey";
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.lscwp-cloudfalre-config;
  echo "CF API info put into LLSCWP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
fi;
#end cloudflare section
#check for sitemap.xml.
wget https://"$DOMAIN"/sitemap.xml;
mv sitemap.xml /home/"$CPUSER"/"$DOMAIN".cp.sitemap.xml;
if grep -q ".xml" /home/"$CPUSER"/"$DOMAIN".cp.sitemap.xml; then
  wp litespeed-option set crawler-sitemap "https://$DOMAIN/sitemap.xml"
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.sitemap-i-a;
  echo "XML Site-Map activated" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
  PLG_IA_google_sitemap_generator=0;
  else 
  wp plugin install google-sitemap-generator --activate;
  echo "Sitemap generator is installed."
  wp litespeed-option set crawler-sitemap "https://$DOMAIN/sitemap.xml"
  PLG_IA_google_sitemap_generator=1;
  wp plugin install google-sitemap-generator --activate;
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.google-sitemap-generator-i-a;  
  echo "sitemap installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
fi;
#end sitemap section
#begin Builder section
# Divi theme builder
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "divi-builder")" -gt 0 ]; then
  CSPLUG_divi_builder=1;
    echo "$Red https://divicommunity.com/rooms/topic/litespeed-and-divi-cache-settings/
    With Divi enable all options at Divi -> Theme options -> General -> Performance 
    except *Defer Additional Third Party Scripts*"
    echo "https://divilife.com/how-to-clear-all-your-caches-on-your-divi-wordpress-website/
    Follow these steps to Clear the Divi Cache:
    Navigate to Divi Theme Options from the WP Admin menu
    Then go to the Builder Tab, then the Advanced sub-tab.
    Click the *Clear Cache* button next to Static CSS File Generation $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp litespeed-option set cache-priv 1 
    wp litespeed-option set optm-css_comb_ext_inl 1 
    wp litespeed-option set optm-css_async 1 
    wp litespeed-option set optm-ccss_per_url 1 
    wp litespeed-option set optm-dns_prefetch //fonts.googleapis.com
    wp litespeed-option set optm-qs_rm 0
    wp litespeed-option set db_optm-revisions_max 10 
    wp litespeed-option set db_optm-revisions_age 30 
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.divi-builder-config;  
    echo "divi configured for LSCWP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_divi_builder=0;
fi;
# elementor or elementor-pro
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "elementor")" -gt 0 ] ; then
        CSPLUG_elementor_pro=1
        else CSPLUG_elementor_pro=0
fi;

if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "elementor")" -gt 0 ] || [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "elementor-pro")" -gt 0 ] ; then
  CSPLUG_elementor=1;
    echo "$Red Follow instructions on this page:
    https://docs.google.com/document/d/1FLe_rlX1z72yu6P3VfrXR6Uh45zWPCnqIQWt54Iz9zk/edit#bookmark=id.xh4xo41czel2 $Clear";
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.elementor-config;
    echo "Elementor is configured" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
 else CSPLUG_elementor=0;
fi;
#end elementor or elementor-pro
# generatepress
if [ "$(< /home/"$CPUSER"/"$DOMAIN".theme.txt grep -c "generatepress")" -gt 0 ]; then
  CSPLUG_generatepress=1;
    echo "$Red Follow instructions on this page:
    https://docs.google.com/document/d/1FLe_rlX1z72yu6P3VfrXR6Uh45zWPCnqIQWt54Iz9zk/edit#bookmark=id.mmz6fxl4b7y9 $Clear";
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.generatepress-config;
    echo "generate press configures for LSCWP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  else CSPLUG_generatepress=0;    
fi;
#end generatepress
# oxygen-builder
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "oxygen-builder")" -gt 0 ]; then
  CSPLUG_oxygen_builder=1;
   wp litespeed-option set cache-exc_qs $"ct_builder\nct_inner" 
   wp litespeed-option set optm-qs_rm 1 
   wp litespeed-option set media-lazy_exc /oxygen/component-framework/toolbar/
   wp litespeed-option set optm-css_exc /oxygen/component-framework/toolbar/
   wp litespeed-option set optm-js_exc $"aos.js\n/oxygen/component-framework/toolbar/\njquery.js\njquery.min.js"
   wp litespeed-option set optm-js_defer_exc $"aos.js\njquery.js\njquery.min.js\ngtm.js\nanalytics.js"
   wp litespeed-option set optm-exc $"/ct_template/\n/?ct_builder=\n/?ct_template="
   touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.oxygen-builder-config;
   echo "Oxygen Builder configured for LSCWP" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt;
  else CSPLUG_oxygen_builder=0;
fi;
#end oxygen-builder
#end builder section
read -r -p "Press enter to continue";
#begin image-opt section
echo "Yes the site's image optimization is going to use the";
echo "LSCWP plugin?";
echo "-----OR------"
echo "No the site's image optimization going to use a";
echo "Different plugin ?";
select yn in "Yes" "No"; do
  case $yn in
    Yes ) IMGOPTlscwp=1; 
    echo "Image OPT will be handled by LSCWP. " | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    break;;
    No ) IMGOPTlscwp=0; 
    wp litespeed-option set img_optm-auto 0
    wp litespeed-option set img_optm-cron 0
    wp litespeed-option set img_optm-ori 0
    wp litespeed-option set img_optm-webp 0
    wp litespeed-option set ig_optm-webp_replace 0
    wp litespeed-option set img_optm-webp_replace_srcset 0
    wp litespeed-option set media-lazy 0
    wp litespeed-option set media-iframe_lazy 0
    wait
    echo "Adjusted LSCWP to not use its image optimization" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.no-lscwp-img-opt; 
    break;;
  esac
done;
# check for image plugin that need to be disabled/uninstalled so no conflicts if IMGOPTlscwp=1
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "wp-smushit")" -gt 0 ]; then
        CSPLUG_wp_smushit=1
        else CSPLUG_wp_smushit=0
fi;
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "wp-smushit")" -gt 0 ] && [ "$IMGOPTlscwp" = 1 ]; then
    echo "$Red STEP 1 https://wpmudev.com/blog/smush-compress-remove-original-images/#:~:text=Under%20Tools%20%3E%20Bulk%20restore%2C%20you,from%20your%20original%20uploaded%20images
    Check to see if the settings were set to backup the uploaded images, is so restore those images.
    If not then we will keep th optimized images as the originals and you skip step 2
    STEP 2 https://wpmudev.com/blog/smush-compress-remove-original-images/#:~:text=Under%20Tools%20%3E%20Bulk%20restore%2C%smush%20restore%20original%20images20you,from%20your%20original%20uploaded%20images.
    Under Tools > Bulk restore, you can restore your thumbnails as long as you enabled the option to back up your uploaded images.
    STEP # https://wordpress.org/support/topic/how-to-uninstall-smush/ 
    Smush > Setting son your site. Under Uninstallation choose option Delete. $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall wp-smushit --deactivate
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.wp-smushit-d-r
    echo "wp-sushit uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
fi;
# check for imagify
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "imagify")" -gt 0 ] && [ "$IMGOPTlscwp" = 1 ]; then
    CSPLUG_imagify=1
    echo "$Red https://imagify.io/documentation/restore-images-original/
    Follow these unstruction to restore the orinal images  ===must be done====$Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall imagify --deactivate
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.imagify-d-r
    echo "imagify uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_imagify=0; 
fi;
# check for ewww-image-optimize
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "ewww-image-optimizer")" -gt 0 ] && [ "$IMGOPTlscwp" = 1 ]; then
    CSPLUG_ewww_image_optimizer=1;
    echo "https://docs.ewww.io/article/83-uninstalling-ewww-image-optimizer
    $Red STEP 1 f you haveve been using EWWW IO for a very long time, you might 
    also find data stored in the attachment metadata. You can go to 
    Tools->EWWW Image Optimizer->Migrate Optimization Records to move 
    this data to the ewwwio_images table.    
    STEP 2 Remove tables: wp_ewwwio_images and 
    wp_ewwwio_queue are where most of the data is stored. 
    You can even empty the tables from Tools->EWWW Image Optimizer 
    with the Erase Optimization History and Clear Queue buttons. $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall ewww-image-optimizer --deactivate
    echo "$Red STEP 3 Remove options: All of the options 
    are prefixed with ewww_image_optimizer_ 
    or exactdn_ (Easy IO options) $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.ewww-images-d-r
    echo "eww image uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
    else CSPLUG_ewww_image_optimizer=0;
fi;
# check for shortpixel-image-optimiser
if [ "$(< /home/"$CPUSER"/"$DOMAIN".plugin.txt grep -c "shortpixel-image-optimiser")" -gt 0 ] && [ "$IMGOPTlscwp" = 1 ]; then
    CSPLUG_shortpixel_image_optimiser=1;
    echo "$Red https://shortpixel.com/knowledge-base/article/81-remove-all-the-shortpixel-related-data-on-a-wp-website
    follow the instructions on this page:
        STEP 1  restore your original images, go to Settings > ShortPixel > Tools, click on *Bulk Restore*
        and follow the instructions.
        STEP 2 SKIP THIS
        STEP 3 Go to Settings > ShortPixel > Tools, click on
        Remove all ShortPixel Data and follow the instructions
        STEP 4 Remove the shortpixel_meta and shortpixel_folders tables 
        from the database (if found). $Clear"
    read -r -p "ONCE THAT IS DONE Press enter to continue";
    wp plugin uninstall shortpixel-image-optimiser --deactivate 
    test -d "$HOMEDIR"/wp-content/uploads/ShortpixelBackups && rm -r "$HOMEDIR"/wp-content/uploads/ShortpixelBackups
    touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.shortpixel-image-optimiser-d-r
    echo "shortpixel uninstalled" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt;
    else CSPLUG_shortpixel_image_optimiser=0;
fi;
#end image-opt section
#begin A2 OPT
if grep -q "a2-optimized-wp" "/home/$CPUSER/$DOMAIN.plugin.txt"; then
  PLG_IA_a2_optimized_wp=0;
  echo "a2-optimized-wp is already installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
  else PLG_IA_a2_optimized_wp=1;
  echo "a2-optimized-wp needs to be installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt;
fi;
if (grep -q "UA a2-optimized-wp" "/home/$CPUSER/$DOMAIN.plugin.txt") || (grep -q "UI a2-optimized-wp" "/home/$CPUSER/$DOMAIN.plugin.txt"); then 
    UP_PLG_a2_optimized_wp=1;
    else UP_PLG_a2_optimized_wp=0;
fi;
if [ "$PLG_IA_a2_optimized_wp" = 1 ]; then
  wp plugin install a2-optimized-wp --activate
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.a2-optimized-wp-c-i-a;
  echo "a2 optimized is installed" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt; 
fi;
if [ "$UP_PLG_a2_optimized_wp" = 1 ]; then
  wp plugin update a2-optimized-wp;
  touch /home/"$CPUSER"/.wp-opt/"$DOMAIN"/.a2-optimized-wp-c-i-a;
  echo "a2 optimized is updated" | tee -a /home/"$CPUSER"/"$DOMAIN".cp.log.txt /home/"$CPUSER"/"$DOMAIN".cp.cx.log.txt;
fi;
#end A2 OPT
clear -x
read -r -p "Press enter to TURN ON THE MAGIC";
#turn on the magic
wp litespeed-purge all
wait
echo "======The Cache has been purged======";
wp litespeed-online init
wait
wp litespeed-image push;
wait
echo "===The image optimization has been primed, if you see an error,";
echo "it just means the WP Heartbeat did it first===";
echo "======The Cache has been purged======";
wp litespeed-option export
wait
clear -x
read -r -p "Press enter to let ET phone home!";
#prepare pass back variables
touch /home/"$CPUSER"/way-pass-back-variables.txt
{
echo CSPLUG_autoptimize="$CSPLUG_autoptimize"
echo CSPLUG_comet_cache="$CSPLUG_comet_cache"
echo CSPLUG_divi_builder="$CSPLUG_divi_builder"
echo CSPLUG_elementor="$CSPLUG_elementor"
echo CSPLUG_elementor_pro="$CSPLUG_elementor_pro"
echo CSPLUG_ewww_image_optimizer="$CSPLUG_ewww_image_optimizer"
echo CSPLUG_generatepress="$CSPLUG_generatepress"
echo CSPLUG_hummingbird_performance="$CSPLUG_hummingbird_performance"
echo CSPLUG_imagify="$CSPLUG_imagify"
echo CSPLUG_jetpack_boost="$CSPLUG_jetpack_boost"
echo CSPLUG_jetpack="$CSPLUG_jetpack"
echo CSPLUG_oxygen_builder="$CSPLUG_oxygen_builder"
echo CSPLUG_perfmatters="$CSPLUG_perfmatters"
echo CSPLUG_shortpixel_image_optimiser="$CSPLUG_shortpixel_image_optimiser"
echo CSPLUG_wp_fastest_cache="$CSPLUG_wp_fastest_cache"
echo CSPLUG_w3_total_cache="$CSPLUG_w3_total_cache"
echo CSPLUG_wp_optimize="$CSPLUG_wp_optimize"
echo CSPLUG_wp_rocket="$CSPLUG_wp_rocket"
echo CSPLUG_wp_smushit="$CSPLUG_wp_smushit"
echo CSPLUG_wp_super_cache="$CSPLUG_wp_super_cache"
echo IMGOPTlscwp="$IMGOPTlscwp"
echo PLG_IA_a2_optimized_wp="$PLG_IA_a2_optimized_wp"
echo PLG_IA_google_sitemap_generator="$PLG_IA_google_sitemap_generator"
echo PLG_IA_litespeed_cache="$PLG_IA_litespeed_cache"
echo PLGcloudflareDisDel="$PLGcloudflareDisDel"
echo RQBKUP="$RQBKUP"
echo UP_PLG_a2_optimized_wp="$UP_PLG_a2_optimized_wp"
echo UP_PLG_litespeed_cache="$UP_PLG_litespeed_cache"
echo wpconfigCHANGE="$wpconfigCHANGE"
echo WHYNOBKUP="$WHYNOBKUP"
echo wphome="$wphome"
echo wpsite="$wpsite"
echo STYPE="$STYPE"
} >> /home/"$CPUSER"/way-pass-back-variables.txt
clear -x
read -r -p "Press enter to go back to /home/$agent/";
exit
