#!/bin/bash

if [ -z "$2" ]; then
    for extension in bcmath bz2 curl exif gd gettext intl mbstring memcached mysqli opcache pcntl pdo_mysql pdo_pgsql shmop soap sockets xmlrpc xsl zip zend; do
        if [ ! -f /opt/php74/conf.d/$extension.ini ]; then
            ln -s /opt/php74/mods-available/$extension.ini  /opt/php74/conf.d/$extension.ini
        fi
    done
fi

if [ -f /opt/php74/.need.convert ]; then
    # Convert old configs to new path
    cd /opt/php74/conf.d
    for config in $(grep -Prl '^\s*(extension|zend_extension)'); do
        if [ ! -L $config ] && [ -f /opt/php74/mods-available/$config ]; then
            cp /opt/php74/mods-available/$config /opt/php74/conf.d/$config
            rm /opt/php74/conf.d/$config
            ln -s /opt/php74/mods-available/$config /opt/php74/conf.d/$config
            echo "The file /opt/php74/conf.d/$config converted."
        fi
    done

    # Convert disabled configs
    for config in $(grep -Prl '^\s*;\s*(extension|zend_extension)'); do
        if [ ! -L $config ] && [ -f /opt/php74/mods-available/$config ]; then
            mv /opt/php74/conf.d/$config /opt/php74/mods-available/$config.old
            #ln -s /opt/php74/mods-available/$config /opt/php74/conf.d/$config
        fi
    done

    for extension in bcmath bz2 curl exif gd gettext intl mbstring memcached mysqli opcache pcntl pdo_mysql pdo_pgsql shmop soap sockets xmlrpc xsl zip ftp pgsql; do
        if [ ! -f /opt/php74/conf.d/$extension.ini ]; then
            ln -s /opt/php74/mods-available/$extension.ini  /opt/php74/conf.d/$extension.ini
        fi
    done

    rm /opt/php74/.need.convert
fi

systemctl daemon-reload
systemctl enable fp2-php74-fpm

if systemctl -q is-active fp2-php74-fpm.service ; then
    systemctl reload fp2-php74-fpm.service
else
    systemctl start fp2-php74-fpm.service
fi

if systemctl -q is-active apache2.service ; then
    systemctl reload apache2.service
fi
