#!/bin/sh

# /********************************************
# LiteSpeed Cache Management Script
# @Author:   LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
# @Copyright: (c) 2017-2018
# *********************************************/

VERSION='1.2.2'
CURR_DIR=`pwd`
FILE_DIR=`dirname $0`
LSWS_DIR="${FILE_DIR}/../.."
SCRIPT=${LSWS_DIR}/add-ons/webcachemgr/bootstrap_cli.php

YELLOW='\e[33m'
CYAN='\e[36m'
GREEN='\e[32m'
L_GRAY='\e[37m'
END_COLOR='\e[0m'

printHeader()
{
    # $1 title , $2 descr
	printf "\n${YELLOW}$1${END_COLOR}\n$2\n"
}

printLabelDesc()
{
	printf "\n${YELLOW}$1${END_COLOR} $2\n\n" 
}

printCmdOption()
{
    printf "${CYAN}$1${END_COLOR} ${GREEN}$2${END_COLOR}\n" 
    printf "    $3\n\n" | fold -sw 80 
}

printExample()
{
	printf "  ${L_GRAY}$1:${END_COLOR}\n"  
	printf "  ./lscmctl $2\n\n"
}

printHelp() 
{
	printHeader "LiteSpeed Cache Manager CLI Tool v${VERSION}"
	printLabelDesc 'Usage:' './lscmctl [-php path/to/php] command [flag/parameter]' 
	printLabelDesc 'Possible Commands:' 
    printCmdOption 'setversion' '[--list] | [--latest] | [$VERSION]' 'List/Set active LSCWP version. This command will list the currently active version when no additional input it given. Use --list to show available versions or --latest to switch to the latest available version. A valid version number can also be provided to switch to that version specifically. This must be set before performing other lscmctl operations.'
	printCmdOption 'scan' '[-n] [-e]' 'Scan for all WordPress installations. This command will create an lscm.data file under the "lsws/admin/lscdata" directory. Add the -n flag to only discover new installations. By adding the -e flag, LSC-WP will be enabled on all installations after scanning is complete.'
	printCmdOption 'enable' '-m | $WP_PATH' 'Enables LSWCP for all discovered WordPress installations with the -m parameter or a single installtion by providing the path to the WordPress installation directory.'
	printCmdOption 'disable' '-m | $WP_PATH' 'Disables LSWCP for all discovered WordPress installations with the -m parameter or a single installtion by providing the path to the WordPress installation directory.'
    printCmdOption 'upgrade' '-m | $WP_PATH' 'Upgrade LSWCP for all discovered WordPress installations to the current active version with the -m parameter or a single installtion by providing the path to the WordPress installation directory.'
    printCmdOption 'flag' '$WP_PATH' 'Flag a single WordPress installation. Flagged installations will be skipped during mass operations.'
    printCmdOption 'unflag' '$WP_PATH' 'Unflag a single WordPress installation. Flagged installations will be skipped during mass operations.'
    printCmdOption 'status' '$WP_PATH' 'Get the most up to date LSC-WP status for the provided WordPress installation.'
	printHeader "Example Usage:"  
    printExample 'Display currently active LSCWP version' 'setversion'
    printExample 'Display selectable LSCWP versions' 'setversion --list'
    printExample 'Set active LSCWP version to latest available' 'setversion --latest'
    printExample 'Set active LSCWP version to v1.5' 'setversion 1.5'
	printExample 'Discover all installations' 'scan'
    printExample 'Discover new installations only, passing in path to php binary' '-php /path/to/php/ scan -n'
	printExample 'Enable LSC-WP on all discovered installations' 'enable -m'
	printExample 'Disable LSC-WP for a single installation' 'disable /home/user/public_html/wp'
    printExample 'Get LSC-WP status for a single installation' 'status /home/user/public_html/wp'
}


errorExit()
{
    printf "${YELLOW}ERROR: ${END_COLOR} $1\n\n"
    exit 1
}

setPanel()
{
    PANEL=false

    x=$(/usr/local/cpanel/cpanel -V 2>/dev/null)

    if [ "$x" ]; then
        PANEL='cpanel'
        return
    fi

    x=$(plesk version 2>/dev/null)

    if [ "$x" ]; then
        PANEL='plesk'
        return
    fi
}

selectPHP() 
{
    if [ "$1" == "-php" ]
    then
        if [ -x "$2" ] ; then
            PHP="$2 -d disable_functions="
            return 2
        else
            errorExit "$2 not detected as a valid php binary."
        fi
    else
        #Use default admin php binary
        PHP="${LSWS_DIR}/admin/fcgi-bin/admin_php5 -c ${LSWS_DIR}/add-ons/webcachemgr/shared/webcachemgr.ini"
        #PHP='/usr/bin/env php'
    fi
}

if [ "$1" == "--help" ]
then
    printHelp
    exit 0
fi

setPanel

if [ "$PANEL" == false ] ; then
    errorExit 'Supported Control Panel not detected. Aborting!'
fi    

selectPHP $1 $2

if [ $? == 2 ] ; then
    shift 2
fi

if [ $# == 0 ] ; then
    errorExit "Missing input command. Try --help for a full list of available commands with examples."
fi

$PHP $SCRIPT $PANEL "$@"

