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

if [ -z "${PRIV_USER}" ]
then
    echo "PRIV_USER 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

scriptdir="${INSTDIR}/bin/scripts/install_scripts"
customsshfile="custom_ssh"
customssh="${scriptdir}/${customsshfile}"

# Source any custom ssh path that's been set
. ${customssh}

# Stomp the ttasshhelper with the ssh client path and privileged user
sshhelper="${INSTDIR}/bin/bin/ttasshhelper"
sshhelperstomp="${INSTDIR}/bin/bin/ttasshhelperstomp"

"${sshhelperstomp}" --file "${sshhelper}" --username "${PRIV_USER}"

exit $OK
