#!/usr/bin/env python
"""
The main purpose of this script is to abstract the call to computation without GUI.
It help to use the computation for released version and development version.
This forward the call to this script to the SimpleCompute class in CASSIS.

Author: M. Boiziot
Date: 05/29/17
Version: 1
Licence: CeCILL-C
Tested on: CASSIS 4.3
"""

from os.path import dirname, abspath, join, exists
import sys
import subprocess

CASSIS_COMPUTATION_CLASS = "eu.omp.irap.cassis.gui.SimpleCompute"
CASSIS_PATH = dirname(dirname(dirname(dirname(abspath(__file__)))))
CASSIS_CLASSES_DEV_PATH = join(CASSIS_PATH, "target", "classes")
CASSIS_LIB_DEV_PATH = join(CASSIS_PATH, "target", "lib")
CASSIS_MAIN_JAR_PATH = join(CASSIS_PATH, "bin", "cassis.cassisAlone.jar")


def isDev():
	return exists(CASSIS_CLASSES_DEV_PATH) and exists(CASSIS_LIB_DEV_PATH) 

def getCommandLineDev():
	return "java -cp {}/*:{} {}".format(CASSIS_LIB_DEV_PATH, CASSIS_CLASSES_DEV_PATH, CASSIS_COMPUTATION_CLASS)

def getCommandLineRelease():
	return "java -cp {} {}".format(CASSIS_MAIN_JAR_PATH, CASSIS_COMPUTATION_CLASS)

def getFullCommand(args):
	base = getCommandLineDev() if isDev() else getCommandLineRelease()
	return base + getFormatedArgs(args)

def getFormatedArgs(args):
	st = ""
	for arg in args:
		st += " " + arg
	return st


if __name__ == '__main__':
	fullCommand = getFullCommand(sys.argv[1:])
	result = subprocess.call(fullCommand, shell=True)
	sys.exit(result)
