#!/usr/bin/python
#
# Copyright 2009 Marcelo Boveto Shima.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA    02110-1301, USA.
#
# Author: marceloshima@gmail.com (Marcelo Boveto Shima)

import os
import sys
import traceback

from tacix.server.nx.session import *

try:
    sessionid = os.getenv('NXSESSIONID')
    if sessionid is None:
        print( "Session id not found" )
        sys.exit(1)

    session_type = os.getenv('NX_SESSION_TYPE')
    if session_type is None or session_type == 'nx':
        nxsession = SessionWorker(sessionid)
    elif session_type == 'nx-shadow':
        nxsession = ShadowSessionWorker(sessionid)

    if nxsession is None:
        print( "Could not start the session" )
        sys.exit(1)

    nxsession.wait()

except Exception, e:
    import tacix.util
    logger = tacix.util.get_home_logger('tacix-session')
    logger.error("Got exception")
    trace = traceback.format_exc()
    for line in trace.split('\n'):
        logger.error('%s' % line)
    logger.error("Exception finished")

