#!/bin/bash
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
# This script is run by rpm before the rpm is removed 
# It should do any special steps that need to be done for systemd
# for a plain uninstall or the uninstall part of an upgrade.
# Usage: preuninstall install_flag
# Where install_flag is a number that tells you if this is a simple uninstall
# or an upgrade. 
# install_flag == 1 - Upgrade
# install_flag == 0 - 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

INSTALL_FLAG="$1"
UPGRADE="false"

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

if [ ${INSTALL_FLAG} -eq 1 ]
then
    UPGRADE="true"
fi

if ! "${UPGRADE}"
then
    # Disable and remove our sgd service ignoring any errors
    systemctl disable sgd &> /dev/null
fi 
