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

shopt -s extglob

if [ "x$NOSTRIP" = "xNOSTRIP" ]; then
	make DESTDIR=$TmpInstDir install
	make -C contrib/cube DESTDIR=$TmpInstDir install
else
	make DESTDIR=$TmpInstDir install-strip
	make -C contrib/cube DESTDIR=$TmpInstDir install-strip
fi

rm -rf $TmpInstDir/usr/share/pgsql/9.3/bin/{ecpg,pg_config,*lang}
rm -rf $TmpInstDir/usr/share/pgsql/9.3/lib/{*.*a,pkgconfig,libecpg*,libpgtypes*,pgxs}
rm -rf $TmpInstDir/usr/share/pgsql/9.3/{include,man}
rm -rf $TmpInstDir/usr/share/pgsql/9.3/share/!(extension)

# keep necessary binary files for upgrade
find $TmpInstDir/usr/share/pgsql/9.3/bin/* | grep -vE '(createuser|pg_controldata|pg_ctl|pg_dump*|post*)' | xargs rm -f
ln -sf /usr/share/postgresql/timezonesets $TmpInstDir/usr/share/pgsql/9.3/share
find $TmpInstDir/usr/share/pgsql/9.3/lib/* -name "*.so" | grep -vE '(libpq|plpgsql|dblink|libpqwalreceiver|cube)' | xargs rm -f

install -d -m700 -o postgres -g postgres $TmpInstDir/usr/share/pgsql/9.3/etc/postgresql
install -m600 -o postgres -g postgres synology/*.conf $TmpInstDir/usr/share/pgsql/9.3/etc/postgresql

