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

# Balazar
# Copyright (C) 2003 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 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 "* Balazard * Balazar lives in %s" % APPDIR
sys.path.insert(0, APPDIR)

from balazar.version import VERSION
import balazar.ng.server as ng_server


PORT       = 36079
MAX_MEMORY = 10000000
LEVEL_DIR  = ""

i = 1
while i < len(sys.argv):
  arg = sys.argv[i]
  i = i + 1
  
  if   arg == "--port":
    PORT = int(sys.argv[i])
    i += 1
    
  elif arg == "--max-memory":
    MAX_MEMORY = int(sys.argv[i])
    i += 1
    
  elif arg == "--level-dir":
    LEVEL_DIR = sys.argv[i]
    i += 1
    
  elif arg == "--version":
    print "Balazard version", VERSION
    sys.exit()
    
  elif arg == "--help":
    print """Balazard: TCP/IP Server for Balazar (Balazar is a fun game)
Synopsis: balazard [options...]

where options are:
--help                     This help
--version                  Shows version number
--port PORT                Sets the server's TCP port number (default 36079)
--max-memory MAX_MEMORY    Sets maximum memory (in bytes) for server-side level storage (default ~10MB) ; clients can store additionnal levels too.
--level-dir LEVEL_DIR      Sets the level directory (usefull if you want to use pre-generated levels, or to keep the level alive when the server is down).
"""
    sys.exit()
    
  else:
    print "Unknown command line arg : '%s' !" % arg
    sys.exit(2)
    
server = ng_server.Server(34, (0, 0), PORT, MAX_MEMORY, LEVEL_DIR)
server.serve_forever()

