# -*- python -*-
#
#  SConstruct file for building json-fortran using scons
#
#  This builds using the same commands as are used in json-fortran's
#  "build.sh" script, but with proper dependency checking.  It also
#  deals gracefully with robodoc not being installed on the system.
#

import sys, os
sys.path.append('..')
from os.path import join, realpath
from SCons.Environment import Environment
from FeffBuild import CompilationEnvironment, InstallEnvironment, FindOtherObjects

env  = Environment()
here = os.getcwd()
ienv = InstallEnvironment()

if env['FORTRAN'] == 'gfortran':
    env = Environment(F90FLAGS = '-O2 -fbacktrace -g -Wall -Wextra -Wno-maybe-uninitialized -pedantic -std=f2008 -J'+here,)
elif env['FORTRAN'] == 'ifort':
    env = Environment(F90FLAGS = '-O2 -warn -stand f08 -diag-disable 7601 -traceback -module '+here,)

src = join('json_module.f90')
obj = join('json_module.o')
ar  = join('libjsonfortran.a')
mod = join('json_module.mod')

## this HAS to come after the if: elif: block above, which will
## overwrite this change to the PATH
if os.name == 'nt':
    env.PrependENVPath('PATH', join('C:', os.sep, 'Program Files',
                                    'mingw-w64', 'x86_64-4.9.2-win32-seh-rt_v3-rev1',
                                    'mingw64', 'bin'))


env.Library(ar, src,
            FORTRANMODDIR=here, # this tells scons that we want the .mod file to be installed in to lib/
            LIBPREFIX='',       # this tells scons /not/ to assume that the mod file should be libjson_module.mod
        )


## ------ installation ----------

# import larch
# if os.name == 'nt':
#     prefix = larch.larchlib.larchdir
#     dlldir = larch.larchlib.get_dlldir()
#     libinstall = join(prefix, 'dlls', dlldir)        #'C:\MinGW\lib'
#     docinstall = join(prefix, 'doc', 'json-fortran') #'C:\MinGW\doc\json-fortran'
# else:
#     libinstall = larch.larchlib.larchdir
#     docinstall = '/usr/local/share/doc/json-fortran'

#env.Install(libinstall, [ar,mod])
#env.Alias('install', libinstall)

env.Install(ienv['i_lib'], [ar,mod])
env.Alias('install', ienv['i_lib'])
