#!/usr/bin/bash
# Copyright 2011-2024 Chad Lemmen http://www.lemmen.com
#################################################################################
# File:     stansoft 
# Date:     2011-01-17
# Author:   Chad Lemmen http://www.lemmen.com
# Purpose:  Startup script for Stansoft.
# History:
#           2024-02-28 CDL - Added error message to set PGDIR 
#
################################################################################


# This works even if a link to this script is used
ssdir=$(dirname $(realpath $0))

# Set xterm title
unset PROMPT_COMMAND
printf "\033]0;Stansoft\007"
#echo -ne "\033]2; Stansoft \007"
#export PS1='\e]0;Stansoft\a[\u@\h \W]\$ '
# Set xterm cursor color
# This makes the character under the cursor visible in reverse video
echo -ne "\033]12;Gray\007"


use_postgres() {
  # Set the PostgreSQL environment
  . "$ssdir"/etc/postgres.sh

  # Check for the PGDATA structure
  # only user stansoft can access the data directory from the shell
  #if ! [ -d "$PGDATA/base" ]; then
    # PGDATA does not exist. Warn the user to run install.
  #  echo "Directory \"$PGDATA\" is missing or empty."
  #  echo "Use \"$ssdir/installss\""
  #  echo "to initialize and create the database."
  #  echo "See \"$ssdir/README\" for more information."
  #  exit 1
  #fi

  # Make sure the Postgres database server is running
  # pg_ctl: cannot be run as root
  # pg_ctl status >/dev/null
  psql -l >/dev/null # we want errors to show

  if [ $? -ne 0 ]; then
    echo
    echo "If you are using the system installed PostgreSQL package,"
    echo "set PGDIR=/usr/lib/postgresql/14 to the exec prefix"
    echo "of the psql command."
    echo 
    echo "If you receive an error that a role does not exist,"
    echo "you must add the new user with user stansoft"
    echo
    echo "The PostgreSQL database server may not be running."
    echo "Start it by running \"$ssdir/etc/rc.postgresql start\""
    echo "or \"systemctl start stansoft\""
    echo
    echo "If you are using Informix then you must set INFORMIXDIR"
    echo "Example: export INFORMIXDIR=/opt/informix"
    echo
    echo "If this is a new install, run \"$ssdir/installss\""
    echo "to initialize and create the database."
    echo "This will create the database in directory \"$PGDATA\""
    exit 1
  fi
}


getstate() {
  onstat -  >/dev/null 2>&1
  engine_state=$?
}


use_informix() {
  # Set Informix environment
  . $INFORMIXDIR/ol_informix*.sh
  . "$ssdir"/etc/stansoft.sh

  # Check that the database engine is up so the user knows right away
  # why it's not starting.
  #engine_up=$(onstat - | grep "Up" | wc -l)
  getstate
  if [ $engine_state != 5 ]; then
    echo "The database engine is down, to start it set the environment with"
    echo ". \$INFORMIXDIR/ol_informix.sh (make sure to include the dot space)"
    echo "then run: /opt/stansoft/etc/rc.informix start"
    exit 1
  fi
}


if [ -z "$INFORMIXDIR" ]; then
  use_postgres
else
  use_informix
fi

if ! [ -d "$AUBITDIR" ]; then
  if [ -d /usr/lib64/aubit4gl ]; then
    export AUBITDIR=/usr/lib64/aubit4gl
  else
    echo "You must set environment variable AUBITDIR"
    exit 1
  fi
fi

if [ -z "$SSMENU" ]; then
  # No menu set, default to the main menu
  SSMENU=MM
fi
menu.4ge $SSMENU

