#!/bin/sh
#
# $Id: start,v 1.36 1998/06/02 18:45:34 grubba Exp $

VERSION=202; BETA=.b;

# Can be set with '--config-dir=DIR'
DIR=../configurations/
LOGDIR=../logs/
FILES="default"
program=base_server/roxenloader.pike

# Enable threads (if available) on Solaris.
# Most other OS's have thread bugs that cause them or Roxen to crash.
if uname | grep 'SunOS' >/dev/null 2>&1; then
  if uname -r | grep '5\.[5-9]' >/dev/null 2>&1; then
    echo 'Solaris 2.5 or later detected. Enabling threads (if available).'
    DEFINES="$DEFINES -DENABLE_THREADS"
  fi
fi

# Roxen will create files as the initial user,
# which it expects to be able to read as the run-time user.
umask 022

# Pike default Master-program
if [ "x$PIKE_MASTER" = "x" ]; then
  if [ -f lib/master.pike ]; then
    DEFINES="$DEFINES -mlib/master.pike -Ilib/include"
    PIKE_MODULE_PATH="$PIKE_MODULE_PATH:lib/modules"
  elif [ -f lib/pike/master.pike ]; then
    DEFINES="$DEFINES -mlib/pike/master.pike -Ilib/pike/include"
    PIKE_MODULE_PATH="$PIKE_MODULE_PATH:lib/pike/modules"
  else
    # This is used with localinstall
    if [ -f ../pike/src/lib/master.pike ]; then
      DEFINES="$DEFINES -m../pike/src/lib/master.pike -I../pike/src/lib/include"
      PIKE_MODULE_PATH="$PIKE_MODULE_PATH:../pike/src/lib/modules"
      if [ -d ../pike/src/share/modules/. ]; then
        DEFINES="$DEFINES -I../pike/src/share/include"
        PIKE_MODULE_PATH="$PIKE_MODULE_PATH:../pike/src/share/modules"
      fi
    fi
  fi
  export PIKE_MODULE_PATH
else
  # This is usefull when using several different Pikes.
  # Specify include and module paths with
  # PIKE_INCLUDE_PATH and PIKE_MODULE_PATH
  # they are handled automatically by the master,
  # so no need to do it here.
  DEFINES="$DEFINES -m$PIKE_MASTER"
fi

# Extra module-path
if [ -d etc/modules ]; then
  DEFINES="$DEFINES -Metc/modules"
fi

# Extra include-path
if [ -d etc/include ]; then
  DEFINES="$DEFINES -Ietc/include"
fi

# Extra include-path (2)
if [ -d base_server ]; then
  DEFINES="$DEFINES -Ibase_server"
fi

# Extra program-path
DEFINES="$DEFINES -P`pwd`"

# Extra kludge for HPUX
# HPUX doesn't like group 60001(nobody)
if uname | grep 'HP-UX' >/dev/null 2>&1; then
  echo 'WARNING: Applying kludge for HPUX. (see base_server/privs.pike)'
  DEFINES="$DEFINES -DHPUX_KLUDGE"
fi

pike=pike
if [ -x bin/pike ] ; then pike=bin/pike; fi
if [ -x bin/roxen ] ; then  pike=bin/roxen; fi
if [ -x "$PIKE" ]; then pike="$PIKE"; fi

gdb=no

rotate () {
  b=5;
  for a in 4 3 2 1 ; do mv -f $1.$a $1.$b 2> /dev/null;  b=$a; done
}

start_roxen() {
  args="-DROXEN $DEBUG $DEFINES $pass $program --config-dir=$DIR"
  if [ "x$gdb" = "xno" ]; then
    echo Executing $pike $args $@
    $pike $args $@
  else
    echo Executing gdb $pike $args $@
    echo >.gdbinit handle SIGPIPE nostop noprint pass
    echo >>.gdbinit run $args $@
    gdb $pike
    rm .gdbinit
  fi
}
####### END PREAMBLE

