#!/bin/bash
# SANDBOX_DIR should refer to the directory in which sandbox is placed.
#SANDBOX_DIR=~/sandbox
#SANDBOX_DIR=.
SANDBOX_DIR=/usr/share/sandboxgamemaker

SANDBOX_OPTIONS=""
SANDBOX_MODULE="fps"
SANDBOX_HOME="${HOME}/.platinumarts"
SANDBOX_TYPE="client"

while [ $# -ne 0 ]
do
   case $1 in
      "-h"|"-?"|"-help"|"--help")
         echo ""
         echo "Sandbox Linux Launching Script"
         echo "Example: ./sandbox_unix -gssp -t1"
         echo ""
         echo "   Script Arguments"
         echo "  -h|-?|-help|--help   show this help message"
         echo "  -g<string>      set gamemode to <string> (default: fps)"
         echo "            available modes: ssp, fps, rpg, movie"
         echo "  --server      launch dedicated server binary instead"
         echo "  --master      launch a masterserver for server registration"
         echo "            NOTE: compiled masterserver not included"
         echo ""
         echo "   Client Options"
         echo "  -q<string>      use <string> as the home directory (default: ~/.platinumarts)"
         echo "  -k<string>      mounts <string> as a package directory"
         echo "  -r<string>      executes <string> before launching, use instead of arguments such as -t, -a, -h, etc"
         echo "  -t<num>      sets fullscreen to <num>"
         echo "  -d<num>      runs a dedicated server (0), or a listen server (1)"
         echo "  -w<num>      sets window width, height is set to width * 3 / 4 unless also provided"
         echo "  -h<num>      sets window height, width is set to height * 4 / 3 unless also provided"
         echo "  -z<num>      sets depth (z) buffer bits (do not touch)"
         echo "  -b<num>      sets colour bits (usually 32 bit)"
         echo "  -a<num>      sets anti aliasing to <num>"
         echo "  -v<num>      sets vsync to <num> -- -1 for auto"
         echo "  -t<num>      sets fullscreen to <num>"
         echo "  -s<num>      sets stencil buffer bits to <num> (do not touch)"
         echo "  -f<num>      turns of shaders (0), or sets shader precision to <num> - 1 (1-3)"
         echo "  -l<string>      loads map <string> after initialisation"
         echo "  -x<string>      executes script <string> after initialisation"
         echo ""
         echo "  Server Options"
         echo "  NOTE: the dedicated server binary uses initserver.cfg, which overrides the below"
         echo "  -u<num>      sets the upload rate to <num>"
         echo "  -c<num>      sets the maximum amount of clients (max: 127 humans)"
         echo "  -i<string>      sets the server's ip address (use with caution)"
         echo "  -j<num>      sets the port to use for server connections"
         echo "  -m<string>      sets the masterserver's URL to <string>"
         echo "  -q<string>      use <string> as the home directory (default: ~/.platinumarts)"
         echo "  -k<string>      mounts <string> as a package directory"
         echo ""
         echo "Sandbox Script by Kevin \"Hirato Kirata\" Meyer"
         echo "(c) 2008-2010 - zlib/libpng licensed"
         echo ""

         exit 1
      ;;
   esac

   tag=$(expr substr "$1" 1 2)
   argument=$(expr substr "$1" 3 1022)

   case $tag in
      "-g")
         SANDBOX_MODULE=$argument
      ;;
      "-q")
         SANDBOX_HOME="\"$argument\""
      ;;
      "-k"|"-i"|"-m"|"-r"|"-l"|"-x")
         SANDBOX_OPTIONS+=" $tag\"$argument\""

      ;;
      "--")
         case $argument in
         "server")
            SANDBOX_TYPE="server"
         ;;
         "master")
            SANDBOX_TYPE="master"
         ;;
         esac
      ;;
      *)
         SANDBOX_OPTIONS+=" $1"
      ;;
   esac

   shift
done

function failed {
   echo "A problem was encountered, please check which of the following it is."
   echo "1) there's no ${SANDBOX_TYPE} for module ${SANDBOX_MODULE}"
   echo "2) There isn't an available executable for your architecture; $(uname -m)"
   echo "3) the executable was moved"
   echo "install the sdl, sdlimage and sdlmixer DEVELOPMENT libraries and use \"cd src && make install\" to compile a binary"
   echo "NOTE: sandbox is installed in ${SANDBOX_DIR}"
   exit 1
}


cd ${SANDBOX_DIR}
case ${SANDBOX_TYPE} in
   "client")
      if [ -a /usr/lib/sandboxgamemaker/sandbox_client_${SANDBOX_MODULE} ]
      then
         eval /usr/lib/sandboxgamemaker/sandbox_client_${SANDBOX_MODULE} -q${SANDBOX_HOME} -r ${SANDBOX_OPTIONS}
      else
         failed
      fi
   ;;
   "server")
      if [ -a /usr/lib/sandboxgamemaker/sandbox_server_${SANDBOX_MODULE} ]
      then
         eval /usr/lib/sandboxgamemaker/sandbox_server_${SANDBOX_MODULE} -q${SANDBOX_HOME} ${SANDBOX_OPTIONS}
      else
         failed
      fi
   ;;
   "master")
      if [ -a /usr/lib/sandboxgamemaker/sandbox_master ]
      then
         eval /usr/lib/sandboxgamemaker/sandbox_master
      else
         failed
      fi
   ;;
esac
