#!/opt/sunpy3/bin/python3
import os
import sys
import sunhpc
import sunhpc.invoke
import logging.handlers

if sys.version_info.major < 3:
    print("Sunhpc cluster supports only Python3. Rerun application in Python3 environment.")
    exit(0)

from sunhpc.console import SunhpcConsole

sunhpc_home = os.environ.get('SUNHPCHOME')
if sunhpc_home:
    log_file = os.path.join(sunhpc_home, 'logs', 'runSunhpc.log')
else:
    log_file = os.path.join('/opt/sunhpc', 'logs', 'runSunhpc.log')

log_handler     = logging.handlers.RotatingFileHandler(filename=log_file, maxBytes=500000)
log_formatter   = logging.Formatter("%(asctime)s %(levelname)s %(name)s       %(message)s")
log_handler.setFormatter(log_formatter)
LOGGER          = logging.getLogger()
LOGGER.setLevel(logging.DEBUG)
LOGGER.addHandler(log_handler)

def sunhpcApplication(argv):
    hpc = SunhpcConsole()
    if len(argv[1:]):
        hpc.nonInteractive(argv)
    else:
        hpc.start()

if __name__ == "__main__":
    try:
        sunhpcApplication(sys.argv)
    except (KeyboardInterrupt, SystemExit):
        pass
