ifndef Sqlzma
$(error Sqlzma is not defined)
endif

INSTALL_DIR = /usr/local/bin

INCLUDEDIR = .

CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -O2
ifdef UseDebugFlags
DebugFlags = -g -Wall -Wno-unused-variable -O0 -UNDEBUG
endif
CFLAGS += -I${Sqlzma} -D_REENTRANT -DNDEBUG ${DebugFlags}
LDFLAGS += -L${LzmaAlone} -L${LzmaC}

all: mksquashfs unsquashfs

mksquashfs: mksquashfs.o read_fs.o sort.o
	$(CC) $(LDFLAGS) mksquashfs.o read_fs.o sort.o -lz -lpthread -lm -lunlzma_r -llzma_r -lstdc++ -o $@

mksquashfs.o: mksquashfs.c squashfs_fs.h mksquashfs.h global.h sort.h \
	${Sqlzma}/sqlzma.h ${Sqlzma}/sqmagic.h \
	${LzmaAlone}/liblzma_r.a ${LzmaC}/libunlzma_r.a

read_fs.o: read_fs.c squashfs_fs.h read_fs.h global.h \
	${Sqlzma}/sqlzma.h ${Sqlzma}/sqmagic.h

sort.o: sort.c squashfs_fs.h global.h sort.h

unsquashfs: unsquashfs.o
	$(CC) $(LDFLAGS) unsquashfs.o -lpthread -lm -lunlzma -lz -o $@

unsquashfs.o: unsquashfs.c squashfs_fs.h read_fs.h global.h \
	${Sqlzma}/sqlzma.h ${Sqlzma}/sqmagic.h ${LzmaC}/libunlzma.a

clean:
	-rm -f *.o mksquashfs unsquashfs

install: mksquashfs unsquashfs
	mkdir -p $(INSTALL_DIR)
	cp mksquashfs unsquashfs $(INSTALL_DIR)
