#!/usr/bin/env python3

# To change this template, choose Tools | Templates
# and open the template in the editor.

import os

__author__="alvare"
__date__ ="$22-Mar-2011 9:53:17 AM$"

global BIRCH
global BIRCH_DAT
global BIRCHLIBS
global BIRCH_PLATFORM
global BL_ImageViewer
global BL_PDFViewer
global BL_PSViewer
global BL_TextEditor
global HOME

if not os.environ.get('BIRCH'):
    print("ERROR - BIRCH environment not properly set")
    exit()

BIRCH              = str(os.environ['BIRCH'])
BIRCH_DAT          = str(os.path.join(BIRCH, 'dat'))
BL_ImageViewer  = ""
BL_PDFViewer        = ""
BL_PSViewer         = ""
BL_TextEditor       = ""

if os.environ.get('BIRCH_PLATFORM'):
    BIRCH_PLATFORM = str(os.environ['BIRCH_PLATFORM'])
else:
    BIRCH_PLATFORM = autodetect_platform()

if os.environ.get('BL_ImageViewer'):
    BL_ImageViewer = os.environ.get('BL_ImageViewer')
if os.environ.get('BL_PDFViewer'):
    BL_PDFViewer = os.environ.get('BL_PDFViewer')
if os.environ.get('BL_PSViewer'):
    BL_PSViewer = os.environ.get('BL_PSViewer')

# Use this for a more diverse server cluster
# Platform-specific setup commands
BIRCHLIBS = ''
if (BIRCH_PLATFORM == 'solaris-sparc'):
    BIRCHLIBS = '/usr/openwin/lib' + os.pathsep + '/usr/lib' + os.pathsep + '/usr/lib/X11' + os.pathsep + '/usr/dt/lib' + os.pathsep + BIRCH + '/lib-solaris-sparc' + os.pathsep + BIRCH + '/local/lib-solaris-sparc'
elif (BIRCH_PLATFORM == 'solaris-amd64'):
    BIRCHLIBS = '/usr/openwin/lib' + os.pathsep + '/usr/lib' + os.pathsep + '/usr/lib/X11' + os.pathsep + '/usr/dt/lib' + os.pathsep + BIRCH + '/lib-solaris-amd64' + os.pathsep + BIRCH + '/local/lib-solaris-amd64'
elif (BIRCH_PLATFORM == 'sun'):   # DEPRECATED
    BIRCHLIBS = '/usr/openwin/lib' + os.pathsep + '/usr/lib' + os.pathsep + '/usr/dt/lib' + os.pathsep + BIRCH + '/lib-solaris-sparc' + os.pathsep + BIRCH + '/local/lib-solaris-sparc'
elif (BIRCH_PLATFORM == 'linux-arm64'):
    BIRCHLIBS = BIRCH + "/local/lib-linux-arm64" + os.pathsep + BIRCH + "/lib-linux-arm64" + os.pathsep + BIRCH + "/lib-linux-arm64/openwin/lib" + os.pathsep + BIRCH + "/lib-linux-arm64/fc4libs"
#    BIRCHLIBS = BIRCH + '/lib-linux-intel' + os.pathsep + BIRCH + '/lib-linux-intel/openwin/lib' + os.pathsep + BIRCH + '/local/lib-linux-intel'
elif (BIRCH_PLATFORM == 'linux-x86_64'):
    BIRCHLIBS=BIRCH + '/local/lib-linux-x86_64' + os.pathsep + BIRCH + '/lib-linux-x86_64' + os.pathsep + BIRCH + '/lib-linux-x86_64/openwin/lib' + os.pathsep + BIRCH + '/lib-linux-x86_64/fc4libs' + os.pathsep
#    BIRCHLIBS = BIRCH + "/lib-linux-x86_64" + os.pathsep + BIRCH + "/lib-linux-x86_64/openwin/lib" + os.pathsep + BIRCH + "/local/lib-linux-x86_64" + os.pathsep + BIRCH + "/lib-linux-x86_64/fc4libs"
#    BIRCHLIBS = BIRCH + '/lib-linux-x86_64' + os.pathsep + BIRCH + '/lib-linux-x86_64/openwin/lib' + os.pathsep + BIRCH + '/local/lib-linux-x86_64'
elif (BIRCH_PLATFORM == 'osx-x86_64'):
    BIRCHLIBS = BIRCH + "/lib-osx-x86_64" + os.pathsep + BIRCH + "/lib-osx-x86_64/openwin/lib" + os.pathsep + BIRCH + "/local/lib-osx-x86_64" + os.pathsep + BIRCH + "/lib-osx-x86_64/fc4libs"
elif (BIRCH_PLATFORM == 'macos-arm64'):
    BIRCHLIBS = BIRCH + "/lib-macos-arm64" + os.pathsep + BIRCH + "/lib-macos-arm64/openwin/lib" + os.pathsep + BIRCH + "/local/lib-macos-arm64" + os.pathsep + BIRCH + "/lib-macos-arm64/fc4libs"


# else:

if os.environ.get('BL_TextEditor'):
    BL_TextEditor = os.environ.get('BL_TextEditor')

if BL_TextEditor == "nedit":
    BL_TextEditor = 'nedit'
elif BL_TextEditor == "gedit":
    BL_TextEditor = 'gedit_wrapper.sh'

HOME = "~"

try:
    from win32com.shell import shellcon, shell
    HOME = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
except ImportError: # quick semi-nasty fallback for non-windows/win32com case
    HOME = os.path.expanduser("~")

if __name__ == "__main__":
    print("Not executable")

def autodetect_platform():
    # Automatic detection of the platform
    #echo 'browser.csh: Detecting platform'
    #Find out the following platform parameters:
    #  operating system
    #  processor family

    #Operating system
    OS = os.uname()[0]
    if OS in ("SunOS", "sun", "solaris"):
        OS = "solaris"
    elif OS == "Darwin":
        OS = "Darwin"
    else:
        OS = "linux"

    #echo 'browser.csh: OS = '$OS
    #Processor family
    PROC = os.uname()[4]
    if PROC == "sparc":
        PROC = "sparc"
    elif PROC in ("x86_64", "amd64"):
        PROC = "x86_64"
    elif PROC in ("aarch64", "arm64"):
        PROC = "arm64"
    else:
        PROC = "intel"

    #echo 'browser.csh: PROC = ' $PROC
    # Map the result to supported BIRCH platforms
    RESULT = 'linux-intel'
    if OS == "solaris":
        RESULT = 'solaris-sparc'
        if PROC == "intel":
            RESULT = 'solaris-amd64'
    elif OS == "linux":
        #default is linux-intel
        if PROC == "x86_64":
            RESULT = 'linux-x86_64'
        elif PROC == "arm64":
            RESULT = 'linux-arm64'            
    elif OS == "Darwin":
        if PROC == "arm64" :
            RESULT = 'macos-arm64'
        else :
            RESULT = 'osx-x86_64'
    return RESULT
