import os
import sys
import os.path

__DEBUG = False
__RUN_MODE = ''
__env_mode = os.getenv('TSADM_MODE')
if __env_mode == 'test' or __env_mode == 'dev':
    __RUN_MODE = __env_mode
if __env_mode == 'dev':
    __DEBUG = True

__BASE_DIR = '/opt/tsadm'+__RUN_MODE
__CONFIG_PATH = '/etc/opt/tsadm'+__RUN_MODE+'/config.json'

__TSADM = {
    'RUN_MODE': __RUN_MODE,
    'BASE_DIR': __BASE_DIR,
    'CONFIG_PATH': __CONFIG_PATH,
    'DEBUG': __DEBUG,
    'CHARSET': 'utf-8',
    'LANG_CODE': 'en-gb',
    'TIME_ZONE': 'Europe/London',
    'DJANGO_SECRET_KEY': '0GQMw7F*Fy(G_+{(K)fop06CH*mR+!,*\Q]S&Mg4|y(l;?}4|~',
    'DJANGO_CACHE_PATH': '/var/tmp/tsadm'+__RUN_MODE+'_cache',
    'DJANGO_CACHE_TIMEOUT': 3600,
    'DJANGO_CACHE_KEY_PREFIX': 'tsadm'+__RUN_MODE+':',
    'OPENSSL': '/usr/bin/openssl',
    'JOBQ_SERVER_PORT': 6100,
    'JOBQ_SERVER_TIMEOUT': 15,
    'JOBQ_SYSLOG_TAG': 'tsadm'+__RUN_MODE+'.jobq',
    'MASTER_SERVER': 'dev.tsadm.local',
    'MASTER_SERVER_PORT': 8000,
    'MASTER_SERVER_SSL': False,
    'SITE_ENV_DOMAIN': 'dev.tsadm.local',
    'SITE_HOME_BASE': '/home/tsadm'+__RUN_MODE+'/sites',
    'LOG_DATE_FMT': '%b%d %H:%M:%S',
    'CUR_TIME_FMT': '%a %b %d %H:%M %Y %Z',
    'JOB_DATE_FMT': '%c %Z',
    'SYSLOG_TAG': 'tsadm'+__RUN_MODE+'.wapp',
    'REGR_TESTS_ENABLE': True,
    'CLEAN_HTML_ENABLE': True,
    'SLAVE_GRAPHS_BASE_URL': 'http://dev.tsadm.local/server-graphs',
    'DB_NAME': 'tsadm'+__RUN_MODE+'db',
    'DB_USER': 'tsadm'+__RUN_MODE,
    'DB_PASS': 'M34iKsymcyHL3hsU',
    'CSS_RELPATH': 'static/css/tsadm.css',
    'OFFLINE_FILE': os.path.join(__BASE_DIR, 'OFFLINE'),
}

def get(arg, default=None):
    return __TSADM.get(arg, default)

def update(d):
    return __TSADM.update(d)

def export():
    r = list()
    for sk in sorted(__TSADM.keys()):
        if sk.lower().find("pass") > 0 or sk.lower().find("secret") > 0:
            r.append((sk, '__HIDDEN__'))
        else:
            r.append((sk, __TSADM.get(sk)))
    return r

def setdefault(k, v):
    if k in __TSADM.keys():
        if k != 'RUN_MODE' and k != 'BASE_DIR':
            __TSADM[k] = v

if __DEBUG:
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        }
    }

def __load_config():
    import json
    cfg = dict()
    try:
        if __DEBUG: print("tsadm config load:", __CONFIG_PATH, file=sys.stderr)
        fh = open(__CONFIG_PATH, 'r')
        cfg = json.load(fh)
        fh.close()
    except Exception as e:
        print("tsadm config load:", __CONFIG_PATH, e, file=sys.stderr)
    if __DEBUG: print("tsadm config loaded:", cfg, file=sys.stderr)
    for k, v in cfg.items():
        if k in __TSADM.keys():
            __TSADM[k] = v
        else:
            print("tsadm config load: unknown key ", k, file=sys.stderr)

if os.path.exists(__CONFIG_PATH):
    __load_config()

@@ -236,14 +236,30 @@
 R = 
+os.path.join(
 '/opt
-/
+', '
 tsad
@@ -271,16 +271,27 @@
 RUN_MODE
+, 'webapp')
 %0A__CONFI
@@ -303,18 +303,30 @@
 H = 
-'/etc/opt/
+os.path.join('/opt', '
 tsad
@@ -338,19 +338,26 @@
 RUN_MODE
-+'/
+, 'etc', '
 config.j
@@ -360,16 +360,51 @@
 ig.json'
+)%0A__MASTER_FQDN = 'dev.tsadm.local'
 %0A%0A__TSAD
@@ -713,18 +713,37 @@
 H': 
-'/var/tmp/
+os.path.join(%5B'var', 'tmp', '
 tsad
@@ -760,24 +760,26 @@
 ODE+'_cache'
+%5D)
 ,%0A    'DJANG
@@ -1025,33 +1025,29 @@
 ERVER': 
-'dev.tsadm.local'
+__MASTER_FQDN
 ,%0A    'M
@@ -1127,33 +1127,29 @@
 OMAIN': 
-'dev.tsadm.local'
+__MASTER_FQDN
 ,%0A    'S
@@ -1168,15 +1168,31 @@
 E': 
-'/home/
+os.path.join(%5B'home', '
 tsad
@@ -1208,17 +1208,19 @@
 MODE
-+'/
+, '
 sites'
+%5D)
 ,%0A  
@@ -1472,31 +1472,33 @@
 'http://
-dev.tsadm.local
+'+__MASTER_FQDN+'
 /server-

relocate to webapp dir, config path and centralize fqdn