# Template file for 'postgresql17'
pkgname=postgresql17
version=17.4
revision=2
build_style=gnu-configure
make_build_target=world
_major="${version%%.*}"
_prefix="usr/lib/psql${_major}"
_sysconfdir="etc/psql${_major}"
configure_args="--with-openssl --with-python
 --with-pam --enable-thread-safety
 --with-perl --with-tcl --without-ldap --without-gssapi
 --without-bonjour --with-libxml --with-libxslt
 --with-system-tzdata=/usr/share/zoneinfo --enable-nls --with-uuid=e2fs"
hostmakedepends="docbook2x gettext flex openjade pkg-config"
makedepends="libfl-devel openssl-devel libuuid-devel libxslt-devel pam-devel perl
 python3-devel readline-devel tcl-devel"
short_desc="Sophisticated open-source Object-Relational DBMS"
maintainer="Piotr Wójcik <chocimier@tlen.pl>"
license="PostgreSQL"
homepage="https://www.postgresql.org"
changelog="https://www.postgresql.org/docs/current/release-${version//./-}.html"
distfiles="https://ftp.postgresql.org/pub/source/v${version}/postgresql-${version}.tar.bz2"
checksum=c4605b73fea11963406699f949b966e5d173a7ee0ccaef8938dec0ca8a995fe7

conf_files="
 /${_sysconfdir}/default/postgresql
 /${_sysconfdir}/pam.d/postgresql
 /${_sysconfdir}/postgresql/postgresql.conf
"
alternatives="
 postgresql:/usr/bin/initdb:/${_prefix}/bin/initdb
 postgresql:/usr/bin/pg_amcheck:/${_prefix}/bin/pg_amcheck
 postgresql:/usr/bin/pg_archivecleanup:/${_prefix}/bin/pg_archivecleanup
 postgresql:/usr/bin/pg_basebackup:/${_prefix}/bin/pg_basebackup
 postgresql:/usr/bin/pg_checksums:/${_prefix}/bin/pg_checksums
 postgresql:/usr/bin/pg_combinebackup:/${_prefix}/bin/pg_combinebackup
 postgresql:/usr/bin/pg_controldata:/${_prefix}/bin/pg_controldata
 postgresql:/usr/bin/pg_createsubscriber:/${_prefix}/bin/pg_createsubscriber
 postgresql:/usr/bin/pg_ctl:/${_prefix}/bin/pg_ctl
 postgresql:/usr/bin/pg_receivewal:/${_prefix}/bin/pg_receivewal
 postgresql:/usr/bin/pg_recvlogical:/${_prefix}/bin/pg_recvlogical
 postgresql:/usr/bin/pg_resetwal:/${_prefix}/bin/pg_resetwal
 postgresql:/usr/bin/pg_rewind:/${_prefix}/bin/pg_rewind
 postgresql:/usr/bin/pg_test_fsync:/${_prefix}/bin/pg_test_fsync
 postgresql:/usr/bin/pg_test_timing:/${_prefix}/bin/pg_test_timing
 postgresql:/usr/bin/pg_upgrade:/${_prefix}/bin/pg_upgrade
 postgresql:/usr/bin/pg_verifybackup:/${_prefix}/bin/pg_verifybackup
 postgresql:/usr/bin/pg_waldump:/${_prefix}/bin/pg_waldump
 postgresql:/usr/bin/pg_walsummary:/${_prefix}/bin/pg_walsummary
 postgresql:/usr/bin/pgbench:/${_prefix}/bin/pgbench
 postgresql:/usr/bin/postgres:/${_prefix}/bin/postgres
"

# Create 'postgres' user for the server.
system_accounts="postgres"
postgres_homedir="/var/lib/postgresql"
postgres_shell="/bin/sh"
postgres_descr="PostgreSQL database server user"
make_dirs="
 /var/lib/postgresql   0750 postgres postgres
 /var/lib/postgresql${_major} 0750 postgres postgres"

build_options="languages"
desc_option_languages="Include procedural languages modules"

if [ -z "$CROSS_BUILD" ]; then
	build_options_default="languages"
