#!/usr/libexec/platform-python -s
#  -*- coding: utf-8 -*-
# *****************************************************************************
# NICOS, the Networked Instrument Control System of the MLZ
# Copyright (c) 2009-2021 by the NICOS contributors (see AUTHORS)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Georg Brandl <georg.brandl@fz-juelich.de>
#
# *****************************************************************************

import argparse
import signal
import sys
from os import path

sys.path.insert(0, path.dirname(path.dirname(path.realpath(__file__))))

from nicos.core.sessions.simulation import SimulationSession

parser = argparse.ArgumentParser()
parser.add_argument('sock', type=str, help='0MQ communication address')
parser.add_argument('uuid', type=str, help='the uuid of the code')
parser.add_argument('setups', help='comma separated list of setup files')
parser.add_argument('user', type=str, help='user name,user Level')
parser.add_argument('code', type=str, help='code to be executed')
parser.add_argument('--quiet', help='send only results', action='store_true',
                    default=False)
parser.add_argument('--debug', help='send log messages to stderr',
                    action='store_true', default=False)

opts = parser.parse_args()

# kill forcibly after 10 minutes
if hasattr(signal, 'alarm'):
    signal.alarm(600)

sys.exit(SimulationSession.run(opts.sock, opts.uuid, opts.setups.split(','),
                               opts.user, opts.code, opts.quiet, opts.debug))
