#!/bin/bash
if [ _$CB_ENVDIR == '_' ]; then
   echo Please ensure SkyForm AI is configured properly in the current environment
   exit 1
fi
if which setenforce > /dev/null; then
    setenforce 0
    sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config
fi
php -m | grep -q yaml
if [ $? != 0 ]; then
    echo ERROR: Please ensure PHP and php-yaml module is installed.
    echo "- On EL7, run yum install -y epel-release, then yum install php php-pecl-yaml."
    echo "- On EL8, run tar xfz php-yaml.el8.x86_64.tar.gz -C /"
    echo "- On Ubuntu, run apt install php-yaml"
    exit 1
fi

php -m | grep -q json
if [ $? != 0 ]; then
    echo ERROR: Please ensure PHP and php-json module is installed.
    echo "- On EL8, run yum install php-json"
    echo "- On Ubuntu, run apt install php-json"
    exit 1
fi

error=0
phpservice=php-fpm
phpconf=/etc/php-fpm.d/www.conf
if [ -f /etc/lsb-release ]; then
    source /etc/lsb-release
    UVERSION=$(printf "%0.f" $DISTRIB_RELEASE)
    if [ $UVERSION == 22 ]; then
        phpservice=php8.1-fpm
    fi
    if [ $UVERSION == 24 ]; then
        phpservice=php8.3-fpm
    fi
fi
for service in nginx $phpservice; do
    systemctl -a | grep -q $service
    if [ $? != 0 ]; then
        echo ERROR: Please ensure "$service" is installed and ENABLED.
        error=1
    fi
done

if [ $error != 0 ]; then
    exit 1
fi

if [ -f /etc/php-fpm.d/www.conf ]; then
    sed -i "s|^listen = 127.0.0.1:9000|listen = /var/run/php-fpm/www.sock|" /etc/php-fpm.d/www.conf
fi

certsdir=/etc/nginx/certs
if [ ! -d $certsdir ]; then
    mkdir -p $certsdir
    openssl req -x509 -sha256 -days 3560 \
    -nodes -newkey rsa:2048 \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=Skyformaip/OU=prod" \
    -keyout $certsdir/server.key -out $certsdir/server.crt
fi

cp -f nginx.conf /etc/nginx/nginx.conf
export apacheuser=apache

# Ubuntu
if [ -f /etc/lsb-release ]; then
    sed -i s/"memory_limit = 128M"/"memory_limit = 2048M"/ /etc/php/*/fpm/php.ini
    apacheuser=www-data
    if [ $UVERSION -ge 22 ]; then
        sock=$(ls /var/run/php/php8.*-fpm.sock)
    else
        sock=$(ls /var/run/php/php7.*.sock)
    fi
    sed -i s/apache/www-data/ /etc/nginx/nginx.conf
    sed -i "s|/var/run/php-fpm/www.sock|$sock|" /etc/nginx/nginx.conf
    systemctl disable --now apache2
    rm /var/www/html/*.html
    systemctl enable nginx
    systemctl restart nginx
    systemctl enable $phpservice
    systemctl restart $phpservice
    rm /bin/sh
    cp /bin/bash /bin/sh
else
    sed -i s/"memory_limit = 128M"/"memory_limit = 2048M"/ /etc/php.ini
    systemctl enable nginx php-fpm
    systemctl restart nginx php-fpm
fi

if [ -f /var/www/html/config.yaml ]; then
    mv /var/www/html/config.yaml /var/www/html/config.yaml.orig
    upgrade=1
else
    upgrade=0
fi

tar xfz aip-portal.tar.gz -C /var/www/html

if [ -f /var/www/html/config.yaml.orig ]; then
    mv /var/www/html/config.yaml.orig /var/www/html/config.yaml
fi
chown -R $apacheuser: /var/www/html/reports /var/lib/nginx
sed -i s/"^#pp:"/"pp:"/ $CB_ENVDIR/vncsub.yaml

/var/www/html/skyformaip-userportal.run
