Author: Magnus Holmgren <holmgren@debian.org>
Description: Mostly adapt server to run as a daemon

--- a/pyscrabble/net/server.py
+++ b/pyscrabble/net/server.py
@@ -46,7 +46,7 @@ class ScrabbleServerFactory(protocol.Ser
         self.db = db.DB()
         self.maxUsersLoggedIn = 0
         self.startDate = util.Time(seconds=time.time(), dispDate=True)
-        self.rankings = rank.Rankings( resources["config"][constants.RANK_CONFIG] )
+        self.rankings = rank.Rankings( resources["serverconfig"][constants.RANK_CONFIG] )
         
         dir = resources["resources"][constants.DICT_DIR].path
         for lang in os.listdir( dir ):
--- a/pyscrabble/dist.py
+++ b/pyscrabble/dist.py
@@ -3,6 +3,8 @@ import os
 import sys
 from distutils.command.install_lib import install_lib
 from distutils.command.install_scripts import install_scripts
+from distutils.command.install_data import install_data
+from distutils.command.install import install
 
 APP_NAME = 'pyscrabble'
 
@@ -31,8 +33,6 @@ except ImportError:
     RESOURCE_PREFIX = 'resources'
 
 CONFIG_DIR = get_app_data_dir()
-if not os.path.exists(CONFIG_DIR):
-    CONFIG_DIR = os.path.join(RESOURCE_PREFIX, 'config')
 
 def getLocaleDirs(dir, domain):
     l = []
@@ -76,6 +76,13 @@ def getDataFiles():
            getResourceDirs('resources/dict', True, 'resources') + \
            getResourceDirs('resources/letters', True, 'resources')
 
+try:
+    from __installed__ import SERVER_LOG_DIR, SERVER_DB_DIR, SERVER_CONFIG_DIR
+except ImportError:
+    SERVER_CONFIG_DIR = os.path.join(RESOURCE_PREFIX, 'config')
+    SERVER_DB_DIR = CONFIG_DIR
+    SERVER_LOG_DIR = CONFIG_DIR
+
 def ensure_config_dir(dir):
     '''
     Ensure config directory exists
@@ -112,11 +119,14 @@ class InstallLib(install_lib):
         self.mkpath(os.path.dirname(filename))
         
         install = self.distribution.get_command_obj('install')
-        datadir = os.path.join(install.prefix, 'share', APP_NAME)
+        datadir = os.path.join(install.prefix, 'share', 'games', APP_NAME)
         
         fp = open(filename, 'w')
         fp.write('# Generated by setup.py do not modify\n')
-        fp.write("RESOURCE_PREFIX = '%s'\n" % datadir)
+        fp.write("RESOURCE_PREFIX = %r\n" % datadir)
+        fp.write("SERVER_CONFIG_DIR = %r\n" % install.confdir)
+        fp.write("SERVER_DB_DIR = %r\n" % install.dbdir)
+        fp.write("SERVER_LOG_DIR = %r\n" % install.logdir)
         fp.close()
 
         return filename
@@ -125,6 +135,35 @@ class InstallLib(install_lib):
         template = self.generate_template()
         return install_lib.install(self) + [template]
 
+class InstallData(install_data):
+    def fix_path(self, item):
+        install = self.distribution.get_command_obj('install')
+        if type(item) in (list, tuple):
+            if 'config' in item[0]:
+                return (item[0].replace('config', install.confdir), item[1])
+            else:
+                return (item[0].replace('resources', 'share/games/'+APP_NAME), item[1])
+        else:
+            return item
+
+    def finalize_options(self):
+        self.data_files = [ self.fix_path(f) for f in self.data_files ]
+        install_data.finalize_options(self)
+
+class Install(install):
+    user_options = install.user_options + [ ('logdir=', None, "log directory"),
+                                            ('dbdir=', None, "database directory"),
+                                            ('confdir=', None, "configuration directory") ]
+
+    def initialize_options(self):
+        self.logdir = None
+        self.dbdir = None
+        self.confdir = None
+        install.initialize_options(self)
+
+    def finalize_options(self):
+        install.finalize_options(self)
+
 class Resource(object):
     '''
     Filesystem resource
@@ -180,4 +219,4 @@ class Resource(object):
         @param value:
         '''
         self.data[item] = value
-        
\ No newline at end of file
+        
--- a/pyscrabble/db.py
+++ b/pyscrabble/db.py
@@ -1,5 +1,4 @@
 from pyscrabble import constants
-from pyscrabble import manager
 from ZODB import FileStorage, DB as _DB
 import transaction
 
@@ -12,8 +11,10 @@ class DB(object):
         '''
         Initialize the connection to the DB
         '''
-        r = manager.ResourceManager()
-        path = r["config"][constants.DB_LOCATION]
+        import os
+        from pyscrabble import dist
+
+        path = os.path.join(dist.SERVER_DB_DIR, constants.DB_LOCATION)
         
         storage = FileStorage.FileStorage(path)    
         db = _DB(storage)
--- a/pyscrabble/manager.py
+++ b/pyscrabble/manager.py
@@ -36,6 +36,7 @@ class ResourceManager(object):
         '''
         self.loaded = True
         
+        self["serverconfig"] = dist.Resource( dist.SERVER_CONFIG_DIR )
         self["config"] = dist.Resource( dist.CONFIG_DIR )
         self["resources"] = dist.Resource( dist.RESOURCE_PREFIX )
     
--- a/server_console.py
+++ b/server_console.py
@@ -76,15 +76,15 @@ class ServerConsole(object):
         '''
         Configure the server
         '''
-        dist.ensure_config_dir(dist.CONFIG_DIR)
+        dist.ensure_config_dir(dist.SERVER_CONFIG_DIR)
         resources = manager.ResourceManager()
         logging.basicConfig(level=logging.DEBUG,
                     format='%(asctime)s %(name)s %(levelname)s %(message)s',
-                    filename=resources["config"][constants.LOG_FILE],
-                    filemode='w')
+                    filename=os.path.join(dist.SERVER_LOG_DIR, constants.LOG_FILE),
+                    filemode='a')
         
         
-        config = resources["config"][constants.SERVER_CONSOLE_CONFIG]
+        config = resources["serverconfig"][constants.SERVER_CONSOLE_CONFIG]
         
         if not os.path.exists(config):
             raise IOError, "%s must exist in %s" % (constants.SERVER_CONSOLE_CONFIG, resources["config"].path)
