#!/usr/bin/env python3

#optparse is deprecated in favor of argparse as of Python 2.7. However,
# since 2.7 is not always present on many systems, at this writing,
# it is safer to stick with optparse for now. It should be easy
# to change later, since the syntax is very similar between argparse and optparse.
from optparse import OptionParser

import os
import os.path
import stat
import subprocess
import sys
import re
import shutil


'''
BIRCHSettings.py - Set environment variables for BioLegato Helper Applications

Synopsis: BIRCHSettings.py --install --birchdir directory
          BIRCHSettings.py --update --birchdir directory
          BIRCHSettings.py --setvar variable_name=value --birchdir directory

@modified: June  30, 2015
@author: Brian Fristensky
@contact: frist@cc.umanitoba.ca
'''

blib = os.environ.get("BIRCHPYLIB")
sys.path.append(blib)

from birchlib import Birchmod


PROGRAM = "BIRCHSettings.py : "
USAGE = "\n\tUSAGE: BIRCHSettings.py --install --birchdir directory" + \
   "\n\t\tBIRCHSettings.py --update --birchdir directory" + \
   "\n\t\tBIRCHSettings.py --setvar variable_name=value --birchdir directory"

DEBUG = True
if DEBUG :
    print('Debugging mode on')

BM = Birchmod(PROGRAM, USAGE)

BIRCHvariables = ['BIRCH_PROMPT']

# - - - - - - - - - - - - - Utility classes - - - - - - - - - - - - - - - - -
def chmod_ar(filename):
    if os.path.exists(filename):
        st = os.stat(filename)
        os.chmod(filename, st.st_mode | stat.S_IREAD \
                | stat.S_IRGRP | stat.S_IROTH)


