Import('env')
from os import path, environ
import gslab_scons as gs
import subprocess

sim_r = env['CONFIG']['global']['release_large']['simulation']
est_r = env['CONFIG']['global']['release_large']['estimation']
wgt_r = env['CONFIG']['global']['release_large']['weights']
sim_s = env['CONFIG']['global']['source']['simulation']
est_s = env['CONFIG']['global']['source']['estimation']
lib   = env['CONFIG']['global']['source']['lib']
plot  = env['CONFIG']['global']['release']['plot']
tbl   = env['CONFIG']['global']['release']['table']

# Needed for parallelizing SyntheticControl
GSLAB_R_EXE = gs.misc.get_executable('r', env['executable_names'])
GSLAB_STATA_EXE = gs.misc.get_executable('stata', env['executable_names'])
environ['GSLAB_STATA_EXE'] = GSLAB_STATA_EXE
environ['GSLAB_R_EXE'] = GSLAB_R_EXE
if not gs.misc.is_unix():
    subprocess.call(['setx', 'GSLAB_STATA_EXE', GSLAB_STATA_EXE], shell=True)
    subprocess.call(['setx', 'GSLAB_R_EXE', GSLAB_R_EXE], shell=True)


# ----------------------------------------------------------------------

target = []
source = [
    '#%s/estimation.m'       % est_s,
    '#%s/GetHTEEstimates.m'  % est_s,
    '#%s/GetMainEstimates.m' % est_s,
    '#%s/GetParams.m'        % sim_s,
    #
    '#%s/matlab/heterogeneous/ivregFE.m'        % lib,
    '#%s/matlab/heterogeneous/licols.m'         % lib,
    '#%s/matlab/heterogeneous/regFE.m'          % lib,
    '#%s/matlab/heterogeneous/se_iid.m'         % lib,
    '#%s/matlab/heterogeneous/estimateHTE_iv.m' % lib,
    '#%s/matlab/heterogeneous/estimateHTE.m'    % lib,
    #
    '#%s/matlab/estimation/eventstudyReg.m'     % lib,
    '#%s/matlab/estimation/eventstudyIFE.m'     % lib,
    '#%s/matlab/estimation/eventstudyIV.m'      % lib,
    '#%s/matlab/misc/loadparam.m'               % lib,
    '#%s/designs_main.csv'                      % lib
]

for design in env['designs_main']:
    source += [
        '#%s/%s.csv'         % (sim_r, design),
        '#%s/designs/%s.txt' % (sim_s, design)
    ]

    target += ['#%s/%s.mat' % (est_r, design),
               '#%s/HTE_%s.mat' % (est_r, design)]

env.BuildMatlab(target = target,
                source = source,
                log_ext = 'estimation')

# ----------------------------------------------------------------------

target = []
source = ['#%s/CannedHTEEstimates.do' % est_s,
          '#%s/stata/heterogeneous/sun_abraham.ado' % lib]

for design in env['designs_main']:
    source += ['#%s/%s.csv'     % (sim_r, design),
               '#%s/%s_ep0.csv' % (sim_r, design)]

    for type in ['bjs', 'sa', 'cdh']:
        target += ['#%s/HeterogeneityRobust/%s_%s_pctiles.dta' % (est_r, design, type),
                   '#%s/HeterogeneityRobust/%s_%s_pctiles.csv' % (est_r, design, type),
                   '#%s/HeterogeneityRobust/%s_%s_results.dta' % (est_r, design, type)]

env.BuildStata(target = target,
               source = source,
               log_ext = 'estimation')

# ----------------------------------------------------------------------

target = ['#%s/weights.txt' % wgt_r]
source = ['#%s/estimate_weights.do' % est_s,
          '#%s/BaselineV.csv' % sim_r]
env.BuildStata(target = target,
               source = source,
               log_ext = 'estimation')

# ----------------------------------------------------------------------

target = []
source = ['#%s/syntheticControlParallel.py' % est_s,
          '#%s/syntheticControlParallel.do' % est_s,
          '#%s/stata/sc/programSynthEst.do' % lib]

for design in env['designs_main']:
    source += ['#%s/%s.csv' % (sim_r, design)]
    for type in ['NotRecycling', 'NotRecycling_negW']:
        target += ["#%s/SyntheticControl/%s_%s.dta" % (est_r, design, type),
                   "#%s/SyntheticControl/%s_%s.csv" % (est_r, design, type)]

env.BuildPython(target = target,
                source = source,
                log_ext = 'estimation')

