#!/bin/bash
# preinst script for #PACKAGE#
#
# see: dh_installdeb(1)

set -e

# Begin vars.sh
#!/bin/bash
# DO NOT EDIT
# This file is regenerated each run.
# If you want to add or change a variable, edit debify/settings.json and the vars hashref

source /etc/os-release
export OBS_REPO="x${NAME}_${VERSION_ID}"
if ls debian/*.install &> /dev/null; then
    perl -pi -e 's/\${env:OBS_REPO}/$ENV{OBS_REPO}/' debian/*.install
fi

export DEB_INSTALL_ROOT="/usr/src/packages/BUILD/debian/tmp"
export DEB_SOURCE_ROOT="/usr/src/packages/BUILD/debian/SOURCES_FROM_SPEC"
export BASE_CONFIGURE_ARGS="$(echo "--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nobody --group=nobody --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl-opt=enable-tls1_3 --with-openssl-opt=no-nextprotoneg")"
export BASE_WITH_CC_OPT=""
export BASE_WITH_LD_OPT=""
export RPM_SOURCE_DIR="/usr/src/packages/BUILD/debian/SOURCES_FROM_SPEC"
export WITH_CC_OPT="$(echo "")"
export WITH_LD_OPT="$(echo "")"
export __isa_bits="64"
export __isa_name="x86"
export __sourcedir=""
export _bindir="/opt/cpanel//root/usr/bin"
export _builddir=""
export _datadir="/usr/share"
export _defaultdocdir="/usr/share/doc"
export _docdir="/usr/share/doc"
export _exec_prefix="/opt/cpanel//root/usr"
export _group="Productivity/Networking/Web/Servers"
export _includedir="/opt/cpanel//root/usr/include"
export _infodir="/usr/share/info"
export _isa=""
export _lib="lib64"
export _libdir="/usr/lib64"
export _libexecdir="/usr/libexec"
export _localstatedir="/var"
export _mandir="/usr/share/man"
export _prefix="/opt/cpanel//root/usr"
export _root_bindir="/opt/cpanel//root/usr/bin"
export _root_datadir="/opt/cpanel//root/usr/share"
export _root_exec_prefix="/opt/cpanel//root/usr"
export _root_includedir="/opt/cpanel//root/usr/include"
export _root_infodir="/usr/share/info"
export _root_initddir="/etc/rc.d/init.d"
export _root_libdir="/opt/cpanel//root/usr/lib64"
export _root_libexecdir="/opt/cpanel//root/usr/libexec"
export _root_localstatedir="/var"
export _root_mandir="/usr/share/man"
export _root_prefix="/opt/cpanel//root/usr"
export _root_sbindir="/opt/cpanel//root/usr/sbin"
export _root_sharedstatedir="/opt/cpanel//root/usr/com"
export _root_sysconfdir="/etc"
export _sbindir="/usr/sbin"
export _scl_prefix="/opt/cpanel"
export _scl_root="/opt/cpanel//root"
export _sysconfdir="/etc"
export _unitdir="/usr/lib/systemd/system"
export bdir="/nginx-1.25.3"
export buildroot="/usr/src/packages/BUILD"
export dist=".el8"
export ea_openssl_ver="1.1.1d-1"
export epoch="1"
export full_package_name="ea-nginx-1.25.3"
export ix86="i386"
export main_version="1.25.3"
export name="ea-nginx"
export nginx_group="nobody"
export nginx_home="/var/cache/nginx"
export nginx_loggroup="adm"
export nginx_user="nobody"
export nil="0"
export ns_name="ea"
export os_minor="%(lsb_release -rs | cut -d '.' -f 2)"
export pkg=""
export pkg_name=""
export pkgname=""
export previous_version="1.25.2"
export release="1"
export release_prefix="1"
export ruby_version="ea-ruby27"
export scl=""
export scl_name=""
export scl_name_base=""
export scl_name_version=""
export scl_prefix=""
export upstream_name="nginx"
export use_systemd="1"
export version="1.25.3"
export version_with_debian_revision="1.25.3-1"
export SOURCE0="$DEB_SOURCE_ROOT/nginx-1.25.3.tar.gz"
export SOURCE1="$DEB_SOURCE_ROOT/logrotate"
export SOURCE2="$DEB_SOURCE_ROOT/nginx.init.in"
export SOURCE3="$DEB_SOURCE_ROOT/nginx.sysconf"
export SOURCE4="$DEB_SOURCE_ROOT/nginx.conf"
export SOURCE5="$DEB_SOURCE_ROOT/nginx.vh.default.conf"
export SOURCE7="$DEB_SOURCE_ROOT/nginx-debug.sysconf"
export SOURCE8="$DEB_SOURCE_ROOT/nginx.service"
export SOURCE9="$DEB_SOURCE_ROOT/nginx.upgrade.sh"
export SOURCE11="$DEB_SOURCE_ROOT/nginx-debug.service"
export SOURCE12="$DEB_SOURCE_ROOT/COPYRIGHT"
export SOURCE13="$DEB_SOURCE_ROOT/nginx.check-reload.sh"
export SOURCE14="$DEB_SOURCE_ROOT/cpanel.tar.gz"
export SOURCE15="$DEB_SOURCE_ROOT/cpanel-chksrvd"
export SOURCE16="$DEB_SOURCE_ROOT/cpanel-scripts-ea-nginx"
export SOURCE17="$DEB_SOURCE_ROOT/FPM_50x.html"
export SOURCE18="$DEB_SOURCE_ROOT/Nginx.pm"
export SOURCE19="$DEB_SOURCE_ROOT/cpanel-scripts-ea-nginx-userdata"
export SOURCE20="$DEB_SOURCE_ROOT/ngx_http_pipelog_module-ngx_http_pipelog_module.c"
export SOURCE21="$DEB_SOURCE_ROOT/ngx_http_pipelog_module-config"
export SOURCE22="$DEB_SOURCE_ROOT/NginxHooks.pm"
export SOURCE23="$DEB_SOURCE_ROOT/NginxTasks.pm"
export SOURCE24="$DEB_SOURCE_ROOT/nginx-adminbin"
export SOURCE25="$DEB_SOURCE_ROOT/nginx-adminbin.conf"
export SOURCE27="$DEB_SOURCE_ROOT/007-restartsrv_nginx"
export SOURCE28="$DEB_SOURCE_ROOT/whm_feature_addon"
export SOURCE29="$DEB_SOURCE_ROOT/set_NGINX_CONFIGURE_array.sh"
# End vars.sh

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    install|upgrade)
    ;;

    abort-upgrade)
    ;;

    *)
        echo "preinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# Contents of SOURCES/pkg.preinst
#!/bin/bash

if test -x "/usr/bin/apt"; then
    if [ "$1" = "install" ]; then
        # Other nginx implementations can leave behind stray config files when they are removed.
        # As such, we make a best effort to ensure a clean config when ea-nginx is installed

        # Engintron will leave behind executable files in this directory which in turn are called via a cron job
        # The executables can and do create files in /etc/nginx/conf.d/ that will cause nginx to fail to start with
        # our implementation
        if [ -e /etc/nginx/utilities ]; then
            echo "Removing /etc/nginx_utilities.pre_install_ea_nginx_config"
            rm -rf /etc/nginx_utilities.pre_install_ea_nginx_config
            echo "Moving /etc/nginx/utilities aside to ensure valid config for ea-nginx since this is a new install"
            mv -fv /etc/nginx/utilities /etc/nginx_utilities.pre_install_ea_nginx_config ||:
        fi

        # Since any stray *.conf files in this directory will be picked up by nginx.conf,
        # we want to ensure that this directory is fresh on new installs
        if [ -e /etc/nginx/conf.d ]; then
            echo "Removing /etc/nginx_conf.d.pre_install_ea_nginx_config"
            rm -rf /etc/nginx_conf.d.pre_install_ea_nginx_config
            echo "Moving /etc/nginx/conf.d aside to ensure valid config for ea-nginx since this is a new install"
            mv -fv /etc/nginx/conf.d /etc/nginx_conf.d.pre_install_ea_nginx_config ||:
        fi

        # If modsec rulesets have been installed, they will create a symlink in '/etc/nginx/conf.d/modsec_vendor_configs/'
        # with conf files in it.  We need to make sure we do not blow away modsec rulesets here too.
        if [ -e /etc/nginx_conf.d.pre_install_ea_nginx_config/modsec_vendor_configs ]; then
            mkdir -p /etc/nginx/conf.d
            cp -r /etc/nginx_conf.d.pre_install_ea_nginx_config/modsec_vendor_configs /etc/nginx/conf.d/modsec_vendor_configs
        fi

        # We need to ensure that any config files that were present are still present
        # so that cpio does not fail to unpack
        if [ -e /etc/nginx_conf.d.pre_install_ea_nginx_config/includes-optional/cloudflare.conf ]; then
            mkdir -p /etc/nginx/conf.d/includes-optional
            cp /etc/nginx_conf.d.pre_install_ea_nginx_config/includes-optional/cloudflare.conf /etc/nginx/conf.d/includes-optional/cloudflare.conf
        fi
        if [ -e /etc/nginx_conf.d.pre_install_ea_nginx_config/ea-nginx.conf ]; then
            mkdir -p /etc/nginx/conf.d
            cp /etc/nginx_conf.d.pre_install_ea_nginx_config/ea-nginx.conf /etc/nginx/conf.d/ea-nginx.conf
        fi
        if [ -e /etc/nginx_conf.d.pre_install_ea_nginx_config/default.conf ]; then
            mkdir -p /etc/nginx/conf.d
            cp /etc/nginx_conf.d.pre_install_ea_nginx_config/default.conf /etc/nginx/conf.d/default.conf
        fi
        if [ -e /etc/nginx_conf.d.pre_install_ea_nginx_config/conf.d/includes-optional/set-CACHE_KEY_PREFIX.conf ]; then
            mkdir -p /etc/nginx/conf.d/includes-optional
            cp /etc/nginx_conf.d.pre_install_ea_nginx_config/set-CACHE_KEY_PREFIX.conf /etc/nginx/conf.d/includes-optional/set-CACHE_KEY_PREFIX.conf
        fi
    fi

    # we need to know the version of cPanel, the hooks cannot be deployed
    # before version 11.80
    cpversion=`/usr/local/cpanel/3rdparty/bin/perl -MCpanel::Version -e 'print Cpanel::Version::get_short_release_number()'`
    if [ $cpversion -ge 80 ]; then
        # do not let nginx hooks run during upgrade. Use GTE because it can go higher,
        # but anything 2 or greater is still an upgrade.
        # Also deregister the hooks on this step, as that's the right point to do it

        # ZC-5816: both pre and preun remove our hooks.
        # meaning it would happen twice on downgrade/upgrade etc, seeing errors on
        # the 2nd removal.
        numhooks=`/usr/local/cpanel/bin/manage_hooks list 2> /dev/null | grep 'hook: NginxHooks::' | wc -l`
        if [ "$numhooks" -ge 1 ]; then
            /usr/local/cpanel/bin/manage_hooks delete module NginxHooks
        fi
    fi

    # Add the "nginx" user
    getent group ${nginx_group} >/dev/null || groupadd -r ${nginx_group}
    getent passwd ${nginx_user} >/dev/null || \
        useradd -r -g ${nginx_group} -s /sbin/nologin \
        -d ${nginx_home} -c "nginx user"  ${nginx_user}
    exit 0
fi

# End SOURCES/pkg.preinst


exit 0
