Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/bin/
Upload File :
Current File : //opt/nginxhttpd_/bin/install

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

###
## This script will install openresty on a shared hosting
## It will use the openresty archive created by the build_openresty script
## It will also install a private redis listening on 1025 with a password authentification
## It will register the OpenResty + Private redis service and add them to cPanel chkserv
## It will disable the normal Nginx too
## It will compile a new normal Nginx too
###

###
## Variables + config /!\ Do not touch, use command arguments instead /!\
###

# Working TMP dir
TMP_WORKING_DIR=/root/tmp_install_dir
# Directory used to store the old openresty + old config (if we need to roll back). Also works for vanilla Nginx
TMP_ROLLBACK=/root/_rollback
# Unique suffixe used for the backup/rollback of things
TMP_ROLLBACK_UNIQUE_SUFFIX=$(date '+%d-%m-%y_%s')

CURRENT_DATE=$(date +'%d-%m-%Y')
ACTIVATE=none
DIR_SETTED_UP=0
VHOST_GENERATOR_DIR=/opt/nginxhttpd

# Path to commands we use
SYSTEMCTL=/usr/bin/systemctl
CHKCONFIG=/sbin/chkconfig
GREP=/usr/bin/grep
PHP=/usr/local/cpanel/3rdparty/bin/php
LDD=/usr/bin/ldd
READLINK=/usr/bin/readlink
SED=/usr/bin/sed
PKILL=/usr/bin/pkill

# Redis stuff & path
REDIS_SRV_BIN=/usr/bin/redis-server
PRIVATE_REDIS_CONF_PATH=/etc/o2redis.conf
PRIVATE_REDIS_SYSTEMCTL_PATH=/usr/lib/systemd/system/o2redis.service
PRIVATE_REDIS_INITD_PATH=/etc/init.d/o2redis
PRIVATE_REDIS_FORCE=1

# Note: dont change the paths. Some of them are hardcoded in init.d/systemctl + hardcoded in the build process too /!\
# Openresty stuff
OPENRESTY_DIR=/usr/local/openresty
OPENRESTY_CONF_DIR=/etc/nginx/openresty
OPENRESTY_SYSTEMCTL_PATH=/usr/lib/systemd/system/openresty.service
OPENRESTY_INIT_PATH=/etc/init.d/openresty
OPENRESTY_OVERRIDE_CONF=0
OPENRESTY_ARCHIVE=0
OPENRESTY_ALREADY_INSTALLED=0

# Colors
RESTORE='\033[0m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'

###
## Functions
###

# $1 : string to display
function _eg(){
	echo -e "${GREEN}$1${RESTORE}"
	return 0
}

# $1 : string to display
function _ey(){
	echo -e "${YELLOW}$1${RESTORE}"
	return 0
}

# $1 : string to display
function _er(){
	echo -e "${RED}$1${RESTORE}"
	return 0
}

function usage(){
    echo -e "Usage: $0 [-o o2-openresty-xx-xx-xx.tar.gz] [-a openresty] [-f openresty|none]" >&2
    help
    exit 1
}

# Centos 6 compat, some path are differents
function checkCommands(){
	test -e $GREP || GREP=/bin/grep
	test -e $SED || SED=/bin/sed
	test -e $PHP || exitError "$PHP is missing"
	test -e $GREP || exitError "Can't find GREP : $GREP"
	test -e $SED || exitError "Can't find SED : $SED"
	test -e $PKILL || exitError "Can't find PKILL : $PKILL"
}

function help(){
    echo -e ""
    echo -e "-o /path/to/o2-openresty-xx-xx-xx.tar.gz : Tell the script to re-install (or update) the Openresty binary."
    echo -e "-a openresty|ignore : activate Openresty  or Ignore (just keep it like it is)"
    echo -e "-f openresty|none : override the openresty or none of the configuration"
    echo -e ""
    echo -e "The openresty archive are generated with others scripts : build_openresty"
    echo -e "Installation of openresty or nginx with -a or -n does not mean that they are activated by default ! See -a"
    echo -e ""
}

# $1 : Error message
function exitError(){
    _er "[Error] $1" >&2
    exit 1
}

