#!/bin/sh

# quake3 or quake3-server or whatever
IOQ3SELF=openarena-server
# "server" or "client"
IOQ3ROLE=server
# ioquake3 or ioq3ded
IOQ3BINARY=ioq3ded

ENGINE="/usr/lib/ioquake3/${IOQ3BINARY}"

DEBUGGER="$OPENARENA_DEBUGGER"

# we're a standalone game
CVARS="+set com_basegame baseoa"
CVARS="$CVARS +set cl_gamename openarena"
CVARS="$CVARS +set fs_basepath /usr/lib/games/openarena"
CVARS="$CVARS +set com_homepath .openarena"

# OA 0.8.5 uses the Quake 3 defaults even though they're not really appropriate
#CVARS="$CVARS +set sv_heartbeat QuakeArena-1"
#CVARS="$CVARS +set sv_flatline QuakeArena-1"

# OA uses a different protocol number to reflect incompatible game content
CVARS="$CVARS +set protocol 71"
# OA's default master server is different
CVARS="$CVARS +set sv_master1 dpmaster.deathmask.net"
# update.quake3arena.com is pretty irrelevant if you're playing OA
CVARS="$CVARS +set cl_motd 0"

QUIET=0

EXCUSE="\
OpenArena ${IOQ3ROLE} wrapper for Debian\n\
\n\
Usage: ${IOQ3SELF} [OPTION]...\n\
\n\
 -h, --help\t\tDisplay this help\n\
 -q, --quiet\t\tDisable console output\n\
  +<internal command>\tPass commands to the engine\n"

while [ "$1" != "" ]; do
  case "$1" in
    -h|--help)
      echo ${EXCUSE}
      exit 0
      ;;
    -q|--quiet)
      CVARS="$CVARS +set ttycon 0"
      QUIET=1
      ;;
    *)
      break
      ;;
  esac
  shift
done

if test "z$QUIET" = z1; then
  exec >/dev/null 2>&1;
fi

if test -n "$OPENARENA_BACKTRACE"; then
  exec gdb -return-child-result -batch -ex run -ex 'thread apply all bt full' -ex kill -ex quit --args ${ENGINE} ${CVARS} "$@"
else
  exec ${DEBUGGER} ${ENGINE} ${CVARS} "$@"
fi
