#!/usr/bin/bash

set -e

. /usr/share/grommunio-pkg-bits/scripts/setup-include.sh


DISTRO_FAMILY=$(get_distro_family)

setup_mariadb () {
	systemctl enable --now mariadb.service
	# Don't think the following is needed
	#mysql_secure_installation
}

setup_pgsql () {
	if test "$DISTRO_FAMILY" = 'rhel'; then
		postgresql-setup --initdb
	elif test "$DISTRO_FAMILY" = 'suse'; then
		# editing config file directly - postgresql is not hooked into ucf as its a system package
		perl -pe 's/^POSTGRES_INITDB_OPTS=.*$/POSTGRES_INITDB_OPTS="--auth-local=peer --auth-host=scram-sha-256"/' \
			-i /etc/sysconfig/postgresql
	fi
	systemctl enable --now postgresql.service
}

case "$PROGNAME" in 
	setup-db-mariadb)
		check_if_root
		setup_mariadb
		;;
	setup-db-pgsql)
		check_if_root
		setup_pgsql
		;;
esac
