#! /bin/bash
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved.
#
# Name - configuregs.sh
#
# Purpose - Create answer file /var/tmp/gsanswers.txt that will be
#           used by $GEMSTONE/install/doconfiguregs to configure GemStone.
#           Then invoke doconfiguregs to perform the actuall installation.
#=========================================================================

if [ -z "$GEMSTONE" ]; then
    echo "GEMSTONE 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 $HELPER"
    exit 1
fi

. $HELPER

verifyIsRoot

$ECHO "configuregs -- Configure GemStone/S 64 Bit"
$ECHO ""
$ECHO "This script will walk you through the process of configuring your host for GemStone."
$ECHO ""
$ECHO "Whenever asked to answer 'yes' or 'no', answer with a 'y' or an 'n'."
$ECHO "If a default answer is available, it is shown in square brackets like this: ['default']"
$ECHO "Typing <enter> accepts the default value."
$ECHO ""
$ECHO "Now we begin the configuration process...."

if [ -f "$ANSWERS" ]; then
    $ECHO "[Warning]: Answer file $ANSWERS already exists."
    $ECHO "[Warning]: Moving old answers file to $ANSWERSBACKUP"
    rm -fr $ANSWERSBACKUP >/dev/null
    mv $ANSWERS $ANSWERSBACKUP
fi

verifyFileNotExists $ANSWERS
touch $ANSWERS
verifyFileExists $ANSWERS

while [ 1 ]; do
    readResponse "Enter the user that will own GEMSTONE (required, must exist and not be root): " ""
    GSUSER=$ARG
    checkUserExistsNotRoot $GSUSER
    if [ $? -eq 1 ]; then
	break
    fi  
done

while [ 1 ]; do
    readResponse "Enter the group that will own GEMSTONE (required, must exist): " ""
    GSGROUP=$ARG
    checkGroupExists $GSGROUP
    if [ $? -eq 1 ]; then
	break
    fi  
done

while [ 1 ]; do
    readResponse "Enter a key file to use with GEMSTONE (optional): " ""
    GSKEY=$ARG
    if [ "x$GSKEY" != "x" ]; then
	checkFileExists $GSKEY
	if [ $? -eq 1 ]; then
	    validateKeyFile $GSKEY
	    if [ $? -eq 1 ]; then
		break
	    fi
	fi
    else
	break
    fi
done

readYesNoResponse "Should the netldi and port number be added to /etc/services ?: " "n"
UPDATE_SERVICES=$ARG

LDINAME=""
LDIPORT=""
if [ "$UPDATE_SERVICES" = "y" ]; then
    readResponse "Enter the netldi name to add to /etc/services: " "gs64ldi"
    LDINAME=$ARG
    while [ 1 ]; do
      readResponse "Enter the netldi port number to add to /etc/services: " "50377"
      LDIPORT=$ARG
      checkPort "LDIPORT" $LDIPORT
      if [ $? -eq 1 ]; then
	  break
      fi      
    done 
fi

if [ "$OS" = "Linux" ]; then
    readYesNoResponse "Protect GemStone processes from the Linux Out of Memory killer?: " "y"
    OOM_PROTECT=$ARG

    readYesNoResponse "Update /etc/systemd/logind.conf to prevent killing GemStone at logout?: " "y"
    LOGIND_PROTECT=$ARG
fi

$ECHO "[Info]: Writing answers to answer file '$ANSWERS'"

$ECHO "GSUSER=\"$GSUSER\"" >>$ANSWERS
$ECHO "GSGROUP=\"$GSGROUP\"" >>$ANSWERS
$ECHO "GSKEY=\"$GSKEY\"" >>$ANSWERS
$ECHO "GSLOCKDIR=\"$GSLOCKDIR_DEFAULT\"" >>$ANSWERS
$ECHO "UPDATE_SERVICES=\"$UPDATE_SERVICES\""  >>$ANSWERS
$ECHO "LDINAME=\"$LDINAME\"" >>$ANSWERS
$ECHO "LDIPORT=\"$LDIPORT\"" >>$ANSWERS
$ECHO "OOM_PROTECT=\"$OOM_PROTECT\"" >>$ANSWERS
$ECHO "LOGIND_PROTECT=\"$LOGIND_PROTECT\"" >>$ANSWERS

$GEMSTONE/install/doconfiguregs $ANSWERS
exit $?







