#!/usr/bin/env bash

# Copyright Notice:
# © (C) Postgres Professional 2015-2016 http://www.postgrespro.ru/
# Distributed under Apache License 2.0
# Распространяется по лицензии Apache 2.0

set -xe
set -o pipefail

# fix https://github.com/moby/moby/issues/23137
ulimit -n 1024

# currenctly we do not build std|ent packages for Suse
if [[ ${PBK_EDITION} != '' ]] ; then
	exit 0
fi

PG_TOG=$(echo $PG_VERSION | sed 's|\.||g')

if [ ${PG_TOG} == '13' ]; then # no packages for PG13 
    exit 0
fi

if [ ${PG_TOG} == '11' ]; then # no packages for PG11
    exit 0
fi

if [ ${PG_TOG} == '95' ]; then # no packages for PG95 
    exit 0
fi

zypper install -y nginx
if ! getent group nginx > /dev/null 2>&1 ; then
    addgroup --system --quiet nginx
fi
if ! getent passwd nginx > /dev/null 2>&1 ; then
    adduser --quiet \
        --system --disabled-login --ingroup nginx \
        --home /var/run/nginx/ --no-create-home \
        nginx
fi

useradd postgres

cat <<EOF > /etc/nginx/nginx.conf
user nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log;
events {
    worker_connections  1024;
}
http {
    server {
        listen   80 default;
        root /app/www;
    }
}
EOF
nginx -s reload || (pkill -9 nginx || nginx -c /etc/nginx/nginx.conf &)

# install POSTGRESQL
zypper install -y postgresql${PG_TOG} postgresql${PG_TOG}-server postgresql${PG_TOG}-contrib
export PGDATA=/tmp/data

# install old packages
zypper install --allow-unsigned-rpm -y http://repo.postgrespro.ru/pg_probackup/keys/pg_probackup-repo-${DISTRIB}.noarch.rpm
zypper --gpg-auto-import-keys install -y ${PKG_NAME}
${PKG_NAME} --help
${PKG_NAME} --version

su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/initdb -k -D ${PGDATA}"
echo "fsync=off" >> ${PGDATA}/postgresql.auto.conf
echo "wal_level=hot_standby" >> ${PGDATA}/postgresql.auto.conf
echo "archive_mode=on" >> ${PGDATA}/postgresql.auto.conf
echo "archive_command='${PKG_NAME} archive-push --no-sync -B /tmp/backup --instance=node --wal-file-path %p --wal-file-name %f'" >> ${PGDATA}/postgresql.auto.conf
su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pg_ctl start -D ${PGDATA}"
sleep 5

su postgres -c "${PKG_NAME} init -B /tmp/backup"
su postgres -c "${PKG_NAME} add-instance --instance=node -B /tmp/backup -D ${PGDATA}"
su postgres -c "${PKG_NAME} backup --instance=node --compress -b full -B /tmp/backup -D ${PGDATA} --no-sync"
su postgres -c "${PKG_NAME} show --instance=node -B /tmp/backup -D ${PGDATA} --archive"

su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pgbench --no-vacuum -i -s 5"
su postgres -c "${PKG_NAME} backup --instance=node -b page -B /tmp/backup -D ${PGDATA} --no-sync"

# install new packages
echo "127.0.0.1 repo.postgrespro.ru" >> /etc/hosts
zypper clean all -y

sed -i "s/https/http/g" /etc/zypp/repos.d/pg_probackup.repo

zypper update -y ${PKG_NAME}
${PKG_NAME} --help
${PKG_NAME} --version

su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pgbench --no-vacuum -t 1000 -c 1"
su postgres -c "${PKG_NAME} backup --instance=node -b page -B /tmp/backup -D ${PGDATA} --no-sync"

su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pgbench --no-vacuum -t 1000 -c 1"
su postgres -c "${PKG_NAME} backup --instance=node -b page -B /tmp/backup -D ${PGDATA} --no-sync"
su postgres -c "${PKG_NAME} show --instance=node -B /tmp/backup -D ${PGDATA}"

su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pg_ctl stop -D ${PGDATA}"
rm -rf ${PGDATA}

su postgres -c "${PKG_NAME} restore --instance=node -B /tmp/backup -D ${PGDATA} --no-sync"
su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/pg_ctl start -w -D ${PGDATA}"

sleep 5

echo "select count(*) from pgbench_accounts;" | su postgres -c "/usr/lib/postgresql${PG_TOG}/bin/psql" || exit 1

exit 0

# SRC PACKAGE
cd /mnt
yum install yum-utils rpm-build -y
yumdownloader --source ${PKG_NAME}
rpm -ivh ./*.rpm
cd /root/rpmbuild/SPECS
exit 0

# build pg_probackup
yum-builddep -y pg_probackup.spec
rpmbuild -bs pg_probackup.spec
rpmbuild -ba pg_probackup.spec #2>&1 | tee -ai /app/out/build.log
