#!/bin/bash
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.

if [ "x$NOSTRIP" = "xNOSTRIP" ]; then
	make DESTDIR=$TmpInstDir install
	#repmgr is installed by repmgr-4.x project
	make -C contrib/cube DESTDIR=$TmpInstDir install
else
	make DESTDIR=$TmpInstDir install-strip
	#repmgr is installed by repmgr-4.x project
	make -C contrib/cube DESTDIR=$TmpInstDir install-strip
fi

rm -rf $TmpInstDir/$BinDir/{ecpg,pg_config,*lang}
rm -rf $TmpInstDir/$LibDir/{*.*a,pkgconfig,libecpg*,libpgtypes*}
rm -rf $TmpInstDir/$LibDir/postgresql/pgxs
rm -rf $TmpInstDir/$Prefix/share/doc
rm -rf $TmpInstDir/$IncludeDir

install -d -m755 $TmpInstDir/usr/syno/bin/
install -d -m755 $TmpInstDir/usr/lib/systemd/system/
install -d -m755 $TmpInstDir/usr/lib/systemd/scripts/
install -d -m755 $TmpInstDir/usr/lib/systemd/system/pgsql-adapter.target.wants
install -m755 synology/pgsql.sh $TmpInstDir/usr/lib/systemd/scripts/pgsql.sh
install -m644 synology/pgsql-adapter.target $TmpInstDir/usr/lib/systemd/system/pgsql-adapter.target
install -m644 synology/pgsql-adapter.service $TmpInstDir/usr/lib/systemd/system/pgsql-adapter.service
install -m644 synology/pgsql.service $TmpInstDir/usr/lib/systemd/system/pgsql.service
install -m755 synology/synopsql $TmpInstDir/usr/syno/bin/
install -m755 synology/synopostgres $TmpInstDir/usr/syno/bin/

if [ "x$NOSTRIP" != "xNOSTRIP" ]; then
    ${STRIP} $TmpInstDir/usr/syno/bin/synopostgres
fi

SYNO_BOOTUP_DONE_TARGET_WANTS_UNITS=pgsql-adapter.service
SYNO_BOOTUP_DONE_TARGET_WANTS_PATH=$TmpInstDir/usr/lib/systemd/system/syno-bootup-done.target.wants

[ -d $SYNO_BOOTUP_DONE_TARGET_WANTS_PATH ] || install -d $SYNO_BOOTUP_DONE_TARGET_WANTS_PATH
ln -s ../$SYNO_BOOTUP_DONE_TARGET_WANTS_UNITS $SYNO_BOOTUP_DONE_TARGET_WANTS_PATH/$SYNO_BOOTUP_DONE_TARGET_WANTS_UNITS

install -d -m755 $TmpInstDir/usr/syno/share/feasibilitycheck/poweroff.d
install -d -m755 $TmpInstDir/usr/syno/bin/feasibilitycheck/poweroff.d
install -m644 synology/poweroffcheck/PgsqlUpdatePowerOff.cfg $TmpInstDir/usr/syno/share/feasibilitycheck/poweroff.d
install -m755 synology/poweroffcheck/PgsqlUpdatePowerOff.sh $TmpInstDir/usr/syno/bin/feasibilitycheck/poweroff.d

install -d -m755 $TmpInstDir${SynoPluginDir}/timezone_change/
install -m755 synology/pgsql_timezone_change.sh $TmpInstDir${SynoPluginDir}/timezone_change/pgsql_timezone_change.sh

install -d -m700 -o postgres -g postgres $TmpInstDir/etc/postgresql
install -d -m700 -o postgres -g postgres $TmpInstDir/etc/postgresql/user.conf.d
install -m600 -o postgres -g postgres synology/*.conf $TmpInstDir/etc/postgresql
install -m600 -o postgres -g postgres synology/user.conf.d/*.conf $TmpInstDir/etc/postgresql/user.conf.d
install -D -m644 synology/postgresql.logrotate $TmpInstDir/etc/logrotate.d/postgresql
install -D -m644 synology/postgresql.syslog-ng $TmpInstDir/etc/syslog-ng/patterndb.d/postgresql.conf

find $TmpInstDir/$LibDir/postgresql/*.so | grep -vE '(plpgsql|libpqwalreceiver|cube)' | xargs rm -f

# do NOT support encoding conversion and snowball text search
echo > $TmpInstDir/$Prefix/share/postgresql/conversion_create.sql
echo > $TmpInstDir/$Prefix/share/postgresql/snowball_create.sql

