#!/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 systemd
# 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

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

if ! ${UPGRADE}
then
    # enable sgd service (but doesn't start it)
    # The sgd.service unit file should already be installed
    # by the RPM
    systemctl enable sgd &> /dev/null
fi

exit $OK
