#! /usr/bin/python -O
# -*- python -*-
# -*- coding: utf-8 -*-

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys, os, os.path

HERE = os.path.dirname(sys.argv[0])

if   HERE.endswith("games"): # /usr/{local/}games
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share", "games"))
elif HERE.endswith("bin"): # /usr/{local/}bin
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share"))
else: # Raw source not installed
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))

print "* Balazar 3 * Balazar 3 lives in %s" % APPDIR
sys.path.insert(0, APPDIR)


try:
  import psyco
  psyco.full()
except:
  print "* Balazar 3 * (Psyco not found; if you are using an x86 processor, installing psyco can speed up Balazar 3)"



import balazar3.globdef as globdef
import balazar3.tofu    as tofu


mode = "gui"

i = 1
while i < len(sys.argv):
  if   sys.argv[i] == "--zaurus"        : globdef.ZAURUS = 1
  elif sys.argv[i] == "--single"        : mode = "single"; i += 1; globdef.SINGLE_LOGIN = sys.argv[i]; i += 1; globdef.SINGLE_PASSWORD = sys.argv[min(i, len(sys.argv) - 1)]
  elif sys.argv[i] == "--client"        : mode = "client"; i += 1; globdef.CLIENT_HOST = sys.argv[i]; i += 1; globdef.CLIENT_LOGIN = sys.argv[i]; i += 1; globdef.CLIENT_PASSWORD = sys.argv[min(i, len(sys.argv) - 1)]
  elif sys.argv[i] == "--server"        : mode = "server"; i += 1; globdef.SERVER_HOST = sys.argv[i]; globdef.DRIVER = "dummy"
  elif sys.argv[i] == "--port"          : i += 1; globdef.CLIENT_PORT = globdef.SERVER_PORT = int(sys.argv[i])
  elif sys.argv[i] == "--gui"           : mode = "gui"
  elif sys.argv[i] == "--2d"            : globdef.DRIVER = "2d"
  elif sys.argv[i] == "--2d800x480"     : globdef.DRIVER = "2d800x480"
  elif sys.argv[i] == "--3d"            : globdef.DRIVER = "3d"
  elif sys.argv[i] == "--no-fps-limit"  : globdef.LIMIT_FPS = 0
  elif sys.argv[i] == "--fullscreen"    : globdef.FULLSCREEN = 1
  elif sys.argv[i] == "--windowed"      : globdef.FULLSCREEN = 0
  elif sys.argv[i] == "--screensize"    : i += 1; globdef.SCREEN_WIDTH = int(sys.argv[i]); i += 1; globdef.SCREEN_HEIGHT = int(sys.argv[i])
  elif sys.argv[i] == "--sound"         : i += 1; globdef.SOUND_VOLUME = float(sys.argv[i])
  elif sys.argv[i] == "--saved-game-dir": i += 1; globdef.SAVED_GAME_DIR = sys.argv[i]
  elif sys.argv[i] == "--version"       :
    print "Balazar III version %s." % globdef.VERSION
    sys.exit()
    
  elif sys.argv[i] == "--help"          :
    print """Balazar III : a dungeon adventure game
Usages :

  balazar3 [options...]
Starts the game menu.

  balazar3 [options...] --single <login> [<password>]
Starts a single player game named <login>.

  balazar3 [options...] --server <host>
Starts the server on <host>.

  balazar3 [options...] --client <host> <login> [<password>]
Starts a client and connect to server <host> with login <login>
and password <password>. If login doesn't exist, a new player is
created.

Where options are:

--3d                          Use the 3D driver
--2d                          Use the 2D driver in 640x480
--2d800x480                   Use the 2D driver in 800x480
--saved-game-dir <path>       Set the directory where games are saved and loaded to <path>
--sound <volume>              Set sound volume (0.0 - 1.0)
--port <port>                 Use network port <port>
--fullscreen                  Fullscreen mode (3D driver only)
--windowed                    Windowed mode (3D driver only)
--screensize <width> <height> Sets the screen size (in pixel, 3D driver only)
--zaurus                      Zaurus optimized mode (disable key repeat)
--no-fps-limit                Disable FPS limit, for benchmarking (default is to limit the FPS to 30-40)
--help                        This help
--version                     Shows version number

Available drivers: %s.
""" % ", ".join(globdef.AVAILABLE_DRIVERS)
    sys.exit()

  else:
    print "Unknown option %s!"% sys.argv[i]
    sys.exit(1)
  i += 1


if globdef.ZAURUS:
  globdef.DRIVER = "2d"
  
  # On Zaurus, key repeat consumes A LOT of time ! => disable them during the game
  os.system("khctl killrepeat")
  
  import atexit
  def reset_khctl_normal(): os.system("khctl reload")
  atexit.register(reset_khctl_normal)
  
  

from balazar3.game import *

init()

if   mode == "single": start_single()
elif mode == "client": start_multi()
elif mode == "server": start_server()
elif mode == "gui":
  exec "import balazar3.menu_%s as menu" % globdef.DRIVER
  menu.start()



# python ./balazar3/balazar3 --single jiba
# python ./balazar3/balazar3 --server localhost
# python ./balazar3/balazar3 --client localhost jiba
# python ./balazar3/balazar3 --client localhost blam

# service shorewall clear

# python ./balazar3/balazar3 --server 192.168.100.5
# python ./balazar3/balazar3 --client 192.168.100.5 jiba
# python ./balazar3/balazar3 --client 192.168.100.5 blam