--- a/setup.py
+++ b/setup.py
@@ -7,21 +7,12 @@ except ImportError:
     HAS_PY2EXE = False
 import glob
 import os
-import pkg_resources
+#import pkg_resources
 import sys
 from pyscrabble.constants import VERSION
 from pyscrabble import util
 from pyscrabble import dist
 
-def fix_path(item):
-    if type(item) in (list, tuple):
-        if 'config' in item[0]:
-            return (item[0].replace('config', dist.get_app_data_dir()), item[1])
-        else:
-            return (item[0].replace('resources/', 'share/pyscrabble/'), item[1])
-    else:
-        return item
-
 kwargs = {
     'name': 'pyscrabble',
     'version': VERSION,
@@ -80,7 +71,7 @@ if HAS_PY2EXE and 'py2exe' in sys.argv:
     #    kwargs['data_files'] += dist.getResourceDirs(egg.location, ensureLower=False, basePath=None, outdir='extra')
 else:
     kwargs['scripts'] = ['pyscrabble-main.py', 'server_console.py', 'db_upgrade.py']
-    kwargs['data_files'] = [fix_path(x) for x in kwargs['data_files']]
-    kwargs['cmdclass'] = {'install_lib': dist.InstallLib, 'install_scripts' : dist.InstallScripts}
+    #    kwargs['data_files'] = [fix_path(x) for x in kwargs['data_files']]
+    kwargs['cmdclass'] = {'install': dist.Install, 'install_lib': dist.InstallLib, 'install_scripts' : dist.InstallScripts, 'install_data': dist.InstallData}
 
-setup(**kwargs)
\ No newline at end of file
+setup(**kwargs)
