#!/bin/sh
sleep 2
lsv_dir=`dirname $0`
cd $lsv_dir
#
. ./go.sys
. ./go.user

#
MAILER=$MAILER"@"$NODE
STOREPW=$CREATEPW
#
export A D Y MAXBSMTP SMTP_HOSTNAME SMTP_PORT
export NJEDOMS PRIMETIME TRAPIN TRAPOUT USERID NODE CREATEPW LOCAL MAILER
export MYDOMAIN POSTMASTER STOREPW TZONE STARTMSG S BITNET_ROUTE MYORG
export DBRINDEX FILTER_ALSO SMTP_FORWARD WWW_ARCHIVE_CGI WWW_ARCHIVE_DIR
export SORT_RECIPIENTS DIST_ALLOWED_USERS BOUNCES_TO SMTP_FORWARD_1
export LOG_PATH LISTSERV_EXIT_STATUS_FILE OLD_WEB_INDEXES 
export WWW_ALLOW_LEGACY_INTERFACE

# Define location of the exit status file
LISTSERV_EXIT_STATUS_FILE="$A/crash.status"

# Substitute /usr/ucb/whoami under Solaris
THISUSER=`whoami | tr '[a-z]' '[A-Z]'`
USERID=`echo $USERID | tr '[a-z]' '[A-Z]'`
not_LSV="$USERID != $THISUSER"
userid=`echo $USERID | tr '[A-Z]' '[a-z]'`
#
# Re-run under correct userid if appropriate
if [ $not_LSV ]
then
	su $userid -c "$0 $1 $2"
	exit $?
fi
#
lsvexec=""
lsvlog=""
lsvdet=""
notty="?"
#
if [ "$1" = "notty" ]
then
    is_bg=$2
    notty="Y"
else
    is_bg=$1
    if [ "$2" = "notty" ]
    then
        notty="Y"
    fi
fi
#
if [ "$notty" = "Y" ]
then
    lsvexec="exec"
fi
#
if [ "$is_bg" = "bg" -o "$is_bg" = "background" ]
then
    echo "> Starting LISTSERV as a background process"
    if [ "$LOG_PATH" ]
    then
        if [ -x "./lsv-logger.pl" ]
        then
            lsvlog="< /dev/null 2>&1 | ./lsv-logger.pl $LOG_PATH"
        else
            lsvlog="2>&1 >>$LOG_PATH/listserv-`date +%Y%m%d`.log"
        fi
    else
        if [ -s listserv.log ]
        then
            mv listserv.log listserv.log.OLD
        fi
        lsvlog="2>&1 >listserv.log"
    fi
    if [ "$notty" != "Y" ]
    then
        lsvdet="&"
    fi
fi
#
tty -s || {
    lsvdet=""
    lsvexec="exec"
}



$lsvexec sh -c "exec ./go2 $lsvlog $lsvdet"
