#!/usr/bin/env python

import sys, os, glob, platform

if os.geteuid() != 0:
	print
	print("You need to have root privileges to run this script.")
	print
	sys.exit()

#
# global variables
#

THISDIR = os.path.abspath(os.path.split(__file__)[0])+"/"
if platform.system() == "Linux":
	EXTENSION = ".so"
	PLATFORM = "linux"
	ADSKDIR = "/usr/autodesk/"
	# if platform.python_compiler().split(" ")[1][0] == "4": 
	# 	GCC = "482"
	# else: 
	# 	GCC = "531"

	MODULE = """+ HardMesh any """+THISDIR+"""
plug-ins: plug-ins/<VERSION>_"""+PLATFORM+"""/
"""
else:
	EXTENSION = ".bundle"
	PLATFORM = "osx"
	ADSKDIR = "/Applications/Autodesk/"
	MODULE = """+ HardMesh any """+THISDIR+"""
plug-ins: plug-ins/<VERSION>_"""+PLATFORM+"""/
"""

def validate_directory(directory):

	if not os.path.isdir(directory):
		print
		print("Directory not found: "+directory)
		print
		sys.exit()

#
# license agreement
#

if not os.path.isfile(THISDIR+"EULA.txt"):
		print
		print("File not found: "+THISDIR+"EULA.txt")
		print
		sys.exit()

f = open(THISDIR+"EULA.txt")
s = f.read()
f.close()

print
print("LICENSE AGREEMENT")
print("-----------------")
print
print(s)
print
result = raw_input("I accept the agreement [Y/N]: ")
if result != "Y" and result != "y": sys.exit()

#
# available SOuP versions
#

validate_directory(THISDIR+"plug-ins/")
if PLATFORM == "linux":
	PLUGIN_VERSIONS = glob.glob(THISDIR+"plug-ins/20*_"+PLATFORM+"/hmTools"+EXTENSION)

else:
	PLUGIN_VERSIONS = glob.glob(THISDIR+"plug-ins/20*_"+PLATFORM+"/hmTools"+EXTENSION)
if not len(PLUGIN_VERSIONS):
		print
		print("Cannot find any versions of HardMesh inside: "+THISDIR)
		print
		sys.exit()

for i in range(len(PLUGIN_VERSIONS)):
	PLUGIN_VERSIONS[i] = os.path.split(os.path.split(PLUGIN_VERSIONS[i])[0])[1].replace("maya", "").split("_")[0]

#
# installed Maya versions
#

directory = ADSKDIR
if not os.path.isdir(directory):
	print
	print("Directory not found: "+ADSKDIR)
	directory = raw_input("Provide a directory with installed versions of Maya: ")
	if directory[-1] != "/": directory += "/"
validate_directory(directory)

if PLATFORM == "linux": l = glob.glob(directory+"maya20*/modules/")
else: l = glob.glob(directory+"maya20*/Maya.app/Contents/modules/")
if not len(l):
	print
	print("Cannot find any installed versions of Maya inside: "+directory)
	directory = raw_input("Provide a directory with installed versions of Maya: ")
	if directory[-1] != "/": directory += "/"
validate_directory(directory)

if PLATFORM == "linux": l = glob.glob(directory+"maya20*/modules/")
else: l = glob.glob(directory+"maya20*/Maya.app/Contents/modules/")
if not len(l):
	print
	print("Cannot find any installed versions of Maya inside: "+directory)
	print
	sys.exit()

print
print("INSTALLATION")
print("------------")
print
print("Found "+str(len(l))+" installed versions of Maya:")
SUPPORTED_MAYA_VERSIONS = []
for d in sorted(l):
	if PLATFORM == "linux": v = d.replace(directory+"maya", "").replace("/modules/", "")
	else: v = d.replace(directory+"maya", "").split("/")[0]
	if v not in PLUGIN_VERSIONS: v += " (not supported)"
	else: SUPPORTED_MAYA_VERSIONS.append(v)
	print("\t"+v)

#
# install SOuP as a module
#

installed = False
for v in sorted(SUPPORTED_MAYA_VERSIONS):
	print
	result = raw_input("Do you want to install HardMesh for Maya "+v+" ? [Y/N]: ")
	if result == "Y" or result == "y":
		if PLATFORM == "linux": filepath = directory+"maya"+v+"/modules/hardmesh.mod"
		else: filepath = directory+"maya"+v+"/Maya.app/Contents/modules/hardmesh.mod"
		f = open(filepath, "w")
		f.write(MODULE.replace("<VERSION>", v))
		f.close()
		installed = True
		print("Result: "+filepath)

#
# view README
#

if installed:
	print
	print("Installation completed !")
	print

	if os.path.isfile(THISDIR+"readme.txt"):
		if PLATFORM == "linux": os.system("xdg-open "+THISDIR+"readme.txt &")
		else: os.system("open -a TextEdit "+THISDIR+"readme.txt &")
