#! /usr/bin/python -O
# -*- python -*-

# Balazar
# Copyright (C) 2003-2005 Jean-Baptiste LAMY
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#import gc
#gc.disable()

import os, os.path, sys
# If installed in /usr, /usr/local, ... add /usr/share (or ...) to the module 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 * Balazar lives in %s" % APPDIR
sys.path.insert(0, APPDIR)

import soya, soya.tofu4soya
import tofu
import balazar, balazar.globdef as globdef

mode = ""
i = 1
while i < len(sys.argv):
  arg = sys.argv[i]
  i = i + 1
  
  if   arg == "--single": mode = arg; login = sys.argv[i]; password = sys.argv[i + 1]; i += 2
  elif arg == "--server": mode = arg
  elif arg == "--client": mode = arg; hostname = sys.argv[i]; login = sys.argv[i + 1]; password = sys.argv[i + 2]; i += 3
  elif arg == "--fullscreen": globdef.FULLSCREEN = 1
  elif arg == "--windowed"  : globdef.FULLSCREEN = 0
  elif arg == "--view"      :
    globdef.SERVER_VIEW   = 1
    globdef.SCREEN_WIDTH  = 320
    globdef.SCREEN_HEIGHT = 240
    
  elif arg == "--saved-game-dir":
    globdef.SAVED_GAME_DIR = sys.argv[i]
    i += 1
    
  elif arg == "--screensize":
    globdef.SCREEN_WIDTH  = int(sys.argv[i])
    globdef.SCREEN_HEIGHT = int(sys.argv[i + 1])
    i += 2
    
  elif arg == "--no-sound":
    globdef.SOUND = 0
    globdef.MUSIC = 0
    
  elif arg == "--debug":
    globdef.DEBUG = 1
    
  elif arg == "--no-fps-limit":
    globdef.MIN_FRAME_DURATION = 0.0
    
  elif arg == "--version":
    print "Balazar version", globdef.VERSION
    sys.exit()
    
  elif arg == "--help":
    print """Balazar: A fun 3D game
Usages :

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

  balazar [options...] --server
Starts the server

  balazar [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:
--help                     This help
--version                  Shows version number
--fullscreen               Fullscreen mode
--windowed                 Windowed mode
--screensize WIDTH HEIGHT  Sets the screen size (in pixel)
--no-sound                 Disable sounds and music
--debug                    Enable debug output
--no-fps-limit             Disable FPS limit, for benchmarking (default is to limit the FPS to 30)
--saved-game-dir PATH      Set the directory where games are saved and loaded to PATH
--view                     Launch the server in graphical mode (server only, use TAB to switch player)
"""
    sys.exit()
    
  else:
    print "Unknown command line arg : '%s' !" % arg
    sys.exit(2)
    

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


if   mode == "--server": globdef.SOUND = globdef.MUSIC = 0
import balazar, balazar.globdef as globdef, balazar.game_interface, balazar.character, balazar.player, balazar.level


#import random; random.seed(12282)

balazar.sound = reload(balazar.sound)


if   mode == "--single": balazar.game_interface.start_single(login, password)
elif mode == "--server": balazar.game_interface.start_server()
elif mode == "--client": balazar.game_interface.start_client(hostname, login, password)
else:
  import balazar.gui
  
  balazar.game_interface.init()
  
  mainmenu = balazar.gui.MainMenu ()
  mainmenu.run ()
  
sys.exit()
