#!/usr/bin/env /lib/runit/invoke-run
#Copyright: 2026 Lorenzo Puliti <plorenzo@disroot.org>
#License: BSD-3-Clause

exec 2>&1

#by default we run 18-main; for example to create a '16-main' instance do:
#cpsv p postgresql@default postgresql@16-main ; update-service --add postgresql@16-main ;
#use 'pg_createcluster -u postgres  18 main' to create the '18-main' cluster

INSTANCE=${PWD##*@}
if [ "$INSTANCE" = 'default' ]; then
    VERSION=18 ; CLUSTER=main
else
    VERSION=${INSTANCE%-*} ; CLUSTER=${INSTANCE##*-}
fi

#config check
[ -r /etc/postgresql/"$VERSION"/"$CLUSTER"/postgresql.conf ] || exit 162
[ -d /var/lib/postgresql/"$VERSION"/"$CLUSTER" ] || exit 162

install -o postgres -g postgres -m 2775 -d /run/postgresql
[ -x /sbin/restorecon ] && restorecon -R /run/postgresql || true

#exec ##bin## --foreground "$VERSION"/"$CLUSTER" start
