aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: b745a24f05a02f80cf501d5713d0f3e293ef5b9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
SHELL := /bin/bash
.ONESHELL:
.SHELLFLAGS := -euo pipefail -c

ROOT_DIR := $(CURDIR)
TOOLS_TXT := tools.txt
AGENTS_TXT := agents.txt
BIN_DIR := bin
TOOLS_DIR := tools
SCRIPTS_DIR := scripts

.PHONY: all build fix-links fix-bin-links fix-tools-links check clean rebuild

all: build fix-links

build:
	@if ! command -v argc >/dev/null 2>&1; then \
	  echo "ERROR: argc not found. Install it first (e.g. cargo install --locked argc)"; \
	  exit 1; \
	fi
	@echo "[1/2] argc build"
	argc build

fix-links: fix-bin-links fix-tools-links
	@echo "[OK] links fixed"

fix-bin-links:
	@echo "[2/2] fixing bin/ symlinks to relative targets"
	@mkdir -p "$(BIN_DIR)"
	@chmod +x "$(SCRIPTS_DIR)/run-tool.sh" "$(SCRIPTS_DIR)/run-agent.sh" 2>/dev/null || true

	@if [[ -f "$(TOOLS_TXT)" ]]; then \
	  while IFS= read -r line || [[ -n "$$line" ]]; do \
	    line="$${line%%#*}"; \
	    line="$$(printf "%s" "$$line" | sed -E 's/^[[:space:]]+|[[:space:]]+$$//g')"; \
	    [[ -z "$$line" ]] && continue; \
	    name="$${line%.sh}"; \
	    ln -sfn "../$(SCRIPTS_DIR)/run-tool.sh" "$(BIN_DIR)/$$name"; \
	  done < "$(TOOLS_TXT)"; \
	else \
	  echo "WARN: $(TOOLS_TXT) not found; skipping tool links"; \
	fi

	@if [[ -f "$(AGENTS_TXT)" ]]; then \
	  while IFS= read -r line || [[ -n "$$line" ]]; do \
	    line="$${line%%#*}"; \
	    line="$$(printf "%s" "$$line" | sed -E 's/^[[:space:]]+|[[:space:]]+$$//g')"; \
	    [[ -z "$$line" ]] && continue; \
	    name="$${line%.sh}"; \
	    ln -sfn "../$(SCRIPTS_DIR)/run-agent.sh" "$(BIN_DIR)/$$name"; \
	  done < "$(AGENTS_TXT)"; \
	else \
	  echo "WARN: $(AGENTS_TXT) not found; skipping agent links"; \
	fi

	@abs="$$(find "$(BIN_DIR)" -maxdepth 1 -type l -print0 2>/dev/null | \
	  xargs -0 -I{} bash -lc 't=$$(readlink "{}"); [[ "$$t" == /* ]] && echo "{} -> $$t" || true' || true)"; \
	if [[ -n "$$abs" ]]; then \
	  echo "WARN: absolute symlinks still present:"; \
	  echo "$$abs"; \
	fi

fix-tools-links:
	@if [[ -d "$(TOOLS_DIR)" ]]; then \
	  while IFS= read -r -d '' lnk; do \
	    tgt="$$(readlink "$$lnk" || true)"; \
	    [[ -z "$$tgt" ]] && continue; \
	    if [[ "$$tgt" == /* ]]; then \
	      base="$$(basename "$$tgt")"; \
	      if [[ -f "$(TOOLS_DIR)/$$base" ]]; then \
	        ln -sfn "$$base" "$$lnk"; \
	      fi; \
	    fi; \
	  done < <(find "$(TOOLS_DIR)" -maxdepth 1 -type l -print0 2>/dev/null); \
	fi

check:
	@echo "functions.json:"
	@ls -l functions.json
	@echo
	@echo "bin/:"
	@ls -la "$(BIN_DIR)" || true
	@echo
	@echo "Any absolute symlinks left?"
	@find "$(BIN_DIR)" -maxdepth 1 -type l -print0 2>/dev/null | \
	  xargs -0 -I{} bash -lc 't=$$(readlink "{}"); [[ "$$t" == /* ]] && echo "{} -> $$t" || true' || true

clean:
	@rm -rf "$(BIN_DIR)" functions.json cache 2>/dev/null || true
	@echo "cleaned"

rebuild: clean all