def chmod_arx(filename):
    if os.path.exists(filename):
        st = os.stat(filename)
        os.chmod(filename, st.st_mode | stat.S_IEXEC | stat.S_IREAD \
                | stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH \
                | stat.S_IROTH)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Parameters:
    """
        Wrapper class for command line parameters
        """
    def __init__(self):
        """
          Initializes arguments:
                INSTALL = False
                UPDATE = False
                SETVAR = False
                VSTRING = ""
                VNAME   = ""
                VALUE   = ""
                BIRCH = ""
                SFN= ""


          Then calls read_args() to fill in their values from command line
          """
        self.INSTALL  = False
        self.UPDATE  = False
        self.SETVAR  = False
        self.VSTRING = ""
        self.VNAME   = ""
        self.VALUE   = ""
        self.BIRCH = ""
        self.read_args()
        self.SFN = os.path.join(self.BIRCH , 'local' , 'admin' , 'BIRCH.settings')


        if DEBUG :
            print('------------ Parameters from command line ------')
            print('    BIRCH: ' + self.BIRCH)
            print('    INSTALL: ' + str(self.INSTALL))
            print('    UPDATE: ' + str(self.UPDATE))
            print('    SETVAR: ' + str(self.SETVAR))
            print('    VNAME: ' + self.VNAME)
            print('    VALUE: ' + self.VALUE)
            print('    Settings file: ' + self.SFN)
            print()

    def read_args(self):
        """
                Read command line arguments into a Parameter object
        """

        parser = OptionParser()
        parser.add_option("--birchdir", dest="birch", action="store", default="",
                          help="path to BIRCH installation directory")
        parser.add_option("--install", dest="install", action="store_true", default=False,
                          help="in a new install, set environment variables to default values")
        parser.add_option("--update", dest="update", action="store_true", default=False,
                          help="in an update, set environment variables to local values, or set to default values")
        parser.add_option("--setvar", dest="vstring", action="store", default="",
                          help="set an environment variable to the specified value")

        (options, args) = parser.parse_args()
        self.BIRCH = options.birch
        self.INSTALL = options.install
        self.UPDATE = options.update
        self.VSTRING = options.vstring
        if self.VSTRING != "" :
            self.SETVAR = True
            tokens = self.VSTRING.split("=")
            self.VNAME = tokens[0]
            self.VALUE = tokens[1]

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class BIRCHSettings:
    """
        Data and methods for the BIRCH Settings file.
        """
    def __init__(self,P):
        """
          Initializes arguments:
                dict = {}
          """
        self.dict = {}
        for var in BIRCHvariables:
            self.dict[var] = ""

        if os.path.exists(P.SFN) :
            self.ReadBIRCHSettings(P.SFN)
        #else:
        #    DFN = os.path.join(self.BIRCH , 'admin' , 'BIRCH.settings.default')
        #    self.ReadBIRCHSettings(DFN)

        if DEBUG :
            print('- - - - - BIRCH Settings - - - - -')
            for k in self.dict :
                print('    ' + k + ',' + self.dict[k])

    def ReadBIRCHSettings(self,FN):
        """
                Read current values of BIRCHvariables from BIRCH.settings.
        """

        if os.path.exists(FN) :
            Sfile = open(FN,'r')
            for line in Sfile :
                line = line.strip()
                # ignore blank lines and comment lines
                if (line != "" and line[0] != '#') :
                    print(line)
                    tokens = line.split("=")
                    if tokens[0] in BIRCHvariables :
                        self.dict[tokens[0]] = tokens[1]
            Sfile.close()


    def WriteBIRCHSettings(self,SFN):
        """
                Write current values of BIRCHvariables to BL.properties.<platform> file.
        """

        Sfile = open(SFN,'w')
        Sfile.write('# DO NOT EDIT THIS FILE!\n')
        Sfile.write('# This file is automatically generated by BIRCHSettings.py during installation,\n')
        Sfile.write('# update or by birchadmin --> Preferences --> Settings\n')
        for k in self.dict :
            Sfile.write(k + '=' + self.dict[k] + '\n')
        Sfile.close()

    def WriteBIRCHenvBourne(self,P):
        """
                Write bash code for setting BIRCHvariables to birch_settings_Bourne.source.
                Used for Bourne type shells eg. bash, sh
        """

        ENVFN = os.path.join(P.BIRCH, 'admin', 'birch_settings_Bourne' + '.source')
        Sfile = open(ENVFN,'w')
        Sfile.write('# DO NOT EDIT THIS FILE!\n')
        Sfile.write('# This file is automatically generated by BIRCHSettings.py during installation,\n')
        Sfile.write('# update or by birchadmin --> BIRCHSettings\n')
        #Enclose value of argument in single quotes. This is maninly for cases such as
        #BL_Terminal='gnome-terminal -e'
        for k in self.dict :
            Sfile.write(k + "='" + self.dict[k] + "'\n")
        Sfile.write('export ')
        for var in BIRCHvariables :
            Sfile.write(' ' + var)
        Sfile.write('\n')
        Sfile.close()
        chmod_ar(ENVFN)

    def WriteBIRCHenvCsh(self,P):
        """
                Write csh code for setting BIRCHvariables to birch_settings_csh.source.
                Used for C type shells eg. csh, tcsh
        """

        ENVFN = os.path.join(P.BIRCH, 'admin', 'birch_settings_csh'  + '.source')
        Sfile = open(ENVFN,'w')
        Sfile.write('# DO NOT EDIT THIS FILE!\n')
        Sfile.write('# This file is automatically generated by BIRCHSettings.py during installation,\n')
        Sfile.write('# update or by birchadmin --> BIRCHSettings\n')
        for k in self.dict :
            Sfile.write('setenv ' +k + ' ' + self.dict[k] + '\n')
        Sfile.close()
        chmod_ar(ENVFN)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class BLChoices:
    """
        Data and methods for BLHelper.blmenu file.
        """
    def __init__(self,P):
        """
          Initializes arguments:
                dict = {}
          """
        self.dict = {}
        self.ReadBLChoices(P)
        if DEBUG :
            print('- - - - - BIRCH Choices - - - - -')
            for var in BIRCHvariables:
                print('    ' + var)
                print('    ' + str(self.dict[var]))


    def ReadBLChoices(self,P):
        """
                Read choice tuples from BL_variable_<platform>.list file.
                Choices are checked to see if the binary file for the program exists.
                Choices are only added to the list if the binary is found.
        """
        for var in BIRCHvariables:
            self.dict[var] = []
            VFN = os.path.join(P.BIRCH, 'admin', 'Settings.default', var +'.list')
            print(VFN)
            if os.path.exists(VFN) :
                Vfile = open(VFN,'r')
                for line in Vfile :
                    line = line.strip()
                    tokens = line.split(',')
                    if len(tokens) == 2 :
                        self.dict[var].append([tokens[0],tokens[1]])
                Vfile.close()

    def WriteBLmenu(self,P,Settings):
        """
                Create a BioLegato .blmenu file with choices for each helper application.
                This function reads a template .blmenu filefunction and replaces tags of the form

                <REPLACE name=BLvariable>

                with a tuple list for a combobox. Output is written to HelperApps.blmenu.
        """

        def GetValue(line) :
            """
            Read value from label/value pair of the form label=value
            """
            #Get rid of newline and terminal '>' character
            line=line.strip()[:-1]
            tokens =line.split("=")
            return tokens[1]

        def GetDefaultNum(var,Settings) :
            """
            If var is in Choices, return the index of the choice.
            Otherwise, return the value for a Custom command,
            which will always be the last choice in the list
            """
            defaultnum = 0
            l = len(self.dict[var])
            FOUND = False
            while (defaultnum < l) and (not FOUND) :
                if self.dict[var][defaultnum][1] == Settings.dict[var] :
                    FOUND = True
                else :
                    defaultnum += 1
            return defaultnum


        Directory = os.path.join(P.BIRCH, 'dat', 'birchadmin', 'PCD', 'Preferences')
        TemplateFN = os.path.join(Directory, 'Settings.blmenu.template')
        OutputFN = os.path.join(Directory, 'Settings.blmenu')
        Templatefile = open(TemplateFN,'r')
        OutputFile = open(OutputFN,'w')
        OutputFile.write('# DO NOT EDIT THIS FILE!\n')
        OutputFile.write('# This file is automatically generated by BIRCHSettings.py during installation,\n')
        OutputFile.write('# update or by birchadmin --> Preferences --> Settings\n')
        INDENT8 = '        '
        INDENT12 = '            '
        defaultnum = 0
        for line in Templatefile :

            if line.startswith("<REPLACE defaultnum=") :
                var = GetValue(line)
                if var in BIRCHvariables :
                    defaulttext = ""
                    defaultnum = GetDefaultNum(var,Settings)
                    OutputFile.write(INDENT8 + 'default     ' + str(defaultnum) + "\n" )

            elif line.startswith("<REPLACE list=") :
                var = GetValue(line)
                if var in BIRCHvariables :
                    for t in self.dict[var] :
                        label = t[0]
                        value = t[1]
                        OutputFile.write(INDENT12 + '"' + label + '" "' + value + '"' + "\n" )

            elif line.startswith("<REPLACE defaulttext=") :
                var = GetValue(line)
                if (var in BIRCHvariables) :
                    if defaultnum == len(self.dict[var]) : # ie. Custom command
                        defaulttext = Settings.dict[var]
                        OutputFile.write(INDENT8 + 'default    ' + '"' + defaulttext + '"' + "\n" )
                    #else:
                        #defaulttext = ""
                else :
                    defaulttext = ""
                    OutputFile.write(INDENT8 + 'default    ' + '"' + defaulttext + '"' + "\n" )
                defaultnum = 0

            else:
                OutputFile.write(line)

        OutputFile.close()
        chmod_arx(OutputFN)


