#! /usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Inuvika Inc.
# http://www.inuvika.com
#
# Copyright (C) 2009-2012 Ulteo SAS
# http://www.ulteo.com
# Author Gauvain POCENTEK <gauvain@ulteo.com> 2009
# Author Julien LANGLOIS <julien@ulteo.com> 2009, 2010, 2012
# Author Laurent CLOUET <laurent@ulteo.com> 2010
#
# 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; version 2
# of the License
#
# 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.

import os
import sys

from ovd_shells.Application import Application
from ovd_shells.CheckFork import CheckFork
from ovd_shells.CheckOpenOffice import CheckOpenOffice
from ovd_shells.Logger import Logger
from ovd_shells.Monitoring import Monitoring
from ovd_shells.Platform.CheckWorkingDir import CheckWorkingDir
from ovd_shells.Platform import _platform as Platform
from ovd_shells import Util

def main(user_sdir):
	path = os.path.join(os.environ['OVD_SESSION_DIR'], "instances")
	if not os.path.exists(path):
		Logger.error("no such directory %s"%(path))
		sys.exit(1)
	
	if not len(sys.argv) > 1:
		Logger.error("no app_id given")
		sys.exit(1)
	
	if not sys.argv[1].isdigit():
		Logger.error("arg is not a integer")
		sys.exit(1)
	
	application_id = int(sys.argv[1])
	args = sys.argv[2:]
	
	application = Application(application_id, args)
	
	if not application.isAvailable():
		Logger.error("Application %d is not available"%(application_id))
		sys.exit(2)
	
	cmd = application.getFinalCommand()
	if cmd is None:
		Logger.error("No available command")
		sys.exit(2)
	
	modules = []
	modules.append(CheckFork(application))
	modules.append(CheckOpenOffice(application))
	modules.append(Monitoring(application))
	modules.append(CheckWorkingDir(application))
	
	
	for module in modules:
		module.beforeStartApp()

	os.environ["OVD_APP_ID"] = str(application_id)
	Platform.launch(cmd, True)
	
	for module in modules:
		module.afterStartApp()
	
	sys.exit(0)


if __name__ == "__main__":
	user_sdir = Util.get_session_dir()
	if user_sdir is None:
		sys.exit(1)

	Util.redirect_to_dump(user_sdir)
	Logger.init()
	main(user_sdir)
