#!/bin/bash
if [[ "$EUID" -ne 0 ]]; then
	echo "Sorry, you need to run this as root"
	exit
fi



while [[ -z $action ]]; do
    clear
    echo "What do you want to do?"
    echo "   1) Install Redis"
    echo "   2) Enable Redis for a site"
    echo "   3) Disable Redis for a site"
    echo "   4) Clear Redis cache"
    echo "   5) Restart Redis"
    echo "   6) Remove Redis"
    echo
    read -p "Action: " action
    until [[ -z "$action" || "$action" =~ ^[1-6]$ ]]; do
    	echo "$action: invalid selection."
    	read -p "Action: " action
    done
done

if [[ $action == "install_redis" || $action == "1" ]]; then

    if hash redis-cli 2>/dev/null; then
        echo
        echo "Redis is already installed!"
        exit
    fi

    apt-get install redis-server php8.1-redis php8.0-redis php7.4-redis php7.3-redis php7.2-redis php7.1-redis php5.6-redis -y
    sed -i "s/supervised no/supervised systemd/g" /etc/redis/redis.conf
    systemctl restart redis

    echo
    echo "Redis has been installed. You can run this script again to enable it for a site."
    exit
fi


if [[ $action == "enable" || $action == "2" ]]; then

    while [[ -z $domain ]]; do
        echo
        echo "Please, select which site you want to work with"
        echo
        ls /var/www/ | grep -v html | nl
        read -p "Select site: " site_number
        number_of_sites=$(ls /var/www/ | grep -v html | wc -l)
        until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
	    	echo "$site_number: invalid selection."
	    	read -p "Select site: " site_number
	    done
        domain=$(ls /var/www/ | grep -v html | sed -n "$site_number"p)
    done
    user_name=$(echo $domain | cut -c1-32)

    if [[ -e /var/www/$domain/html/wp-content/object-cache.php ]]; then
        echo "An object cache is already enabled for $domain"
        echo
        echo "If you want to enable Redis, the currently enabled cache system needs to be disabled first."
        exit
    fi

    cd /var/www/$domain/html/
    su - $user_name -c "wp plugin install --activate redis-cache"
    su - $user_name -c "cd /var/www/$domain/html/wp-content/; ln -s ./plugins/redis-cache/includes/object-cache.php ./object-cache.php"

    echo
    echo "Redis has been enabled for $domain"
    exit
fi


if [[ $action == "disable" || $action == "3" ]]; then

    while [[ -z $domain ]]; do
        echo
        echo "Please, select which site you want to work with"
        echo
        ls /var/www/ | grep -v html | nl
        read -p "Select site: " site_number
        number_of_sites=$(ls /var/www/ | grep -v html | wc -l)
        until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
	    	echo "$site_number: invalid selection."
	    	read -p "Select site: " site_number
	    done
        domain=$(ls /var/www/ | grep -v html | sed -n "$site_number"p)
    done
    user_name=$(echo $domain | cut -c1-32)

    cd /var/www/$domain/html/
    rm -f wp-content/object-cache.php
    su - $user_name -c "wp plugin uninstall --deactivate redis-cache"

    echo
    echo "Redis has been disabled for $domain"
    exit
fi


if [[ $action == "redis_clear" || $action == "4" ]]; then
    redis-cli FLUSHALL
    echo
    echo "Redis cache has been cleared"
    exit
fi


if [[ $action == "redis_restart" || $action == "5" ]]; then
    systemctl restart redis.service
    echo
    echo "Redis server has been restarted"
    exit
fi


if [[ $action == "remove_redis" || $action == "6" ]]; then

    apt-get remove redis-server php8.1-redis php8.0-redis php7.4-redis php7.3-redis php7.2-redis php7.1-redis php5.6-redis redis-tools -y

    echo
    echo "Redis has been removed from the system."
    exit
fi
