## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (c) 2007-2008 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------
##
## FIXME: Add full support for built-in ssh client

import wx
import os
import config
import logging

SSH_OPTIONS = [ 
    "REMOTE_HOST",
    "REMOTE_PORT",
    "REMOTE_USER",
    "RSH_KEY",
    "REMOTE_BIN", 
    "REMOTE_OUTPUT_DIR",
    "REMOTE_LIBRARY_PATH"]

opts = {}
prefs = {}

def _add_opt(name, default_val):
    if "SILK_"+name in os.environ:
        print "Found %s in environment" % ("SILK_"+name)
        val = opts[name] = os.environ["SILK_" + name]
    elif name in prefs: 
        print "Found %s in preferences" % name
        val = opts[name] = prefs[name]
    else: 
        val = opts[name] = default_val
    print "Option '%s' = '%s'" % (name, val)    
    return opts[name]
    
def getSSHDefaults():
    global opts
    # Copy the options
    sshOptions = {}
    for o in SSH_OPTIONS: 
        sshOptions[o] = opts[o]
    return sshOptions
    
def workRemotely():
    return opts["WORK_REMOTE"] == "True"
    
def getLargeQueryThreshold():
    return int(opts["LARGE_QUERY_THRESHOLD"])
    
def getMaxQueryRecords():
    return int(opts["MAX_QUERY_RECORDS"])
    
def getDefaultClass():
    return opts["DEFAULT_FGLOB_CLASS"]
    
def useExternalSSH():
    return opts["USE_EXTERNAL_SSH"] == "True"
    
def buildExternalRemoteShellCommand(command_line):
    if opts["USE_EXTERNAL_SSH"] == "True":
        cmd = opts["RSH_CMD"]
    else:
        cmd = ""
    cmd = cmd.replace("$user", opts["REMOTE_USER"])
    cmd = cmd.replace("$host", opts["REMOTE_HOST"])
    cmd = cmd.replace("$key", opts["RSH_KEY"])
    cmd = cmd + " " + command_line
    return cmd
    
def buildExternalDownloadCommand(remote_file, local_file):
    cmd = opts["FILE_DOWNLOAD_CMD"]
    cmd = cmd.replace("$user", opts["REMOTE_USER"])
    cmd = cmd.replace("$host", opts["REMOTE_HOST"])
    cmd = cmd.replace("$key", opts["RSH_KEY"])
    cmd = cmd.replace("$local", local_file)
    cmd = cmd.replace("$remote", remote_file)
    return cmd    
    
def dump():
    for k in opts.keys():
        print "silk.opt.%s = '%s'" % (k, opts[k])

def init():
    global prefs
    global opts
    
    documentsDir = wx.StandardPaths.Get().GetDocumentsDir()
    print "Using documents directory: %s" % documentsDir
    pref_list = ["USE_EXTERNAL_SSH", "WORK_REMOTE", "LOCAL_SILK_BIN", "RSH_CMD", "REMOTE_BIN",
        "REMOTE_HOST", "REMOTE_USER", "RSH_KEY", 
        "LOCAL_OUTPUT_DIR", "REMOTE_OUTPUT_DIR", "FILE_DOWNLOAD_CMD",
        "MAX_QUERY_RECORDS"]
    config_file_exists = config._read_prefs("silk", prefs, pref_list)
    print "config_file_exists = %s" % config_file_exists
    # FIXME: How can I put these executables in a sub-directory?
    opts["INSTALL_DIR"] = os.getcwd()
    # User-configurable options (defaults)
    #_add_opt("WORK_REMOTE", "True")
    # FIXME: need to change this when I re-enable local mode
    opts["WORK_REMOTE"] = "True"
    _add_opt("REMOTE_HOST", "localhost")
    _add_opt("REMOTE_PORT", "22")
    _add_opt("LOCAL_COPY_CMD", "cp")
    _add_opt("LOCAL_SILK_BIN", opts["INSTALL_DIR"])
    _add_opt("LARGE_QUERY_THRESHOLD", "720")
    # Remote file access
    _add_opt("USE_EXTERNAL_SSH", "False")
    _add_opt("REMOTE_USER", "")
    _add_opt("RSH_CMD", "ssh")
    _add_opt("RSH_KEY", "")#"/Users/ajk/.ssh/id_rsa")
    # FIXME: before release
    _add_opt("DEFAULT_FGLOB_CLASS", "all")
    _add_opt("FILE_DOWNLOAD_CMD", "scp")
    _add_opt("REMOTE_DATA_DIR", "/data")
    _add_opt("MAX_QUERY_RECORDS", "500000")
    _add_opt("REMOTE_BIN", "")
    _add_opt("REMOTE_OUTPUT_DIR", "")
    _add_opt("REMOTE_LIBRARY_PATH", "") 
    # Other options
    _add_opt("LOCAL_OUTPUT_DIR", os.path.join(documentsDir, "isilk"))
    # Local files
    _add_opt("LOCAL_SET_DIR", os.path.join(documentsDir, "isilk", "sets"))
    return config_file_exists
        
