#!/usr/bin/env python
#
# @package      hubzero-submit-client
# @file         submit
# @author       Steven Clark <clarks@purdue.edu>
# @copyright    Copyright (c) 2012-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2012-2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#
import os
import sys
import logging

LOGDIRECTORY      = os.getcwd()
HUBLOGFILE        = '.submit.log'
APPLICATIONLOGGER = logging.getLogger('')

CONFIGURATIONDIRECTORY = os.path.join(os.sep,'etc','submit')
CONFIGURATIONFILE      = 'submit-client.conf'
CONFIGFILEPATH         = os.path.join(CONFIGURATIONDIRECTORY,CONFIGURATIONFILE)

from hubzero.submit.LogMessage   import getLogIDMessage as getLogMessage, logSetMessageFile
from hubzero.submit.SubmitClient import SubmitClient

def openLogger(logDirectory,
               hubLogFile):
   class EmptyFilter(logging.Filter):
      """
      This is a filter which rejects empty messages

      """

      def filter(self,record):
         if record.getMessage() == "":
            emptyRecord = True
         else:
            emptyRecord = False

         return(not emptyRecord)

   APPLICATIONLOGGER.setLevel(logging.ERROR)

   if sys.stderr.isatty():
      logHandler = logging.StreamHandler(sys.stderr)
      fdLogFile = sys.stderr.fileno()
      logSetMessageFile(sys.stderr)
   else:
      hubLogPath = os.path.join(logDirectory,hubLogFile)
      logHandler = logging.FileHandler(hubLogPath)
      fdLogFile = logHandler.stream.fileno()

   emptyFilter = EmptyFilter()
   logHandler.addFilter(emptyFilter)

   logFormatter = logging.Formatter('%(asctime)s %(message)s','[%a %b %d %H:%M:%S %Y]')
   logHandler.setFormatter(logFormatter)
   APPLICATIONLOGGER.addHandler(logHandler)

   return(fdLogFile)


def client(configFilePath):
   submitClient = SubmitClient(configFilePath)

   if submitClient.configure():
      (parseCommandError,contactServer) = submitClient.parseCommandArguments()
      if not parseCommandError:
         if not submitClient.reportDebugOutput():
            logSetMessageFile(None)
         else:
            APPLICATIONLOGGER.setLevel(logging.DEBUG)
         if contactServer:
            submitClient.getUserAttributes()
            if submitClient.connectToServer():
               submitClient.run()
      else:
         submitClient.sendUserErrorNotification("Command line argument parsing failed.")
         sys.exit(1)
   else:
      submitClient.sendUserErrorNotification("submit configuration failed.")
      sys.exit(1)


if __name__ == '__main__':
   fdLogFile = openLogger(LOGDIRECTORY,HUBLOGFILE)

   client(CONFIGFILEPATH)

   sys.exit(0)


