# MATLAB Makefile for the model component
# This file replaces the execution logic of A_mainscript.m

# MATLAB command with common flags for batch execution
MATLAB := matlab -batch

# Directories
FIG_DIR := ../../bld/figures
DATA_DIR := ../../bld/data

# Grouped outputs for each script to avoid redundant executions
# GNU Make 4.3+ supports grouped targets using &:

B_OUTPUTS := Bellman_RHS.mat z.mat x.mat mu.mat par_baseline.mat \
             $(FIG_DIR)/baseline_fit_exit.pdf $(FIG_DIR)/baseline_fit_growth.pdf

C_OUTPUTS := Bellman_RHS_fb.mat z_fb.mat x_fb.mat mu_fb.mat \
             $(FIG_DIR)/f-Ddist-new.pdf

D_OUTPUTS := agg_vars_base.mat Bellman_RHS_GEsub.mat z_GEsub.mat x_GEsub.mat \
             mu_GEsub.mat agg_vars_GEsub.mat $(FIG_DIR)/stationary_distro_baseline.pdf \
             Bellman_RHS_GE_OC_sub.mat z_GE_OC_sub.mat x_GE_OC_sub.mat \
             mu_GE_OC_sub.mat agg_vars_GE_OC_sub.mat

E_OUTPUTS := par_restricted.mat Bellman_RHS_restricted.mat \
             $(FIG_DIR)/stationary_distro_restricted.pdf \
             $(FIG_DIR)/stationary_distro_restrictedR2.pdf \
             $(FIG_DIR)/restricted_fit_exit.pdf \
             $(FIG_DIR)/restricted_fit_growth.pdf

F_OUTPUTS := agg_vars_GEsub_restricted.mat Bellman_RHS_GEsub_restricted.mat \
             x_GEsub_restricted.mat agg_vars_GE_OCsub_restricted.mat \
             Bellman_RHS_GE_OCsub_restricted.mat x_GE_OCsub_restricted.mat


# Data dependencies in the build directory
BUILD_DATA := $(DATA_DIR)/compustat_size_weights.csv $(DATA_DIR)/bds_lifecycle.csv

# All MATLAB files in this directory are potential dependencies for any script
ALL_M_FILES := $(wildcard *.m)

.PHONY: all B C D E F

all: B C D E F

B: $(B_OUTPUTS)
C: $(C_OUTPUTS)
D: $(D_OUTPUTS)
E: $(E_OUTPUTS)
F: $(F_OUTPUTS)

$(B_OUTPUTS) &: B_calibration_and_fit.m $(BUILD_DATA)  $(ALL_M_FILES) ../../settings.json
	@echo "Part 1: Running calibration and model fit to data..."
	$(MATLAB) "B_calibration_and_fit"

$(C_OUTPUTS) &: C_counterfactual.m $(B_OUTPUTS) $(ALL_M_FILES)
	@echo "Part 2: Quantifying the importance of different mechanisms..."
	$(MATLAB) "C_counterfactual"

$(D_OUTPUTS) &: D_growth_policies_baseline.m $(B_OUTPUTS) $(ALL_M_FILES)
	@echo "Part 3a: Implications for growth policies (Baseline)..."
	$(MATLAB) "D_growth_policies_baseline"

$(E_OUTPUTS) &: E_restricted.m $(B_OUTPUTS)  $(ALL_M_FILES)
	@echo "Part 3b: The restricted model calibration and fit to data..."
	$(MATLAB) "E_restricted"

$(F_OUTPUTS) &: F_restricted_policies.m $(E_OUTPUTS) $(ALL_M_FILES)
	@echo "Part 3c: Implications for growth policies in the restricted model..."
	$(MATLAB) "F_restricted_policies"