#======================== MAIN PROCEDURE ==========================
def main():
    """
        Called when not in documentation mode.
        """

    # Read parameters from command line
    P = Parameters()

    # Special case: If $BIRCH/lcoal/admin/BIRCH.settings doesn't exist, we treat it as a fresh install.
    # This should only occur when upgrading from an earlier version of BIRCH pre-dating
    # the BIRCH settings ie. BIRCH Version 3.0 or earlier.
    if not os.path.exists(P.SFN) :
        P.UPDATE = False
        P.INSTALL = True

    Settings = BIRCHSettings(P)

    if P.INSTALL:
        print("Install")
        Choices = BLChoices(P)
        for var in BIRCHvariables :
            # Select the best choice for this platform
            # This will be the first one in the choice list
            Settings.dict[var] = Choices.dict[var][0][1]
        Settings.WriteBIRCHSettings(P.SFN)
        Settings.WriteBIRCHenvBourne(P)
        Settings.WriteBIRCHenvCsh(P)
        Choices.WriteBLmenu(P,Settings)

    elif P.UPDATE:
        print("Update")
        Choices = BLChoices(P)
        Settings.WriteBIRCHSettings(P.SFN)
        Settings.WriteBIRCHenvBourne(P)
        Settings.WriteBIRCHenvCsh(P)
        Choices.WriteBLmenu(P,Settings)

    elif P.SETVAR:
        print("Setvar")
        Choices = BLChoices(P)
        Settings.dict[P.VNAME] = P.VALUE
        Settings.WriteBIRCHSettings(P.SFN)
        Settings.WriteBIRCHenvBourne(P)
        Settings.WriteBIRCHenvCsh(P)
        Choices.WriteBLmenu(P,Settings)

    else:
        print(USAGE)

    BM.exit_success()


if (BM.documentor() or "-test" in sys.argv):
    pass
else:
    main()
