#!/bin/sh
if [ "$PKG_UPGRADE" != 1 ]; then
    uci set uhttpd.main.index_page='index.php'
    uci set uhttpd.main.interpreter='.php=/usr/bin/php-cgi'
    uci commit uhttpd
    uci get luci.themes.Spectra >/dev/null 2>&1 || \
    uci batch <<EOF
set luci.themes.Spectra=/luci-static/spectra
set luci.main.mediaurlbase=/luci-static/spectra
commit luci
EOF

    rm -rf /tmp/luci-*
    if [ -n "$(command -v nginx)" ];then
        if [ ! -f /etc/nginx/conf.d/spectra.locations ]; then
cat << 'EOF' > /etc/nginx/conf.d/spectra.locations
location /luci-static/spectra/ {
    location ~ \.php$ {
        if (!-f $request_filename) {
            return 404;
        }
        fastcgi_pass unix:/var/run/php8-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}
EOF
            /etc/init.d/nginx restart
        fi
    elif [ -n "$(command -v lighttpd)" ];then
cat << 'EOF' > /etc/lighttpd/conf.d/81-php.conf
cgi.assign += ( ".php" => "/usr/bin/php-cgi" )
EOF
        /etc/init.d/lighttpd restart
    else
        uci set uhttpd.main.index_page='index.php'
        uci set uhttpd.main.interpreter='.php=/usr/bin/php-cgi'
        uci commit uhttpd
        /etc/init.d/uhttpd restart
    fi
fi
exit 0