fi

if [ -z "$build_option_languages" ]; then
	configure_args+=" --without-perl --without-python --without-tcl"
fi

post_patch() {
	cp "$FILESDIR"/pg_config.sh.in src/common/pg_config.sh.in
	_substitute src/common/pg_config.sh.in
	vsed -i -e "s,@XBPS_SYSROOT@,${XBPS_CROSS_BASE%/}," src/common/Makefile
}

pre_configure() {
	_style_configure_args="${configure_args}"
	configure_args=" "
	for arg in ${_style_configure_args}; do
		case $arg in
			--prefix=*|*dir=*) : ;; # skip
			*) configure_args+=" $arg"
		esac
	done
	configure_args="--prefix=/${_prefix}
	 --sysconfdir=/${_sysconfdir}
	 --includedir=/usr/include
	 ${configure_args}"
}

post_build() {
	make -C contrib ${makejobs}
}

pre_check() {
	ln -sr tmp_install/${_prefix} /${_prefix%/*}
}

post_install() {
	vmkdir usr/share/man
	cp -r doc/src/sgml/man? ${DESTDIR}/usr/share/man
	for i in ${DESTDIR}/usr/share/man/*[^3]/*; do
		mv $i ${i%.*}-${_major}.${i##*.}
	done
	vmkdir ${_prefix}/share/doc
	cp -r doc/src/sgml/html ${DESTDIR}/${_prefix}/share/doc
	vsv ${pkgname}
	find "${DESTDIR}/etc/sv" -type f -exec sed -i -e s:@VERSION@:${_major}: -e s:@PREFIX@:${_prefix}: '{}' ';'
	vlicense COPYRIGHT

	vinstall ${FILESDIR}/postgresql.confd 644 ${_sysconfdir}/default postgresql
	_substitute ${DESTDIR}/${_sysconfdir}/default/postgresql
	vinstall ${FILESDIR}/postgresql.pam 644 ${_sysconfdir}/pam.d postgresql
	_substitute ${DESTDIR}/${_sysconfdir}/pam.d/postgresql
	vinstall ${DESTDIR}/${_prefix}/share/postgresql/postgresql.conf.sample \
		644 ${_sysconfdir}/postgresql postgresql.conf
	vbin src/common/pg_config.sh

	vsed -i 's/install_bin = .*/install_bin = install/g' \
		${DESTDIR}/${_prefix}/lib/postgresql/pgxs/src/Makefile.global
}

_substitute() {
	sed -i "$1" -e s:@PREFIX@:${_prefix}: -e s:@VERSION@:${_major}: -e s:@FULL_VERSION@:${version}:
}

postgresql17-doc_package() {
	short_desc="PostgreSQL documentation"
	pkg_install() {
		vmove ${_prefix}/share/doc
	}
}

postgresql-libs_package() {
	short_desc="PostgreSQL shared libraries"
	conflicts="postgresql9.6-libs>=0 postgresql12-libs>=0 postgresql13-libs>=0 postgresql14-libs>=0 postgresql15-libs>=0 postgresql16-libs>=0"
	pkg_install() {
		vmove "${_prefix}/lib/*.so*"
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		    -type f -name libpq5\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
		mv ${PKGDESTDIR}/${_prefix}/lib/* ${PKGDESTDIR}/usr/lib
	}
}

postgresql-libs-devel_package() {
	depends="postgresql-libs>=${version}_${revision} openssl-devel"
	short_desc="PostgreSQL shared libraries (development files)"
	conflicts="postgresql9.6-libs-devel>=0 postgresql12-libs-devel>=0 postgresql13-libs-devel>=0 postgresql14-libs-devel>=0 postgresql15-libs-devel>=0 postgresql16-libs-devel>=0"
	pkg_install() {
		vmkdir usr/bin
		vmkdir usr/lib
		for f in pg_config ecpg; do
			vmove ${_prefix}/bin/${f}
			vmove "usr/share/man/man1/$(basename ${f})*"
			ln -sr "${PKGDESTDIR}/${_prefix}/bin/${f}" "${PKGDESTDIR}/usr/bin"
		done
		vmove usr/bin/pg_config.sh
		vmove usr/include
		vmove "${_prefix}/lib/*.a"
		mv "${DESTDIR}/${_prefix}/lib/pkgconfig" "${PKGDESTDIR}/usr/lib"
		vmove usr/share/man/man3
		vmove ${_prefix}/lib/postgresql/pgxs
		for f in libpq.so libpgtypes.so libecpg_compat.so libecpg.so; do
			ln -sr "${PKGDESTDIR}/usr/lib/${f}" "${PKGDESTDIR}/${_prefix}/lib"
		done
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		   -type f -name pg_config\* -o -name ecpg\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
	}
}

# XXX disabled for now in cross builds.
if [ -z "$CROSS_BUILD" ]; then

postgresql17-plperl_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc="PL/Perl procedural language for PostgreSQL"
	pkg_install() {
		vmove "${_prefix}/lib/postgresql/plperl*"
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		   -type f -name plperl\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
	}
}

postgresql17-plpython_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc="PL/Python procedural language for PostgreSQL"
	pkg_install() {
		vmove "${_prefix}/lib/postgresql/plpython*"
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		   -type f -name plpython\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
	}
}

postgresql17-pltcl_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc="PL/Tcl procedural language for PostgreSQL"
	pkg_install() {
		vmove "${_prefix}/lib/postgresql/pltcl*"
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		   -type f -name pltcl\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
	}
}

fi # !CROSS_BUILD

postgresql17-client_package() {
	short_desc="Client frontends programs for PostgreSQL"
	alternatives="
	 postgresql-client:/usr/bin/clusterdb:/${_prefix}/bin/clusterdb
	 postgresql-client:/usr/bin/createdb:/${_prefix}/bin/createdb
	 postgresql-client:/usr/bin/createuser:/${_prefix}/bin/createuser
	 postgresql-client:/usr/bin/dropdb:/${_prefix}/bin/dropdb
	 postgresql-client:/usr/bin/dropuser:/${_prefix}/bin/dropuser
	 postgresql-client:/usr/bin/pg_dump:/${_prefix}/bin/pg_dump
	 postgresql-client:/usr/bin/pg_dumpall:/${_prefix}/bin/pg_dumpall
	 postgresql-client:/usr/bin/pg_isready:/${_prefix}/bin/pg_isready
	 postgresql-client:/usr/bin/pg_restore:/${_prefix}/bin/pg_restore
	 postgresql-client:/usr/bin/psql:/${_prefix}/bin/psql
	 postgresql-client:/usr/bin/reindexdb:/${_prefix}/bin/reindexdb
	 postgresql-client:/usr/bin/vacuumdb:/${_prefix}/bin/vacuumdb
	"
	pkg_install() {
		for f in clusterdb createdb createuser dropdb \
			dropuser pg_dump pg_dumpall pg_isready pg_restore psql reindexdb \
			vacuumdb; do
			vmove ${_prefix}/bin/${f}
			vmove usr/share/man/man1/$(basename ${f})-${_major}.1
		done
		vmove usr/share/man/man7
		for d in $(find ${DESTDIR}/${_prefix}/share/locale \
		   -type f -name pgscripts\* -o \
			   -name psql\* -o \
			   -name pg_dump\*); do
			mkdir -p ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
			mv ${d} ${PKGDESTDIR}/$(dirname ${d#${DESTDIR}})
		done
		mv ${DESTDIR}/${_prefix}/share/postgresql/psqlrc.* \
			${PKGDESTDIR}/${_prefix}/share
	}
}

postgresql17-contrib_package() {
	short_desc="PostgreSQL contributed programs and extensions"
	alternatives="
	 postgresql-contrib:/usr/bin/oid2name:/${_prefix}/bin/oid2name
	 postgresql-contrib:/usr/bin/vacuumlo:/${_prefix}/bin/vacuumlo
	"
	pkg_install() {
		make -C contrib install DESTDIR=${PKGDESTDIR}
		mv ${PKGDESTDIR}/usr/include ${PKGDESTDIR}/${_prefix}
	}
}
