#!/usr/bin/env python3

import birchenv
import copy
import os
import os.path
import subprocess
import sys

# Wrapper for treetool

TREEENV = copy.copy(os.environ)

# Use this for a more diverse server cluster
# Platform-specific setup commands
# GDE requires the obsolete xview libraries, which are probably available by default on
# Solaris systems in /usr/openwin, but we put this here just to make sure.
# Linux systems almost never have /usr/openwin, so we need to supply that in lib-linux-intel/openwin/lib.
# We put /usr/dt/lib in in case someone is using CDE, and the other libraries are there just as additional
# fallbacks. For example, if /usr/openwin/lib wasn't on your Solaris system, you could install it in 
# $BIRCH/local/lib-solaris-sparc.

GDEFONTS = ""

# Use this for a more diverse server cluster
# Platform-specific setup commands
if (birchenv.BIRCH_PLATFORM == 'linux-intel'):
    GDEFONTS = "" + birchenv.BIRCH + "/lib-linux-intel/gde.fonts/100dpi" + \
        "," + birchenv.BIRCH + "/lib-linux-intel/gde.fonts/misc" + \
        "," + birchenv.BIRCH + "/lib-linux-intel/gde.fonts/75dpi"
elif (birchenv.BIRCH_PLATFORM == 'linux-x86_64'):
    GDEFONTS = birchenv.BIRCH + "/lib-linux-x86_64/gde.fonts/100dpi" + \
        "," + birchenv.BIRCH + "/lib-linux-x86_64/gde.fonts/misc" + \
        "," + birchenv.BIRCH + "/lib-linux-x86_64/gde.fonts/75dpi"
elif (birchenv.BIRCH_PLATFORM == 'osx-x86_64'):
    GDEFONTS = birchenv.BIRCH + "/lib-osx-x86_64/gde.fonts/100dpi" + \
        "," + birchenv.BIRCH + "/lib-osx-x86_64/gde.fonts/misc" + \
        "," + birchenv.BIRCH + "/lib-osx-x86_64/gde.fonts/75dpi"
#elif (birchenv.BIRCH_PLATFORM == 'AIX'):
#elif (birchenv.BIRCH_PLATFORM == 'HP'):
# else:

if GDEFONTS != "":
    TREEENV['GDEFONTS'] = GDEFONTS
elif TREEENV.has_key('GDEFONTS'):
    GDEFONTS = TREEENV['GDEFONTS']

if (birchenv.BIRCHLIBS != ''):
    if TREEENV.has_key('LD_LIBRARY_PATH'):
        TREEENV['LD_LIBRARY_PATH'] = str(TREEENV['LD_LIBRARY_PATH']) + os.pathsep + birchenv.BIRCHLIBS
    else:
        TREEENV['LD_LIBRARY_PATH'] = birchenv.BIRCHLIBS

if (birchenv.BIRCH_PLATFORM == 'osx-x86_64'):
    TREEENV['DYLD_LIBRARY_PATH'] = str(TREEENV['LD_LIBRARY_PATH'])

#echo LD_LIBRARY_PATH $LD_LIBRARY_PATH

# ------------------------ FONTS ------------------------------
# See $BIRCH/local/lib-linux-intel/README.fonts

if TREEENV.has_key('GDEFONTS'):
#   echo GDEFONTS "$GDEFONTS"
   # FOR TESTING PURPOSES ONLY:
   # Remove all fonts from fontpath, and set ONLY the fonts we think we need
   #xset fp- /home/psgendb/.gnome2/share/cursor-fonts,tcp/localhost:7100,/home/psgendb/.gnome2/share/fonts
   # Set $GDEFONTS to fontpath
   #xset fp= $GDEFONTS

   # Append $GDEFONTS to fontpath. Probably the safest choice.
   subprocess.call(['xset', '+fp' + GDEFONTS], env=TREENV)
   # Prepend $GDEFONTS to fontpath
   #xset fp+ $GDEFONTS
   
   # Show current X settings, including fontpath
   #xset -q   

# $TREETOOL_FONTS is supposed to tell treetool where
# to find fonts, but it simply doesn't work. The user must have
# a .font_map file in their $home directory.
# $BIRCH/script/treetool is a wrapper that checks for this file
# and calls $BIRCH/$BIRCH_BIN/treetool.bin
# This has nothing whatsoever to do with GDEFONTS
if not os.path.exists(os.path.join(os.path.expanduser('~'), '.font_map')):
    os.symlink(os.path.join(TREENV['dat'], 'treetool', 'font_map'), os.path.join(os.path.expanduser('~'), '.font_map'))

# Finally, we get around to actually running the program
COMMAND = copy.copy(sys.argv)
COMMAND[0] = 'treetool.bin'

subprocess.call(COMMAND, env=ACEENV) # &
