#!/bin/bash
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
# This script is run by rpm before the rpm is installed, this means
# it's only run on upgrade because on plain install there is no
# script installed to run.
# 
# It should do any special steps that need to be done for the core 
# components 
# Usage: preinstall install_flag
# Where install_flag is a number that tells you if this is a simple uninstall
# or an upgrade. 
# install_flag == 2 - Upgrade
# install_flag == 1 - Uninstall

#
# Exit codes
#
OK=0
FAIL=1

#
# Check we have the necessary arguments.
# 

if [ $# -lt 1 -o $# -gt 1 ]
then
    echo "Usage: $0 install_flag"
    exit $FAIL
fi

if [ -z "${INSTDIR}" ]
then
    echo "INSTDIR not set"
    exit $FAIL
fi

INSTALL_FLAG="$1"

if [ ${INSTALL_FLAG} -ne 2 ]
then
    echo "Invalid install_flag: ${INSTALL_FLAG}"
    exit $FAIL
fi

# Keep a note of our previous version before we over write it
cp "${INSTDIR}/etc/version" "${INSTDIR}/etc/version.prev"

