#!/bin/bash

# see https://docs.nextcloud.com/server/latest/admin_manual/installation/php_configuration.html#required-php-modules

ckmodules()
{
	local mod=
	local pkg=
	local match=

	for mod
	do
		case $mod in
		  *,*)	match=$(echo "$mod" | cut -d',' -f2)
		  	mod=$(echo "$mod" | cut -d',' -f1)
			;;
		  *)	match=$mod
		  	;;
		esac

		printf "  %-12s: " $mod

		pkg=$(apt-cache search php${php_ver}-$match)
		[ "X$pkg" = "X" ] && pkg=$(apt-cache search php-$match)
		pkg=${pkg/ */}


		echo "$PhpModules" | grep -q -i "^$mod$" && {
			printf "%-20s ok\n" "$pkg"
			continue
		}
		pkg=${pkg:-**NO_PACKAGE**}
		printf "%-20s NOT_INSTALLED\n" "$pkg"
	done
	return 0
}

# (MAIN)

if [ $# != 0 ]
then
	php_ver=$1
	shift
else
	php_ver=$(kusa-conf php.version 2>/dev/null) || {
		php_ver=$(ls /etc/php | tail -1)
	}
fi

echo "
	CHECKING EXTENSION FOR PHP VERSION: $php_ver
"

PhpModules=$(php -m | fgrep -v ' ' \
	| sed -e 's/\[.*//' -e 's/^\s*$//' -e '/^$/d' \
	| tr '[A-Z]' '[a-z]' | sort -u)


echo "
REQUIRED MODULES"
ckmodules curl DOM GD xml mbstring zip

# already included in php*-common or in php itself
# (listed here for reference)
#
# ctype fileinfo posix OpenSSL session zlib

# optional but we install them
ckmodules intl
# sodium sysvsem	# in php*-common or php

# our choiche, mysql
#
ckmodules pdo_mysql,mysql



echo "

OPTIONAL, FOR SPECIFIC TASKS"
ckmodules ldap smbclient ftp imap


echo "

OPTIONAL (NOT YET ENABLED): CACHING"
ckmodules apc redis



exit 0