## Parse all arguments.
## GNU-style, long options only, except for -D, simply passed on.
parse_args() {
 while [ ! c"$1" = "c" ] ; do
  case $1 in
  -D*)
   DEFINES="$DEFINES $1"
  ;;
# Used by the 'install' script
  --log-dir=*)
   LOGDIR=`echo $1 | sed -e 's/--log-dir=//'`
  ;;
  --config-dir=*)
   DIR=`echo $1 | sed -e 's/--config-dir=//'`
   FILES=`echo $1 | sed -e's/--config-dir=//' -e's/\.//g' -e's./..g' -e 's.-..g'`
  ;;
  '--debug')
    debug=1
  ;;
  '--fd-debug')
    DEFINES="-DFD_DEBUG $DEFINES"
  ;;
  '--threads')
    DEFINES="-DENABLE_THREADS $DEFINES"
  ;;
  '--no-threads')
    DEFINES="`echo $DEFINES | sed -e 's/-DENABLE_THREADS//'`"
  ;;
  '--profile')
    DEFINES="-DPROFILE $DEFINES"
  ;;
  '--keep-alive')
    DEFINES="-DKEEP_ALIVE $DEFINES"
  ;;
  '--once')
    once=1
  ;;
  '--gdb')
    gdb=gdb
    once=1
  ;;
  '--program')
    program="$2"
    shift
  ;;
  '--version')
    echo Roxen Challenger 1.`expr $VERSION / 100`$BETA`expr $VERSION % 100`
    exit 0
  ;;
  '--help'|'-?')
  tput 'bold' 2>/dev/null
  cat << EOF
Syntax: $0 [--version] [--config-dir=DIR] [--log-dir=DIR] [-DDEFINE] [--threads] [--no-threads] [--profile] [--keep-alive] [--once] [--gdb] [--program=PROGRAM] [--help]
This command will start the Roxen server. 
If the configuration dir is set, a different set of debuglogfiles
will be used.

The environment variable ROXEN_ARGS can be used to specify
the default arguments.
EOF
    tput 'rmso' 2>/dev/null
    exit 0
   ;;
  *)
    pass="$pass $1"
   ;;
  esac
  shift
 done
}

####### END PREAMBLE

parse_args $@

if [ ! "X$ROXEN_ARGS" = "X" ]; then
  echo $$: Using $ROXEN_ARGS from ROXEN_ARGS.
  parse_args $ROXEN_ARGS
fi

if [ ! "X$pass" = "X" ] ; then set -- $pass ;fi


echo $$: Starting the Roxen Challenger World Wide Web server.

./mkdir -p $LOGDIR/debug/

if [ -z "$debug" ] ; then
  DEBUG="-DMODULE_DEBUG";
else
  DEBUG="-DDEBUG -DMODULE_DEBUG -DDEVELOPER -DMAC_DEBUG"
fi

if [ -z "$once" ] ; then
  (while : ; do
    echo $$: "Server restart at `date`"
    echo $$: "Debug log in $LOGDIR/debug/$FILES.1"
    rotate $LOGDIR/debug/$FILES

    start_roxen 2>>$LOGDIR/debug/$FILES.1 1>&2

    exitcode="$?"

    if [ "$exitcode" -eq "0" ] ; then
      # Clean shutdown.
      echo $$: "Roxen shutdown."
      exit 0
    fi
    if [ "$exitcode" -lt "0" ] ; then
      # Signal death.
      echo $$: "Roxen died of signal $exitcode. Restarting..."
    else
      # Restart. Sleep a few seconds before restarting.
      echo $$: Roxen died. Restarting in 5 seconds...
      sleep 5
    fi
  done
    
  echo $$: Not Reached "famous last words"

  ) < /dev/null > $LOGDIR/debug/start_$FILES.output 2>&1 &
  cat << oo
Using configuration from $DIR, storing the debug log in $LOGDIR/debug/$FILES.1
You can use the configuration interface in the server to get debug info.
oo

else
  echo $$: "Server restart at `date`"
  start_roxen
fi
    
