#!/bin/sh
# Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
#
##########################################
##########################################

# check we have privileged user/group
HOMEBASE="/home"
[ -d "/export/home" ] && HOMEBASE="/export/home"

privusr="temsys"
privgrp="temserv"


# Create tem group
getent group "${privgrp}" >/dev/null || groupadd "${privgrp}"

# Create tem user
getent passwd "${privusr}" >/dev/null || useradd -d "${HOMEBASE}/${privusr}" -m "${privusr}"

# Lock user account
passwd -l "${privusr}" > /dev/null 2>&1

# Make home directory writeable
mkdir -p "${HOMEBASE}/${privusr}"
chown "${privusr}" "${HOMEBASE}/${privusr}"
chmod 700 "${HOMEBASE}/${privusr}"

# backup the properties files...
if [ -s "${INSTDIR}/etc/pkg/temversion" ] 
then

    # an upgrade, we need to find the properties files and 
    # back them up
    BACKUP_DIR="${INSTDIR}/var/serverconfig/local/save"
    PROP_DIR="${INSTDIR}/var/serverconfig/local"
    CONF_DIR="${INSTDIR}/etc"

    if [ -d "${BACKUP_DIR}" ] 
    then 
            rm -rf ${BACKUP_DIR}
    fi 

    mkdir -p ${BACKUP_DIR}

    # backup properties file(s)
    if [ -f "${PROP_DIR}/tier3loadbalancing.properties" ] 
    then
            echo "*** Backing up $PROP_DIR/tier3loadbalancing.properties file ***" >> $LOGFILE 
            cp ${PROP_DIR}/tier3loadbalancing.properties ${BACKUP_DIR}
    fi 

    # backup config file(s)
    if [ -f "${CONF_DIR}/client.prf" ] 
    then
            echo "*** Backing up $CONF_DIR/client.prf file ***" >> $LOGFILE 
            cp ${CONF_DIR}/client.prf ${BACKUP_DIR}
    fi 
fi

if [ "`uname -s`" -eq "SunOS" ]; then
    chown ${privusr}:${privgrp} "${INSTDIR}"
fi
