#!/bin/bash
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
#
# Upgrade information shell API

# Global input params:
# TTAINSTDIR - the SGD installation directory

SGD_UPGRADE_FILE=${TTAINSTDIR}/var/info/upgrade
SGD_OWNER=ttasys
SGD_GROUP=ttaserv

### External API

# Get the current upgrade state.
# Output:
#   HOT   - Any upgrades have been applied and are running.
#   WARM  - Upgrades have been applied, but may not yet be running (unused).
#   COLD  - Upgrades have been applied and a restart is required.
function sgd_upgrade_state_get()
{
    if [ -r "${SGD_UPGRADE_FILE}" ]
    then
        cat "${SGD_UPGRADE_FILE}"
    else
        echo "HOT"
    fi
}

# Set the current upgrade state
# $1 - The new upgrade state (HOT, COLD or WARM). See sgd_upgrade_state_get().
function sgd_upgrade_state_set()
{
    (umask 077; echo -n "$1" > "${SGD_UPGRADE_FILE}")
    chown ${SGD_OWNER}:${SGD_GROUP} "${SGD_UPGRADE_FILE}" 2> /dev/null
}


# Initialize the upgrade state to HOT
function sgd_upgrade_state_init()
{
    sgd_upgrade_state_set HOT
}

# Clean up the upgrade state
function sgd_upgrade_state_terminate()
{
    rm -f "${SGD_UPGRADE_FILE}"
}


