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

# Shared core: the v4 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

# 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 lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper
	./bch_test
	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_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
	mkdir -p $(PREFIX)/bin
	install -m 755 lzply $(PREFIX)/bin/lzply
	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 bch_test *.o liblzply.a

.PHONY: all test install clean
