#!/bin/bash
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
# This script is run by rpm after the rpm is installed.
# It should do any special setup that needs to be done for the ens
# components for an install or an upgrade.
# Usage: postinstall install_flag
# Where install_flag is a number that tells you if this is a fresh installation
# or an upgrade. 
# install_flag == 1 - Initial install
# install_flag == 2 - Upgrade

#
# 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"
UPGRADE="false"
if [ ${INSTALL_FLAG} -eq 2 ]
then
    UPGRADE="true"
elif [ ${INSTALL_FLAG} -ne 1 ]
then
    echo "Invalid install_flag: ${INSTALL_FLAG}"
    exit $FAIL
fi

ENS_TEMPLATES="${INSTDIR}/etc/templates/var/ens"
VAR_DIR="${INSTDIR}/var"
ENS_DIR="${VAR_DIR}/ens"

MODE=027

if [ ! -d "${ENS_DIR}" ]
then
    umask $MODE
    cp -pr "${ENS_TEMPLATES}" "${VAR_DIR}"
fi

exit $OK
