#!/bin/sh -e
# -*- mode: sh; indent-tabs-mode: t -*-
#
# 2019-2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# 2020 Beni Ruef (bernhard.ruef at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

state_is=$(cat "${__object:?}/explorer/state")
state_should=$(cat "${__object:?}/parameter/state")
postgres_user=$(cat "${__object:?}/explorer/postgres_user")

conf_name=${__object_id:?}

if test "${state_is}" = "${state_should}"
then
	exit 0
fi

quote() {
	for _arg
	do
		shift
		if test -n "$(printf '%s' "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')"
		then
			# needs quoting
			set -- "$@" "'$(printf '%s' "${_arg}" | sed -e "s/'/'\\\\''/g")'"
		else
			set -- "$@" "${_arg}"
		fi
	done
	unset _arg

	# NOTE: Use printf because POSIX echo interprets escape sequences
	printf '%s' "$*"
}


psql_cmd() {
	printf 'su - %s -c %s\n' "$(quote "${postgres_user}")" "$(quote "$(quote psql "$@")")"
}

case ${state_should}
in
	(present)
		test -n "${__object:?}/parameter/value" || {
			echo 'Missing required parameter --value' >&2
			exit 1
		}

		cat <<-EOF
		exec 3< "\${__object:?}/parameter/value"
		$(psql_cmd postgres -tAwq -o /dev/null -v ON_ERROR_STOP=on) <<'SQL'
		\\set conf_value \`cat <&3\`
		ALTER SYSTEM SET ${conf_name} = :'conf_value';
		SELECT pg_reload_conf();
		SQL
		exec 3<&-
		EOF
		;;
	(absent)
		psql_cmd postgres -qwc "ALTER SYSTEM SET ${conf_name} TO DEFAULT"
		;;
	(*)
		printf 'Invalid --state: %s\n' "${state_should}" >&2
		printf 'Only "present" and "absent" are acceptable.\n' >&2
		exit 1
		;;
esac

# Restart PostgreSQL server if required to apply new configuration value
cat <<EOF

if test 't' = "\$($(psql_cmd postgres -twAc "SELECT pending_restart FROM pg_settings WHERE lower(name) = lower('${conf_name}')"))"
then
	$(
		init=$(cat "${__global:?}/explorer/init")
		case ${init}
		in
			(systemd)
				echo 'systemctl restart postgresql.service'
				;;
			(*openrc*)
				echo 'rc-service postgresql restart'
				;;
			(sysvinit)
				echo '/etc/init.d/postgresql restart'
				;;
			(init)
				case $(cat "${__global:?}/explorer/kernel_name")
				in
					(FreeBSD)
						echo '/usr/local/etc/rc.d/postgresql restart'
						;;
					(OpenBSD|NetBSD)
						echo '/etc/rc.d/postgresql restart'
						;;
					(*)
						echo "Unsupported operating system. Don't know how to restart services." >&2
						exit 1
				esac
				;;
			(*)
				printf "Don't know how to restart services with your init (%s)\n" "${init}" >&2
				exit 1
		esac
	)
fi
EOF
