#!/bin/bash
################################################################################
# Copyright (c) 2016 VMware, Inc. All rights reserved.
################################################################################
# Wrapper script on top of pg_upgrade aimed at performing in-place upgrades
# for standalone deployments, aka deployments initialized and deployed using
# the standard set of RPMs.
# Before invoking this script, the existing PostgreSQL service needs to be
# stopped. This script needs to be run as root, and invokes a set of script
# with some of them performing operations as the OS user managing the Postgres
# instance.

if [ -z $VMWARE_POSTGRES_BASE ]; then
   echo "VMWARE_POSTGRES_BASE is not set."
   echo "Check your installation."
   exit 1
fi
SANITY_FILE=$VMWARE_POSTGRES_BASE/scripts/vpostgres_sanity_checks
if [ -f $SANITY_FILE ]; then
   source $SANITY_FILE
else
   echo "Sanity check file for environment variables of VMware Postgres"
   echo "is not available. Check your installation."
   exit 1
fi

# Show utility help
show_help()
{
   ERROR_NUM=$1
   echo "Usage: `basename $0` [old_version]"
   echo "Example: Upgrade from 9.4 to latest version"
   echo "`basename $0` 9.4"
   exit $ERROR_NUM
}

EXPECTED_ARGS=1
if [ $# -ne $EXPECTED_ARGS ]; then
   # Leave with an error code
   show_help 1
fi

# Grab the current version of the existing instance
# The old version can be decided freely, as the user may want to upgrade
# from multiple old versions depending on what the existing version of
# the deployed data folder is.
PG_OLD_VERSION=$1
PG_NEW_VERSION=9.4
if [ ${PG_OLD_VERSION} == ${PG_NEW_VERSION} ]; then
   echo "Existing and new versions are matching, no need to do a major upgrade."
   exit 0
fi

OLD_PG_DATA=${VMWARE_POSTGRES_DATA_BASE}/${PG_OLD_VERSION}/pgdata
if [ ! -d ${OLD_PG_DATA} ]; then
   echo "Data folder \"${OLD_PG_DATA}\" to upgrade does not exist"
   exit 1
fi

# Launch the upgrade command
# --pg-svc-name is used to tweak the OS user in charge of running Postgres,
# which is called "postgres" for RPM deployments. Such a deployment does
# not soft-link pg_xlog, and pg_log is let at its default place.
python ${VMWARE_POSTGRES_BIN}/vmw_vpg_config/vmw_vpg_config.py \
       --action upgrade_inplace \
       --pg-data-dir ${OLD_PG_DATA} \
       --pg-log-dir ${OLD_PG_DATA}/pg_log \
       --pg-xlog-dir ${OLD_PG_DATA}/pg_xlog/ \
       --pg-svc-name vmware-postgres

# Standalone deployments include in PGDATA path a hardcoded version
# number. Refresh that as well using the new version number.

# Clean up the previous soft link
rm ${VMWARE_POSTGRES_DATA_BASE}/current

# Remove data folder of old version if any. This is replaced by the new
# one.
rm -rf ${VMWARE_POSTGRES_DATA_BASE}/${PG_NEW_VERSION}

# And now put in place the new soft link
mv ${VMWARE_POSTGRES_DATA_BASE}/${PG_OLD_VERSION} ${VMWARE_POSTGRES_DATA_BASE}/${PG_NEW_VERSION}
ln -s ${VMWARE_POSTGRES_DATA_BASE}/${PG_NEW_VERSION}/ ${VMWARE_POSTGRES_DATA_BASE}/current

exit 0
