#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys, os, shutil

def install():
    print 'Installing...'
    if not os.path.exists(vsldir): os.mkdir(vsldir)
    pyfiles = [f for f in os.listdir('VDK/src/') if not os.path.isdir(f) and f.endswith('.py')]
    for p in pyfiles:
        shutil.copy('VDK/src/'+p,vsldir)
    shutil.copy('VDK/bin/vslc',INSTALLDIR)
    shutil.copy('VRE/vsl',INSTALLDIR)
    shutil.copy('VRE/vsl',INSTALLDIR)
    shutil.copy('vmake/vmake',INSTALLDIR)
    os.chmod(INSTALLDIR+'vslc',111)
    os.chmod(INSTALLDIR+'vsl',111)
    os.chmod(INSTALLDIR+'vmake',111)
    print 'Installed!'

def uninstall():
    print 'Desinstalling...'
    if os.path.exists(vsldir): os.system('rm -R '+vsldir)
    if os.path.exists(INSTALLDIR+'vslc'): os.system('rm '+INSTALLDIR+'vslc')
    if os.path.exists(INSTALLDIR+'vsl'): os.system('rm '+INSTALLDIR+'vsl')
    if os.path.exists(INSTALLDIR+'vsl'): os.system('rm '+INSTALLDIR+'vmake')
    print 'Desinstalled!'

def printHelp():
    print '''usage : python setup.py COMMANDE
            - install : installation of the compiler and runtime environement
            - uninstall : uninstall the compiler et the runtime environement
                (this two last command need root password)'''

if __name__ == '__main__':
    pythonpath = [path for path in sys.path if path.find('python')!=-1 and path.find('site-packages')!=-1]
    print '========================================'.center(80)
    print '=====         VSL Compiler         ====='.center(80)
    print '===== David Jacod , Anthony Mougin ====='.center(80)
    print '========================================'.center(80)
    if len(pythonpath)==0:
        print ' * No python path found. Installation failed'
        sys.exit(-1)

    if os.name=='posix': INSTALLDIR = '/usr/local/bin/'
    elif os.name=='nt': INSTALLDIR = 'c:/VSLCompiler/'

    pythonpath=pythonpath[0]
    vsldir = pythonpath+'/vslcomp/'
    if len(sys.argv)<=2:
        if sys.argv[1]=='install': install()
        elif sys.argv[1]=='uninstall': uninstall()
        else: printHelp()
    else:
        printHelp()