# $1 : Error message
function exitErrorUsage(){
    _er "[Error] $1" >&2
    echo -e ""
    usage
}


# $1 : Message
function info(){
    echo -e "${CYAN}[Info] $1${RESTORE}"
}

# Setup the working directory for this installation, redis old dir if they exists
function setupWorkingDirs(){
    if test $DIR_SETTED_UP = 0 ; then
        test -d $TMP_WORKING_DIR && rm -vfr $TMP_WORKING_DIR
        mkdir $TMP_WORKING_DIR
        test -d $TMP_ROLLBACK || mkdir $TMP_ROLLBACK
        DIR_SETTED_UP=1
    fi
    return 0
}

function privateRedisForce(){
    if test $PRIVATE_REDIS_FORCE -eq 1 ; then
        _eg "Force private redis set to true, overriding the conf ..."
        test -e $PRIVATE_REDIS_CONF_PATH && rm -vf $PRIVATE_REDIS_CONF_PATH
    fi
}

# Make sure the redis server is installed and enabled
function checkAndInstallRedis(){
    test -d /var/run/redis/ || mkdir -p /var/run/redis/
    chown -R redis: /var/run/redis/
    test -d /var/log/redis || mkdir -p /var/log/redis
    chown -R redis: /var/log/redis
    test -d /opt/nginxhttpd/redisbkp || mkdir -p /opt/nginxhttpd/redisbkp
    chown -R redis: /opt/nginxhttpd/redisbkp

    if ! test -e $REDIS_SRV_BIN ; then
        yum info redis || yum install -y epel-release
        yum install -y redis
    fi
    # Register the service
    if test -e $SYSTEMCTL ; then
        info "Enabling the systemctl service for redis (Centos 7)"
        $SYSTEMCTL enable redis
        $SYSTEMCTL start redis
    else
        info "Enabling the chkconfig service for redis (Centos 6)"
        $CHKCONFIG --add redis
        /etc/init.d/redis start
    fi
}

# Check if the private, secondary, redis is deployed. We'll use this one, not the public one
function checkAndInstallPrivateRedis(){
    test -d /var/run/redis/ || mkdir -p /var/run/redis/
    chown -R redis: /var/run/redis/
    test -d /var/log/redis || mkdir -p /var/log/redis
    chown -R redis: /var/log/redis
    test -d /opt/nginxhttpd/redisbkp || mkdir -p /opt/nginxhttpd/redisbkp
    chown -R redis: /opt/nginxhttpd/redisbkp

    if test -e $PRIVATE_REDIS_CONF_PATH ; then
        info "Private o2switch redis is already installed"
        info "Making sure the private o2switch redis is enabled"
        if test -e $SYSTEMCTL ; then
            info "Enabling the systemctl service for the private redis (Centos 7)"
            test -e $PRIVATE_REDIS_SYSTEMCTL_PATH || cp -vaR $VHOST_GENERATOR_DIR/etc/o2redis.service $PRIVATE_REDIS_SYSTEMCTL_PATH
            $SYSTEMCTL enable o2redis
            $SYSTEMCTL start o2redis
            $SYSTEMCTL status o2redis
        else
            info "Enabling the chkconfig service for the private redis (Centos 6)"
            test -e $PRIVATE_REDIS_INITD_PATH || cp -vaR $VHOST_GENERATOR_DIR/etc/o2redis $PRIVATE_REDIS_INITD_PATH
            chmod 755 $PRIVATE_REDIS_INITD_PATH
            $PRIVATE_REDIS_INITD_PATH start
            $CHKCONFIG --add o2redis
            $PRIVATE_REDIS_INITD_PATH status
        fi
    else
        info "Private o2switch redis not installed, installing it ..."
        cp -vaR $VHOST_GENERATOR_DIR/etc/o2redis.conf $PRIVATE_REDIS_CONF_PATH
        if test -e $SYSTEMCTL ; then
            info "Creating the systemctl conf for Redis and enabling it (Centos 7)"
            ! test -e $PRIVATE_REDIS_SYSTEMCTL_PATH && cp -vaR $VHOST_GENERATOR_DIR/etc/o2redis.service $PRIVATE_REDIS_SYSTEMCTL_PATH
            $SYSTEMCTL daemon-reload
            $SYSTEMCTL enable o2redis
            $SYSTEMCTL start o2redis
            $SYSTEMCTL status o2redis
        else
            info "Creating the init.d conf for Redis and enabling it (Centos 6)"
            ! test -e $PRIVATE_REDIS_INITD_PATH && cp -vaR $VHOST_GENERATOR_DIR/etc/o2redis $PRIVATE_REDIS_INITD_PATH
            chmod 755 $PRIVATE_REDIS_INITD_PATH
            $CHKCONFIG --add o2redis
            $PRIVATE_REDIS_INITD_PATH start
            $PRIVATE_REDIS_INITD_PATH status
        fi
    fi
    return 0
}

