#!/bin/bash
#Nifty: Install PHP CLI with extensions from Remi repository

# shellcheck source=../../lib/universal/niftylib.sh
source <(curl -fSs https://nifty.sh/lib/universal/niftylib.sh)

# Requirements
require_root
require_64bit
require_os alma8 alma9 alma10

ce "Updating system"
dnf update -y

ce "Installing EPEL"
dnf install -y epel-release

ce "Installing remi repo"
OS_RELEASE="$(detect_os)"
OS_VERSION="$(get_os_version)"
[ "$OS_RELEASE" = "alma8" ] && dnf module reset -y php # Must be enabled to prevent a conflict when installing remi-release
rpm -Uvh "http://rpms.remirepo.net/enterprise/remi-release-${OS_VERSION/.*/}.rpm"

mapfile -t PHP_MODULES < <(dnf module list php | awk '/^php.+remi-/ { print $1 ":" $2 }' | sort -t'-' -k2 -rV)
if is_interactive; then
  echo
  PHP_MODULE="$(prompt_select_num_value "Which PHP version do you want to install?" "${PHP_MODULES[@]}")"
else
  PHP_MODULE="${PHP_MODULES[0]}"
fi

ce "Enabling module $PHP_MODULE"
dnf module switch-to -y "$PHP_MODULE"

ce "Installing required packages"
dnf install -y php-cli php-pdo php-opcache php-xml php-gd php-mbstring php-mcrypt php-soap php-mysqlnd php-bcmath php-pecl-zip

ce "All done!"
