CC = gcc
CFLAGS = -O2 -Wall -Wextra
LDLIBS = -llz
PREFIX = /usr/local

all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair

# Shared core: the v5 archive format, the per-entry pipeline
# (lzip -> BCH), header I/O, scan, and filters. The
# command-line tool (lzply), the FUSE filesystem (lzply_mount),
# the statistics tool (lzply_stat) and the heal tool (lzply_heal)
# all link it.
liblzply.a: lzply_lib.o bch.o sha256.o
	ar rcs $@ $^

lzply_lib.o: lzply_lib.c lzply_lib.h bch.h bch_polys.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzply_lib.c

sha256.o: sha256.c sha256.h
	$(CC) $(CFLAGS) -c -o $@ sha256.c

bch.o: bch.c bch.h bch_polys.h
	$(CC) $(CFLAGS) -c -o $@ bch.c

lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h
	$(CC) $(CFLAGS) -c -o $@ lzply.c

lzply: lzply.o liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply.o liblzply.a $(LDLIBS)

# FUSE filesystem. Needs libfuse3-dev; no pkg-config on the server, so
# the flags are explicit (-D_FILE_OFFSET_BITS=64 is required by FUSE).
FUSE_CFLAGS = -D_FILE_OFFSET_BITS=64
FUSE_LIBS = -lfuse3

lzply_mount.o: lzply_mount.c lzply_lib.h bch.h bch_polys.h
	$(CC) $(CFLAGS) $(FUSE_CFLAGS) -c -o $@ lzply_mount.c

lzply_mount: lzply_mount.o liblzply.a
	$(CC) $(CFLAGS) $(FUSE_CFLAGS) -o $@ lzply_mount.o liblzply.a $(LDLIBS) $(FUSE_LIBS)

# Shared helpers for lzply_stat and lzply_heal (mount resolution via
# /proc, human-size formatting). No FUSE needed.
lzply_util.o: lzply_util.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_util.c

lzply_stat.o: lzply_stat.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_stat.c

lzply_stat: lzply_stat.o lzply_util.o liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply_stat.o lzply_util.o liblzply.a $(LDLIBS)

lzply_heal.o: lzply_heal.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_heal.c

lzply_heal: lzply_heal.o lzply_util.o liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply_heal.o lzply_util.o liblzply.a $(LDLIBS)

# Bit error rate detector. Read-only: decodes each BCH entry's stored
# bytes in memory and counts the corrected bit errors. Links the same
# shared util (mount resolution) as stat and heal.
lzply_check.o: lzply_check.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_check.c

lzply_check: lzply_check.o lzply_util.o liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply_check.o lzply_util.o liblzply.a $(LDLIBS) -lm

# In-place platter refresher. Reads a file (or every regular file
# under a directory / mount point) and rewrites it over itself, with
# SHA-256 verification. Links the same shared util (mount
# resolution) as stat, heal and check.
lzply_refresh.o: lzply_refresh.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_refresh.c

lzply_refresh: lzply_refresh.o lzply_util.o liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply_refresh.o lzply_util.o liblzply.a $(LDLIBS)

# Striped volume (bit-plane, 8 data stripes + 1-3 XOR/GF parity stripes
# + BCH). No compression, so it links only bch.o + sha256.o — no
# liblz, no lzply_lib.
lzply_hyper.o: lzply_hyper.c bch.h bch_polys.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzply_hyper.c

lzply_hyper: lzply_hyper.o bch.o sha256.o
	$(CC) $(CFLAGS) -o $@ lzply_hyper.o bch.o sha256.o

bch_test: bch.c bch_test.c bch.h bch_polys.h
	$(CC) $(CFLAGS) -o $@ bch.c bch_test.c

# Reed-Solomon codec (rs.c) and the LZRS v1 archive layer
# (lzrs_lib.c: LZRS entry encode/decode + header I/O + scan).
# lz_rs links both plus the shared v4 core (liblzply.a).
rs.o: rs.c rs.h
	$(CC) $(CFLAGS) -c -o $@ rs.c

lzrs_lib.o: lzrs_lib.c lzrs_lib.h lzply_lib.h rs.h bch.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzrs_lib.c

liblzrs.a: lzrs_lib.o rs.o
	ar rcs $@ $^

lz_rs.o: lz_rs.c lzrs_lib.h lzply_lib.h bch.h bch_polys.h
	$(CC) $(CFLAGS) -c -o $@ lz_rs.c

