"""
     qtgui/pimple.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009-2011 University of York
     Copyright (C) 2012 STFC

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     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 Lesser General Public License for more details.
"""
import sys,os
import shutil


def getCCP4MG_DIR():
	target = os.path.join(os.path.realpath(sys.argv[0]),"..")
	abstarget = os.path.abspath(target)
	targetdir = os.path.dirname(abstarget)
	return targetdir
	
def setup_pythonpath():
	CCP4MG = getCCP4MG_DIR()
	sys.path.append(CCP4MG)
	sys.path.append(os.path.join(CCP4MG,"lib"))

def setup_environment():
	targetdir = getCCP4MG_DIR()
	MGROOT = targetdir
	CCP4MG=MGROOT
	CCP4_MGDATA=os.path.join(CCP4MG,"data")
	PYTHONHOME=os.path.join(CCP4MG,"pythondist")
	if sys.platform == "darwin":
		PYTHONHOME=os.path.join(CCP4MG,"Frameworks/Python.framework/Versions/Current")
		DYLD_FRAMEWORK_PATH=os.path.join(CCP4MG,"Frameworks")
		os.environ["DYLD_FRAMEWORK_PATH"] = DYLD_FRAMEWORK_PATH
	MONOMER_LIBRARY=os.path.join(CCP4_MGDATA,"monomer_library")
	CLIBD=os.path.join(CCP4MG,"ccp4_suite","lib","data")
	
	if sys.platform == "win32":
		path_sep = ';'
	else:
		path_sep = ':'
	DYLD_LIBRARY_PATH=MGROOT + path_sep + \
		os.path.join(CCP4MG,"lib")
	if sys.platform == "darwin":
		PYTHONPATH=MGROOT + path_sep + \
		os.path.join(CCP4MG,"lib")
		os.environ["PYTHONPATH"] = PYTHONPATH

	os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = DYLD_LIBRARY_PATH
	os.environ["LD_LIBRARY_PATH"] = DYLD_LIBRARY_PATH
	os.environ["LIBPATH"] = DYLD_LIBRARY_PATH
	if sys.platform == "win32":
		OLDPATH=os.environ["PATH"]
		PATH=os.path.join(CCP4MG,"bin") + path_sep + os.path.join(CCP4MG,"pythondist","bin") + path_sep + DYLD_LIBRARY_PATH + path_sep + OLDPATH
		os.environ["PATH"] = PATH
		os.environ["PYTHONPATH"] = PATH
		if not os.environ.has_key("HOME") or os.environ["HOME"] == "":
			try:
				HOME=os.environ["USERPROFILE"]
				os.environ["HOME"] = HOME
			except:
				pass
	else:
		os.environ["PATH"] = os.path.join(CCP4MG,"bin") + path_sep + os.environ["PATH"]
	
	os.environ["CCP4MG"] = CCP4MG
	os.environ["CCP4_MGDATA"] = CCP4_MGDATA
	os.environ["MONOMER_LIBRARY"] = MONOMER_LIBRARY
	os.environ["CLIBD"] = CLIBD
	os.environ["PYTHONHOME"] = PYTHONHOME
	os.environ["CCP4MG_CIRCLE_TEXTURE_FILE"] = os.path.join(CCP4MG,'qticons','circles.png')


if __name__ == '__main__':
	import socket
	setup_environment()
	ccp4mg = getCCP4MG_DIR()
	if sys.platform == "win32":
		mainpy = os.path.join("qtgui","MGQTmatplotlib.py")
		import subprocess
		args = ["python",mainpy]
		for sysarg in sys.argv[1:]:
			args.append(sysarg)
		retval = subprocess.call(args)
	else:
		if sys.platform == "win32":
			pywrap = "pywrap"
		else:
			pywrap = os.path.join(os.environ["CCP4MG"],"QtMG")
		mainpy = os.path.join(ccp4mg,"qtgui","main.py")
		os.execvp(pywrap,[pywrap,mainpy])
	print "Finished"
