# Benin's country Makefile (GH #1705).
#
# Benin had no _/Makefile until it became a panel country.  It has one now for
# one reason: the panel crosswalk is regenerated by a deliberate maintainer
# target, `make panel-ids`, as in every other panel country (CLAUDE.md, "The
# crosswalk is a committed SOURCE").  A Makefile is not inert, though: once it
# exists, Wave.grab_data builds every script-path wave table by
# `make -s ../<wave>/_/<table>.parquet` instead of running the script itself, so
# every wave script needs a rule below.  Each recipe is the direct run the
# framework used to fall back to, `(cd $(@D) && python <table>.py)`, and the
# target names are the in-tree names the framework asks for; the scripts write
# under data_root() through to_parquet, so a target never exists in the tree and
# make always runs the recipe it is asked for.  Failure behaviour is unchanged:
# a failing wave script raised CalledProcessError before this file existed too
# (from the direct run); now the error names the make target instead of the
# script, and the "Makefile not found" warning is gone.  Measured cold, serial,
# all waves (GH #1705 red team).

rounds := $(shell ls -d ../[12]*)

COUNTRY := $(shell basename "$$(cd .. && pwd)")
LSMS_DATA_ROOT ?= $(if $(LSMS_DATA_DIR),$(LSMS_DATA_DIR),$(if $(XDG_DATA_HOME),$(XDG_DATA_HOME),$(HOME)/.local/share)/lsms_library)
VAR_DIR ?= $(LSMS_DATA_ROOT)/$(COUNTRY)/var

# The wave-level script tables (materialize: make in data_scheme.yml).
tables := community_prices crop_production livestock people_last7days \
          plot_features plot_inputs plot_labor
wave_scripts := $(foreach t,$(tables),$(wildcard $(addsuffix /_/$(t).py,$(rounds))))

all: $(wave_scripts:.py=.parquet)

# panel_ids.json / updated_ids.json are REVIEWED SOURCES, committed to the
# config tree -- NOT build products (GH #914, #894).  They deliberately carry
# no rule, and `clean` does not touch them.  A rule here fires whenever
# panel_ids.py happens to be the newer file, and on a pip install which of the
# two is newer is decided by the millisecond the wheel was unpacked; the
# recipe then writes into site-packages, which on a shared install is not
# writable by the user running the code.  Regenerating the crosswalk is a
# deliberate maintainer action, reviewed as a diff to the committed JSON:
#
#     make panel-ids
#
.PHONY: all panel-ids clean
panel-ids:
	python panel_ids.py

../%/_/community_prices.parquet: ../%/_/community_prices.py
	(cd $(@D) && python community_prices.py)

../%/_/crop_production.parquet: ../%/_/crop_production.py
	(cd $(@D) && python crop_production.py)

../%/_/livestock.parquet: ../%/_/livestock.py
	(cd $(@D) && python livestock.py)

../%/_/people_last7days.parquet: ../%/_/people_last7days.py
	(cd $(@D) && python people_last7days.py)

../%/_/plot_features.parquet: ../%/_/plot_features.py
	(cd $(@D) && python plot_features.py)

../%/_/plot_inputs.parquet: ../%/_/plot_inputs.py
	(cd $(@D) && python plot_inputs.py)

../%/_/plot_labor.parquet: ../%/_/plot_labor.py
	(cd $(@D) && python plot_labor.py)

# The country-level _/plot_features.py concatenator has no rule: the framework
# runs it directly (try_script), as it did before this file existed.

clean:
	-rm -f $(VAR_DIR)/*.parquet
