#!/bin/bash
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved.
#
# Name - doconfiguregs.sh
#
# Purpose - Install GemStone using a previously created answer file.
#
#=========================================================================

usage(){
    echo "Usage: doinstall.sh <answerFile>"
    exit 1
}

if [ "x$1" = "x" ]; then
    usage
fi
ANSWERARG=$1

if [ -z "$GEMSTONE" ]; then
    echo "[Error]: GEMSTONE env var must be set"
    exit 1
fi

if [ -z "$DEBUG_INSTALLGS" ]; then
    DEBUG_LOG="/dev/null"
else
    DEBUG_LOG="/dev/stdout"
fi

HELPER="${GEMSTONE}/install/helper.sh"
if [ ! -f $HELPER ]; then
    echo "[Error]: Cannot find file $HELPER"
    exit 1
fi

. $HELPER

verifyFileExists $ANSWERARG
$ECHO "[Info]: Starting GemStone installation tasks"
$ECHO "[Info]: Using answer file '$ANSWERARG'"

verifyIsRoot

verifyFileExists $ANSWERARG
. $ANSWERARG

# These must be set in the answer file
verifyNotEmpty "GSUSER" "$GSUSER"
verifyNotEmpty "GSGROUP" "$GSGROUP"
verifyNotEmpty "GSLOCKDIR" "$GSLOCKDIR"

if [ "$OS" = "Linux" ]; then
   # These must be set in the answer file on Linux
  verifyNotEmpty "OOM_PROTECT" "$OOM_PROTECT"
  verifyNotEmpty "LOGIND_PROTECT" "$LOGIND_PROTECT"
fi

# if we have a key file, install it
if [ "x$GSKEY" != "x" ]; then
    verifyFileExists $GSKEY
    validateKeyFile $GSKEY
    if [ -f "$GEMSTONE/sys/gemstone.key" ]; then
	$ECHO "[Warning]: \$GEMSTONE/sys/gemstone.key already exists."
	$ECHO "[Warning]: Key file $GSKEY will NOT be installed."
    else	
	doChmod "" "u+w" "$GEMSTONE/sys"
	doCp "-p" "$GSKEY" "$GEMSTONE/sys/gemstone.key"
	doChmod "" "444" "$GEMSTONE/sys/gemstone.key"
	doChmod "" "u-w" "$GEMSTONE/sys"
	$ECHO "[Info]: Installed key file $GSKEY to \$GEMSTONE/sys/gemstone.key"
    fi
fi

doChown "-R" $GSUSER $GEMSTONE
doChgrp "-R" $GSGROUP $GEMSTONE

# Create the locks and log directories if they do not exist.
makeDir "${GSLOCKDIR}/log"
makeDir "${GSLOCKDIR}/locks"

# Set them owned by root so no one can delete.
# Do not use -R in case we have existent files.
doChown "" root  "$GSLOCKDIR"
doChown "" root  "$GSLOCKDIR/log"
doChown "" root  "$GSLOCKDIR/locks"

# Set group. Do not use -R in case we have existent files
doChgrp "" "$GSGROUP"  "$GSLOCKDIR"
doChgrp "" "$GSGROUP"  "$GSLOCKDIR/log"
doChgrp "" "$GSGROUP"  "$GSLOCKDIR/locks"

# Set perms. Do not use -R in case we have existent files
doChmod "" "775" "$GSLOCKDIR"
doChmod "" "775" "$GSLOCKDIR/log"
doChmod "" "775" "$GSLOCKDIR/locks"

if [ "$UPDATE_SERVICES" = "y" ]; then
    verifyNotEmpty "LDINAME" $LDINAME
    verifyNotEmpty "LDIPORT" $LDIPORT
    while [ 1 ]; do
	matchOnFile "/etc/services" $LDINAME
	if [ $? -eq 1 ]; then
	    $ECHO "[Warning]: /etc/services already contains an entry for $LDINAME. Skipping file update"
	    break
	fi
	matchOnFile "/etc/services" "${LDIPORT}/"
	if [ $? -eq 1 ]; then
	    $ECHO "[Warning]: /etc/services already contains an entry for $LDIPORT. Skipping file update"
	    break
	fi
	
	LINE="$LDINAME\t\t$LDIPORT/tcp\t\t\t# GemStone NetLDI"
#	$ECHO -e "[Debug]: LINE is '$LINE'"
	echo -e "$LINE" >> "/etc/services"
# 	appendFile "/etc/services" "$LINE"
	$ECHO "[Info]: Added this line to /etc/services:"
	echo -e "[Info]: '$LINE'"
	break
    done
fi

if [ "$OS" = "Linux" ]; then
    if [ "$LOGIND_PROTECT" = "y" ]; then
	while [ 1 ]; do
	    if [ ! -f "/etc/systemd/logind.conf" ]; then
		$ECHO "[Warning]: File /etc/systemd/logind.conf does not exist. Skipping file update"
		break
	    fi
	    regexMatchOnFile "/etc/systemd/logind.conf" "^RemoveIPC=no"
	    if [ $? -ge 1 ]; then
		$ECHO "[Warning]: /etc/systemd/logind.conf already contains line 'RemoveIPC=no'. Skipping file update."
		break
	    fi
	    LINE="RemoveIPC=no"
	    appendFile "/etc/systemd/logind.conf" $LINE
	    $ECHO "[Info]: Added this line to /etc/systemd/logind.conf:"
	    $ECHO "[Info]: '$LINE'"
	    break
	done
    fi

    if [ "$OOM_PROTECT" = "y" ]; then
	doSetcap "cap_sys_resource=pe" "$GEMSTONE/sys/pgsvrmain"
	doSetcap "cap_sys_resource=pe" "$GEMSTONE/sys/stoned"
	$ECHO "[Info]: Successfully added OOM killer protections"
    fi
fi

$ECHO "[Info]: Successful GemStone Install!"
exit 0

