#!/usr/bin/env python
###########################################################################
# @package makeAll
# @brief This script installs MOE in user-provided install dir
# It takes as input 
# (1) install directory
# It outputs 
# bin/ libexec/ share/ doc/ within install dir
#
# @author Harini Eavani
#
# @link: https://www.cbica.upenn.edu/sbia/software/
#
# @author: sbia-software@uphs.upenn.edu
##########################################################################

import os, sys
from MOEUtils import *

## check input arguments
if len(sys.argv) < 2:
    cryandexit('python code - does not require compilation/build')
    
if not sys.argv[1] in ["test","install"]:
    cryandexit('unknown make target -- exiting')    
    
if sys.argv[1]=='test':
    print('Running test code')
    cmdArray=['./test.py']
    execCmd(cmdArray,verbose=1,simulate=False)    
    exit    

if sys.argv[1]=='install':

    if len(sys.argv) < 3:
        cryandexit('provide installation directory - make install <installDir>')
    
    ## check if installDir exists
    installDir = os.path.realpath(sys.argv[2])
    if not os.path.exists(installDir):
        os.makedirs(installDir)
    elif not os.path.isdir(installDir):
        cryandexit("Install argument is not a directory", installDir)    
    ## make path full path
    #installDir = os.path.abspath(installDir)
        
    os.makedirs(installDir+'/bin')
    os.makedirs(installDir+'/doc')
    os.makedirs(installDir+'/libexec')
    os.makedirs(installDir+'/share')
    
    # replace install directory in main executible
    s = open("moe").read()
    s = s.replace('PATH_TO_INSTALL', installDir+'/libexec')
    f = open("moe", 'w')
    f.write(s)
    f.close()
    
    ## copy all python dependencies to libexec
    for pyf in ['SV_fuzzy.py','draw_moe_plot.py','MOEUtils.py','QP.py','test.py']:
        cmdArray=['cp',pyf,installDir+'/libexec/']
        execCmd(cmdArray,verbose=1,simulate=False,shell=True)
        pyFile=installDir+'/libexec/'+pyf
        if not fileExists(pyFile):
            cryandexit("Error in copying python files to libexec dir, ", pyFile," not found")
    
    cmdArray=['cp','moe',installDir+'/bin/']
    execCmd(cmdArray,verbose=1,simulate=False,shell=True)
    pyFile=installDir+'/bin/'+'moe'
    if not fileExists(pyFile):
        cryandexit("Error in copying python files to libexec dir, ", pyFile," not found")
    
    
    # make moe executible
    cmdArray=['chmod','u+x',installDir+'/bin/moe']
    execCmd(cmdArray,verbose=1,simulate=False,shell=True)
        
    ## copy test files to share
    cmdArray=['cp','-r','test/',installDir+'/share/']
    execCmd(cmdArray,verbose=1,simulate=False,shell=True)
    if not fileExists(installDir+'/share/'+'test/Case1/Case1.csv'):
        cryandexit("Error in copying test files to share dir, test/Case1/Case1.csv not found")
        
    ## generate documentation
    cmdArray=['cd','../',';','doxygen','Doxyfile']
    execCmd(cmdArray,verbose=1,simulate=False,shell=True)
    if not os.path.exists('../docs/'):
        cryandexit("Error in building documentation, docs/ not found")
        
    ## copy to docs
    cmdArray=['cp','-r','../docs/*',installDir+'/doc/']
    execCmd(cmdArray,verbose=1,simulate=False,shell=True)
    if not os.path.exists(installDir+'/doc/'):
        cryandexit("Error in copying documentation to doc dir, doc/ not found")
        