# $1 : Binary to check
# Check that all the libs required for the binary are installed
function sanityCheckLibs(){
    test -e /lib64/libpcre.so.0 || ln -s /usr/lib64/libpcre.so.1 /lib64/libpcre.so.0
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH+x}:$TMP_WORKING_DIR/openresty/usr/local/openresty/luajit/lib:$TMP_WORKING_DIR/openresty/usr/local/openresty/lualib:$TMP_WORKING_DIR/openresty/usr/local/openresty/openssl/lib"
    $LDD $1 | $GREP -qi 'not found'  && exitError "Libs are missing, please check with : ldd $1"
    return 0
}

# $1 : Binary to check
# Check that the Nginx/Openresty binary can be executed and display at least the version
function sanityCheckVersion(){
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH+x}:$TMP_WORKING_DIR/openresty/usr/local/openresty/luajit/lib:$TMP_WORKING_DIR/openresty/usr/local/openresty/lualib:$TMP_WORKING_DIR/openresty/usr/local/openresty/openssl/lib"
    chmod 755 $1
    $1 -V || exitError "The $1 binary can't be executed properly"
    return 0
}

# $1 : o2-openresty-xx-xx-xx.tar.gz path
function extractOpenrestyArchive(){
    cp -vaR $1 $TMP_WORKING_DIR
    cd $TMP_WORKING_DIR
    info "Extracting $1 to $TMP_WORKING_DIR/openresty"
    tar -xvf $TMP_WORKING_DIR/o2-openresty*.tar.gz
    rm -vf $TMP_WORKING_DIR/o2-openresty*.tar.gz
    mv o2-openresty* openresty
}

# Check if OpenResty is already installed
function checkOldOpenresty(){
    if test -d $OPENRESTY_DIR ; then
        OPENRESTY_ALREADY_INSTALLED=1
    fi
}

function backupCurrentOpenrestyAndConf(){
    test -d $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX || mkdir -p $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX
    test -d $OPENRESTY_DIR && cp -vaR $OPENRESTY_DIR $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX/openresty_dir
    test -d $OPENRESTY_CONF_DIR && cp -vaR $OPENRESTY_CONF_DIR $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX/openresty_conf
    test -f $OPENRESTY_INIT_PATH && cp -vaR $OPENRESTY_INIT_PATH $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX/openresty_initd
    test -f $OPENRESTY_SYSTEMCTL_PATH && cp -vaR $OPENRESTY_SYSTEMCTL_PATH $TMP_ROLLBACK/openresty-$TMP_ROLLBACK_UNIQUE_SUFFIX/openresty_systemd
    return 0
}

# Remove the current Openresty version
function removeCurrentOpenresty(){
    $PKILL -9 nginx || echo "No nginx processes"
    # We dont touch the openresty conf (yet, maybe later)
    test -d $OPENRESTY_DIR && rm -vfr $OPENRESTY_DIR
    # Remove the symlink we created
    test -e /usr/bin/opm && rm -vf /usr/bin/opm
    test -e /usr/bin/resty && rm -vf /usr/bin/resty
    test -e /usr/bin/restydoc && rm -vf /usr/bin/restydoc
    test -e /usr/bin/restydoc-index && rm -vf /usr/bin/restydoc-index
    test -e $OPENRESTY_SYSTEMCTL_PATH &&  rm -vfr $OPENRESTY_SYSTEMCTL_PATH
    test -e $OPENRESTY_INIT_PATH && rm -vfr $OPENRESTY_INIT_PATH
}

