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

if [ "x$NOSTRIP" = "xNOSTRIP" ]; then
	make DESTDIR=$TmpInstDir install
	make -C contrib/repmgr DESTDIR=$TmpInstDir install
	make -C contrib/tablefunc DESTDIR=$TmpInstDir install
	#make -C contrib/dblink DESTDIR=$TmpInstDir install
else
	make DESTDIR=$TmpInstDir install-strip
	make -C contrib/repmgr DESTDIR=$TmpInstDir install-strip
	make -C contrib/tablefunc DESTDIR=$TmpInstDir install-strip
	#make -C contrib/dblink 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/etc/init
install -d -m755 $TmpInstDir/usr/syno/etc/synoservice.d/
install -d -m755 $TmpInstDir/usr/syno/etc/rc.sysv/
install -d -m755 $TmpInstDir/usr/syno/bin/
install -m755 synology/pgsql.sh $TmpInstDir/usr/syno/etc/rc.sysv/pgsql.sh
install -m644 synology/pgsql-adapter.upstart $TmpInstDir/etc/init/pgsql-adapter.conf
install -m644 synology/pgsql.upstart $TmpInstDir/etc/init/pgsql.conf
install -m644 synology/pgsql.servicecfg $TmpInstDir/usr/syno/etc/synoservice.d/pgsql.cfg
install -m755 synology/synopsql $TmpInstDir/usr/syno/bin/

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/usr/libexec/timezone_change/
install -m755 synology/pgsql_timezone_change.sh $TmpInstDir/usr/libexec/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

# keep `plpgsql', `dblink' and `libpqwalreceiver'
find $TmpInstDir/$LibDir/postgresql/*.so | grep -vE '(plpgsql|dblink|libpqwalreceiver)' | 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