lz_rs: lz_rs.o liblzrs.a liblzply.a
	$(CC) $(CFLAGS) -o $@ lz_rs.o liblzrs.a liblzply.a $(LDLIBS)

rs_test: rs.c rs_test.c rs.h
	$(CC) $(CFLAGS) -o $@ rs.c rs_test.c

# CRC-16/CCITT-FALSE unit test (vectors + cross-checks). Links the
# shared core for crc16_of.
crc16_test: crc16_test.c liblzply.a
	$(CC) $(CFLAGS) -o $@ crc16_test.c liblzply.a $(LDLIBS)

# Deep repair: Chase / list decoding past t (bch.c) + the escalating
# ladder (deep_repair.c) + the lzply_deep_repair CLI. Links both
# archive layers (LZPL v5 + LZRS v1).
deep_repair.o: deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h rs.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ deep_repair.c

lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h
	$(CC) $(CFLAGS) -c -o $@ lzply_deep_repair.c

lzply_deep_repair: lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a
	$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a $(LDLIBS)

# bch_polys.h is generated by gen_bch_polys.py from the rssearch data
# (~/workspace/rssearch/polys.txt + finds.txt) and checked in; it is
# NOT rebuilt by make. Regenerate manually after a re-crawl with:
#   python3 gen_bch_polys.py

# The shell suite lives in ./tests when built from the repo layout
# (~/workspace/lzply) and in ../tests when built from /opt/lzply/src.
# run_tests.sh derives its ROOT from its own location, so it works
# in both layouts; LZPLY_TEST_ROOT overrides it explicitly.
# NB: lzply_mount is a test dep on purpose — without it the mount
# tests in run_tests.sh silently skip after a `make clean`, even on a
# machine with /dev/fuse. (Needs libfuse3-dev; present on the server
# and the dev VM. The mount tests themselves still skip cleanly
# where /dev/fuse is absent.)
# testdata/ is generated (it includes a 50 MiB random file) and gitignored,
# so a fresh clone has none. Build it on demand so `make test` works
# straight from a clone.
testdata:
	@if [ ! -d testdata/mixed ]; then ./tests/make_testdata.sh; fi

test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
	./bch_test
	./rs_test
	./crc16_test
	if [ -x ./tests/run_rs_tests.sh ]; then \
		./tests/run_rs_tests.sh ./lz_rs; \
	elif [ -x ../tests/run_rs_tests.sh ]; then \
		../tests/run_rs_tests.sh ./lz_rs; \
	else echo "run_rs_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_tests.sh ]; then \
		./tests/run_tests.sh ./lzply; \
	elif [ -x ../tests/run_tests.sh ]; then \
		../tests/run_tests.sh ./lzply; \
	else echo "run_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_deep_repair_tests.sh ]; then \
		./tests/run_deep_repair_tests.sh ./lzply_deep_repair ./lzply ./lz_rs; \
	elif [ -x ../tests/run_deep_repair_tests.sh ]; then \
		../tests/run_deep_repair_tests.sh ./lzply_deep_repair ./lzply ./lz_rs; \
	else echo "run_deep_repair_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_hyper_tests.sh ]; then \
		./tests/run_hyper_tests.sh ./lzply_hyper; \
	elif [ -x ../tests/run_hyper_tests.sh ]; then \
		../tests/run_hyper_tests.sh ./lzply_hyper; \
	else echo "run_hyper_tests.sh not found"; exit 1; fi

install: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
	mkdir -p $(PREFIX)/bin
	install -m 755 lzply $(PREFIX)/bin/lzply
	install -m 755 lz_rs $(PREFIX)/bin/lz_rs
	install -m 755 lzply_deep_repair $(PREFIX)/bin/lzply_deep_repair
	install -m 755 lzply_mount $(PREFIX)/bin/lzply_mount
	install -m 755 lzply_stat $(PREFIX)/bin/lzply_stat
	install -m 755 lzply_heal $(PREFIX)/bin/lzply_heal
	install -m 755 lzply_check $(PREFIX)/bin/lzply_check
	install -m 755 lzply_refresh $(PREFIX)/bin/lzply_refresh
	install -m 755 lzply_hyper $(PREFIX)/bin/lzply_hyper
	install -m 755 mount.lzply /sbin/mount.lzply

clean:
	rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair bch_test rs_test crc16_test *.o liblzply.a liblzrs.a

.PHONY: all test install clean