# Copy the compiled openresty file to the right directories
function installOpenrestyFiles(){
    cd $TMP_WORKING_DIR/openresty
    # Don't override the config in place
    test -e /lib64/libpcre.so.0 || link /usr/lib64/libpcre.so.1 /lib64/libpcre.so.0
    ! test -d $OPENRESTY_CONF_DIR && cp -vaR $TMP_WORKING_DIR/openresty/etc/nginx/openresty $OPENRESTY_CONF_DIR
    # Make sure we ovevride this directory !
    test -d $OPENRESTY_DIR && rm -vfr $OPENRESTY_DIR
    cp -vaR $TMP_WORKING_DIR/openresty/usr/local/openresty $OPENRESTY_DIR
    # Copy the symlink
    cp -vaR $TMP_WORKING_DIR/openresty/usr/bin/* /usr/bin/
    test -L $OPENRESTY_DIR/conf && rm -vf $OPENRESTY_DIR/conf
    # Those two symlink are necessary, otherwise Nginx will not be able to load .lua, it won't find the conf
    ln -s $OPENRESTY_CONF_DIR $OPENRESTY_DIR/conf
    test -L $OPENRESTY_DIR/nginx/conf && rm -vf $OPENRESTY_DIR/nginx/conf
    ln -s $OPENRESTY_CONF_DIR $OPENRESTY_DIR/nginx/conf
    test -d $OPENRESTY_CONF_DIR && chmod 755 $OPENRESTY_CONF_DIR
    test -f $OPENRESTY_DIR/nginx/sbin/nginx && chmod 755 $OPENRESTY_DIR/nginx/sbin/nginx
    test -d /var/log/openresty || mkdir -p /var/log/openresty
    test -d /var/log/oblig || mkdir -p /var/log/oblig
    test -d /eacc/openresty_client || mkdir -p /eacc/openresty_client
    test -d /eacc/openresty_proxy || mkdir -p /eacc/openresty_proxy
    test -d /eacc/openresty_fastcgi || mkdir -p /eacc/openresty_fastcgi
    test -d /eacc/openresty || mkdir -p /eacc/openresty
    chown -R nobody: /eacc

    # Fix symlink
    test -L /usr/local/openresty/bin/openresty && rm -vf /usr/local/openresty/bin/openresty
    ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

    if test -e $SYSTEMCTL; then
        info "Deploying Openresty systemctl file to $OPENRESTY_SYSTEMCTL_PATH"
        test -e $OPENRESTY_SYSTEMCTL_PATH || cp -vaR $VHOST_GENERATOR_DIR/etc/openresty.service $OPENRESTY_SYSTEMCTL_PATH
        $SYSTEMCTL daemon-reload
    else
        info "Deploying Nginx init.d file to $OPENRESTY_INIT_PATH"
        test -e $OPENRESTY_INIT_PATH || cp -vaR $VHOST_GENERATOR_DIR/etc/openresty $OPENRESTY_INIT_PATH
        chmod 755 $OPENRESTY_INIT_PATH
    fi
}

# Reinstall the OPM Dependecies we have
function installOpmDependencies(){
    # Avoid an error with openssl lib
    export LD_LIBRARY_PATH="" 
    $OPENRESTY_DIR/bin/opm get thibaultcha/lua-resty-mlcache
    $OPENRESTY_DIR/bin/opm get xiangnanscu/lua-resty-cookie
    $OPENRESTY_DIR/bin/opm get fffonion/lua-resty-openssl 
    $OPENRESTY_DIR/bin/opm get ledgetech/lua-resty-http
    $OPENRESTY_DIR/bin/opm get openresty/lua-resty-string
    $OPENRESTY_DIR/bin/opm update
}

# Override the Openresty configuration and install the one we have. Can also be used to install the openresty conf for the first time.
function overrideOpenrestyConf(){
    TMP_DIR_CONF=$(mktemp -p /root -d)
    chmod 755 $TMP_DIR_CONF
    info "Creating a new Openresty config directory inside $TMP_DIR_CONF"
    cp -vaR $VHOST_GENERATOR_DIR/etc/openresty_config/* $TMP_DIR_CONF
    test -e $VHOST_GENERATOR_DIR/etc/openresty_config/.htpasswd && cp -vaR $VHOST_GENERATOR_DIR/etc/openresty_config/.htpasswd $TMP_DIR_CONF
    test -e $VHOST_GENERATOR_DIR/etc/openresty_config/.git && cp -vaR $VHOST_GENERATOR_DIR/etc/openresty_config/.git $TMP_DIR_CONF

    test -e $TMP_DIR_CONF/nginx.conf || exitError "Something is wrong with the openresty configuration, $TMP_DIR_CONF/nginx.conf does not exists. Aborted."
    test -e $TMP_DIR_CONF/lua/lib/o2switch_config.lua || exitError "Something is wrong with the openresty configuration, $TMP_DIR_CONF/lua/lib/o2switch_config.lua does not exists. Aborted."

    test -d $OPENRESTY_CONF_DIR && rm -vfr $OPENRESTY_CONF_DIR
    info "Moving $TMP_DIR_CONF to $OPENRESTY_CONF_DIR"
    mv -v $TMP_DIR_CONF $OPENRESTY_CONF_DIR
    chmod 755 $OPENRESTY_CONF_DIR
    info "Populating the Openresty Redis DB"
    # $PHP $VHOST_GENERATOR_DIR/bin/console openresty:generate -v || echo ""
}

function activateOpenresty(){
    if test -e $SYSTEMCTL ; then
        info "Starting private redis (systemctl)"
        $SYSTEMCTL start o2redis
        info "Enabling private redis (systemctl)"
        $SYSTEMCTL enable o2redis
        info "Starting Openresty (systemctl)"
        $SYSTEMCTL start openresty
        sleep 1
        info "Enabling Openresty (systemctl)"
        $SYSTEMCTL enable openresty
    else
        info "Starting private redis (init.d)"
        $PRIVATE_REDIS_INITD_PATH start || $PRIVATE_REDIS_INITD_PATH start
        info "Enabling private redis (chkconfig)"
        $CHKCONFIG --add o2redis
        info "Starting Openresty (init.d)"
        $OPENRESTY_INIT_PATH start
        sleep 1
        $OPENRESTY_INIT_PATH status || $OPENRESTY_INIT_PATH start
        sleep 1
        info "Enabling Openresty (chkconfig)"
        $CHKCONFIG --add openresty
    fi
}

# $1 : name of the service to check
function status(){
    if test -e $SYSTEMCTL ; then
        #if $SYSTEMCTL list-units --full -all|$GREP -q "$1" ; then
            $SYSTEMCTL status $1
        #else
        #    _ey "$1 is not declared a systemd service. Please check with : $SYSTEMCTL list-units --full -all|$GREP -F $1.service"
        #fi
    else
        if test -x "/etc/init.d/$1" ; then
            /etc/init.d/$1 status
        else
            _ey "/etc/init.d/$1 doesn't exists or is not executable"
        fi
    fi
}

function installMonit(){
    test -d /etc/monit.d/ || return 
    test -e /etc/monit.d/o2redis && rm -vf /etc/monit.d/o2redis
    test -e /etc/monit.d/openresty && rm -vf /etc/monit.d/openresty
    cp -vaR $VHOST_GENERATOR_DIR/etc/monit.d/o2redis /etc/monit.d/o2redis
    cp -vaR $VHOST_GENERATOR_DIR/etc/monit.d/openresty /etc/monit.d/openresty
    systemctl restart monit
}

function installChkservdFiles(){
    info "Installating custom chkserd service"
    test -e /etc/chkserv.d/o2redis && rm -vf /etc/chkserv.d/o2redis
    test -e /etc/chkserv.d/openresty && rm -vf /etc/chkserv.d/openresty
    cp -va $VHOST_GENERATOR_DIR/etc/chkserv.d/o2redis /etc/chkserv.d/o2redis
    cp -va $VHOST_GENERATOR_DIR/etc/chkserv.d/openresty /etc/chkserv.d/openresty
}

# $1 : service we want to add on the monitor. Will also disabled the opposing services if needed
function activateChkservdMonitor(){
    info "Activate the $1 service inside cPanel chkservd"
    if test "$1" == "openresty" ; then
        $SED -i '/openresty:/d' /etc/chkserv.d/chkservd.conf
        $SED -i '/nginx:/d' /etc/chkserv.d/chkservd.conf
        $SED -i '/o2redis:/d' /etc/chkserv.d/chkservd.conf
        echo "o2redis:1" >> /etc/chkserv.d/chkservd.conf
        echo "openresty:1" >> /etc/chkserv.d/chkservd.conf
    elif test "$1" == "o2redis" ; then
        $SED -i '/o2redis:/d' /etc/chkserv.d/chkservd.conf
        echo "openresty:1" >> /etc/chkserv.d/chkservd.conf
    fi
    /scripts/restartsrv_chkservd
}

function installCron(){
    # Remove old cron
    $SED -i '/\/opt\/nginxhttpd\/bin\/cron/d' /var/spool/cron/root
    test -e /scripts-alex/misc.sh && $SED -i '/\/opt\/nginxhttpd\/bin\/cron/d' /scripts-alex/misc.sh
    test -e /scripts-alex/misc.sh && echo 'bash /opt/nginxhttpd/bin/cron' >> /scripts-alex/misc.sh
    test -e /scripts-alex/misc-25m.sh && $SED -i '/\/opt\/nginxhttpd\/bin\/cron/d' /scripts-alex/misc-25m.sh
    test -e /scripts-alex/misc-25m.sh  && echo 'bash /opt/nginxhttpd/bin/cron force' >> /scripts-alex/misc-25m.sh
    test -e /scripts-alex/misc-25m.sh && $SED -i '/\/etc\/nginx\/openresty\/utils\/openresty_cron.sh/d' /scripts-alex/misc-25m.sh
    test -e /scripts-alex/misc-25m.sh  && echo 'bash /etc/nginx/openresty/utils/openresty_cron.sh' >> /scripts-alex/misc-25m.sh
}

function cleanUpVanillaNginx(){
    $SED -i '/nginx:/d' /etc/chkserv.d/chkservd.conf
    test -e /usr/local/sbin/nginx && rm -vf /usr/local/sbin/nginx
    test -d /usr/local/nginx && rm -vfr /usr/local/nginx
    test -e /etc/nginx/fastcgi_params && rm -vf /etc/nginx/fastcgi_params
    test -e /usr/lib/systemd/system/nginx.service && rm -vf /usr/lib/systemd/system/nginx.service
    test -e /etc/init.d/nginx && rm -vf /etc/init.d/nginx
    rm -vf  /etc/nginx/*.conf /etc/nginx/*.inc /etc/nginx/zone* /etc/nginx/general.vhost /etc/nginx/testcookie.whitelist /etc/nginx/koi-utf /etc/nginx/koi-win  /etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/win-utf /etc/nginx/mime.types
}

function installHooks(){
    /usr/local/cpanel/bin/manage_hooks delete script /opt/nginxhttpd/bin/addDomainHook.sh 2> /dev/null || echo ""
    /usr/local/cpanel/bin/manage_hooks delete script  /opt/nginxhttpd/bin/postSslHook.sh 2> /dev/null || echo ""
    /usr/local/cpanel/bin/manage_hooks add script /opt/nginxhttpd/bin/addDomainHook.sh
    /usr/local/cpanel/bin/manage_hooks add script  /opt/nginxhttpd/bin/postSslHook.sh
}

function restartServices(){
  if test -e $SYSTEMCTL ; then
      info "Restarting private redis (systemctl)"
      $SYSTEMCTL restart o2redis
      info "Restarting openresty (systemctl)"
      $SYSTEMCTL restart openresty
  else
      info "Starting private redis (init.d)"
      $PRIVATE_REDIS_INITD_PATH start || $PRIVATE_REDIS_INITD_PATH start
      info "Starting Openresty (init.d)"
      $OPENRESTY_INIT_PATH start
      sleep 1
      $OPENRESTY_INIT_PATH status || $OPENRESTY_INIT_PATH start
  fi
}

function excludeDirFromCagefs(){
  echo '/opt/nginxhttpd' > /etc/cagefs/empty.dirs/nginxhttpd
}
###
## Check and set the options
###

checkCommands

while getopts "h?o:a:f:r" opt; do
    case "$opt" in
    h)  help
        ;;
    o)  test -e $OPTARG || exitErrorUsage "-o $OPTARG does not exists"
        file $OPTARG | grep -q 'gzip' || exitErrorUsage "-o $OPTARG is not a Gzip file"
        OPENRESTY_ARCHIVE=$OPTARG
        ;;
    a)  if test "$OPTARG" != "openresty" && "$OPTARG" != "none" ; then
            exitErrorUsage "-a $OPTARG value must be one of the following : openresty|none"
        else
            ACTIVATE=$OPTARG
        fi
        ;;
    f)  if test "$OPTARG" == "openresty" ; then
            OPENRESTY_OVERRIDE_CONF=1
        elif test "$OPTARG" == "none" ; then
            OPENRESTY_OVERRIDE_CONF=0
            NGINX_OVERRIDE_CONF=0
        else
            exitErrorUsage "-f $OPTARG value must be one of the following : openresty|none"
        fi
        echo -e "$OPTARG"
        ;;
    r)
        PRIVATE_REDIS_FORCE=1 # not used now
        ;;
    \?) usage
        ;;
    esac
done

###
## Main
###

if test $OPENRESTY_ARCHIVE != 0 ; then
    _eg "Setup working directories"
    setupWorkingDirs

    privateRedisForce

    _eg "Checking if redis in installed and install it if needed"
    checkAndInstallRedis

    _eg "Checking it the private redis, with the password authentification is deployed"
    checkAndInstallPrivateRedis

    _eg "Extracting the OpenResty archive"
    extractOpenrestyArchive $OPENRESTY_ARCHIVE

    _eg "Sanity check : verify that the libs required by Nginx are installed"
    sanityCheckLibs $TMP_WORKING_DIR/openresty/usr/local/openresty/nginx/sbin/nginx

    _eg "Sanity check : verify that the Nginx binary can be executed and show at least the version"
    sanityCheckVersion $TMP_WORKING_DIR/openresty/usr/local/openresty/nginx/sbin/nginx

    _eg "Checking is a version of OpenResty is already installed"
    if test $OPENRESTY_ALREADY_INSTALLED -eq 1 ; then
        info "Openresty is already installed, we create a backup of the current files : $TMP_ROLLBACK/openresty-$CURRENT_DATE.tar.gz"
        backupCurrentOpenrestyAndConf
        info "Removing current Openresty"
        removeCurrentOpenresty
    else
        _eg "Openresty is not installed yet"
    fi

    _eg "Deploying Openresty compiled files"
    installOpenrestyFiles

    _eg "Installing dependencies with OPM (Openresty package manager)"
    installOpmDependencies

    _eg "Checking Openresty configuration"
    if test -d $OPENRESTY_CONF_DIR ; then
        info "An openresty configuration exists"
        if test $OPENRESTY_OVERRIDE_CONF -eq 1 ; then
            info "Openresty conf override set to true : overriding it"
            overrideOpenrestyConf
        else
            info "Openresty conf override set to false, we do nothing"
        fi
    else
        info "Openresty configuration not found"
        overrideOpenrestyConf
    fi
else
    if test $OPENRESTY_OVERRIDE_CONF -eq 1 ; then
        _eg "Overriding of Openresty configuration set to true"
        if test -d $OPENRESTY_DIR ; then
            overrideOpenrestyConf
        else
            _ey "Overriding of Openresty configuration asked but $OPENRESTY_DIR does not exists. Ignoring"
        fi
    fi
fi

case $ACTIVATE in
    openresty)
        _eg "Activation of Openresty requested"
        activateOpenresty
        installChkservdFiles
        activateChkservdMonitor openresty
	installMonit
        ;;
    none)
        _eg "No activation requested"
        # TODO : Display what's being used
        ;;
esac

excludeDirFromCagefs

_eg "Installing / Checking cron"
installCron

_eg "Removing trace of old vanilla Nginx"
cleanUpVanillaNginx

_eg "Making sure the hooks are installed"
installHooks

_eg "Restarting the services"
restartServices
status openresty
status o2redis

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists