#!/bin/sh
#
# postgresql-setup	Initialization and upgrade operations for PostgreSQL

# PGVERSION is the full package version, e.g., 9.3.0
# Note: the specfile inserts the correct value during package build
PGVERSION=9.3.24
# PGMAJORVERSION is major version, e.g., 9.3 (this should match PG_VERSION)
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
# PGENGINE is the directory containing the postmaster executable
# Note: the specfile inserts the correct value during package build
PGENGINE=/usr/pgsql-9.3/bin
# PREVMAJORVERSION is the previous major version, e.g., 9.1, for upgrades
PREVMAJORVERSION=9.1
# PREVPGENGINE is the directory containing the previous postmaster executable
PREVPGENGINE=/usr/pgsql-$PREVMAJORVERSION/bin

# The second parameter is the new database version, i.e. $PGMAJORVERSION in this case.
# Use  "postgresql-$PGMAJORVERSION" service, if not specified.
SERVICE_NAME="$2"
if [ x"$SERVICE_NAME" = x ]
then
    SERVICE_NAME=postgresql-$PGMAJORVERSION
fi

# The third parameter is the old database version, i.e. $PREVMAJORVERSION in this case.
# Use  "postgresql-$PREVMAJORVERSION" service, if not specified.
OLD_SERVICE_NAME="$3"
if [ x"$OLD_SERVICE_NAME" = x ]
then
    OLD_SERVICE_NAME=postgresql-$PREVMAJORVERSION
fi

# Find the unit file for new version.
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]
then
    SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
elif [ -f "/lib/systemd/system/${SERVICE_NAME}.service" ]
then
    SERVICE_FILE="/lib/systemd/system/${SERVICE_NAME}.service"
else
    echo "Could not find systemd unit file ${SERVICE_NAME}.service"
    exit 1
fi

# Log file for pg_upgrade
PGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade.log
# Log file for initdb
PGLOG=/var/lib/pgsql/9.3/initdb.log

# Get port number and data directory from the service file
PGPORT=`sed -n 's/Environment=PGPORT=//p' "${SERVICE_FILE}"`
PGDATA=`sed -n 's/Environment=PGDATA=//p' "${SERVICE_FILE}"`

export PGPORT
export PGDATA

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi

script_result=0

# code shared between initdb and upgrade actions
perform_initdb(){
	if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
	then
		mkdir -p "$PGDATA" || return 1
		chown postgres:postgres "$PGDATA"
		chmod go-rwx "$PGDATA"
	fi
	# Clean up SELinux tagging for PGDATA
	[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"

	# Create the initdb log file if needed
	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
	then
		touch "$PGLOG" || return 1
		chown postgres:postgres "$PGLOG"
		chmod go-rwx "$PGLOG"
		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
	fi

	# Initialize the database
	$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null

	# Create directory for postmaster log files
	mkdir "$PGDATA/pg_log"
	chown postgres:postgres "$PGDATA/pg_log"
	chmod go-rwx "$PGDATA/pg_log"

	if [ -f "$PGDATA/PG_VERSION" ]
	then
	    return 0
	fi
	return 1
}

initdb(){
    if [ -f "$PGDATA/PG_VERSION" ]
    then
	echo $"Data directory is not empty!"
	echo
	script_result=1
    else
	echo -n $"Initializing database ... "
	if perform_initdb
	then
	    echo $"OK"
	else
	    echo $"failed, see $PGLOG"
	    script_result=1
	fi
	echo
    fi
}

upgrade(){

## Absorb configuration settings from the specified systemd service files.

# Do the same for the old PostgreSQL version.
if [ -f "/etc/systemd/system/${OLD_SERVICE_NAME}.service" ]
then
    OLD_SERVICE_FILE="/etc/systemd/system/${OLD_SERVICE_NAME}.service"
elif [ -f "/lib/systemd/system/${OLD_SERVICE_NAME}.service" ]
then
    OLD_SERVICE_FILE="/lib/systemd/system/${OLD_SERVICE_NAME}.service"
else
    echo "Could not find systemd unit file ${OLD_SERVICE_NAME}.service"
    exit 1
fi

## Get port number and data directory from the service file
NEWPGPORT=`sed -n 's/Environment=PGPORT=//p' "${SERVICE_FILE}"`
NEWPGDATA=`sed -n 's/Environment=PGDATA=//p' "${SERVICE_FILE}"`

## Get port number and data directory from the service file
OLDPGPORT=`sed -n 's/Environment=PGPORT=//p' "${OLD_SERVICE_FILE}"`
OLDPGDATA=`sed -n 's/Environment=PGDATA=//p' "${OLD_SERVICE_FILE}"`

# must see previous version in PG_VERSION
    if [ ! -f "$OLDPGDATA/PG_VERSION" -o \
	 x`cat "$OLDPGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
    then
	echo
	echo $"Cannot upgrade because database is not of version $PREVMAJORVERSION."
	echo
	exit 1
    fi

    if [ ! -x "$PGENGINE/pg_upgrade" ]
    then
	echo
	echo $"Please install the postgresql92-contrib RPM."
	echo
	exit 5
    fi

# Perform initdb on the new server
$PGENGINE/postgresql92-setup initdb
RETVAL=$?
if [ $RETVAL -ne 0 ]
  then
	echo "initdb failed!"
	exit 1
fi

# Check the clusters first, without changing any data:
su -l postgres -c "$PGENGINE/pg_upgrade -b $PGPREVENGINE -B $PGENGINE/ -d $OLDPGDATA -D $NEWPGDATA -p $OLDPGPORT -P $NEWPGPORT -c"
RETVAL=$?
if [ $RETVAL -eq 0 ]
  then
	echo "Clusters checked successfully, proceeding with upgrade from $PREVMAJORVERSION to $PGMAJORVERSION"
	echo "Stopping old cluster"
	/bin/systemctl stop $OLD_SERVICE_NAME.service
	#/sbin/service $OLD_INIT_SCRIPT stop

	# Set up log file for pg_upgrade
	rm -f "$PGUPLOG"
	touch "$PGUPLOG" || exit 1
	chown postgres:postgres "$PGUPLOG"
	chmod go-rwx "$PGUPLOG"
	[ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"

	echo "Performing upgrade"
	su -l postgres -c "$PGENGINE/pg_upgrade \
		-b $PGPREVENGINE -B $PGENGINE/ \
		-d $OLDPGDATA -D $NEWPGDATA -p $OLDPGPORT -P $NEWPGPORT" >> "$PGUPLOG" 2>&1 < /dev/null
  else
	echo "Cluster check failed. Please see the output above."
	exit 1
fi
	echo

exit 0

}

# See how we were called.
case "$1" in
  initdb)
	initdb
	;;
  upgrade)
	upgrade
	;;
  *)
	echo $"Usage: $0 {initdb|upgrade} [ service_name ]"
	exit 2
esac

exit $script_result
