# either linux or dragonfly
SYSTEM?=linux

DESTDIR?=
PREFIX?=/usr/local
LIBSUFFIX?=
LIBDIR?=$(PREFIX)/lib$(LIBSUFFIX)
MANDIR?=$(PREFIX)/share/man
SBINDIR?=$(PREFIX)/sbin
INCLUDEDIR?=$(PREFIX)/include

# either openssl or gcrypt
PBKDF_BACKEND?=openssl

# system compiler, normally gcc
CC?=gcc

INSTALL?=install

RM?=rm -f

LN?=ln

# whether to enable debugging or not
DEBUG?=no

MAJ_VER=2
MIN_VER=0


# I've given up on -Werror for now; many Linux distros have warnings even in
# their standard header files.
WARNFLAGS= -Wsystem-headers -Wall -W -Wno-unused-parameter \
	-Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith \
	-Wold-style-definition -Wreturn-type -Wwrite-strings \
	-Wswitch -Wshadow -Wcast-align -Wunused-parameter -Wchar-subscripts \
	-Winline -Wnested-externs

VER_FLAGS= -DMAJ_VER=$(MAJ_VER) -DMIN_VER=$(MIN_VER) -D_FILE_OFFSET_BITS=64

SRCS=	tcplay.c crc32.c safe_mem.c io.c hdr.c humanize.c
SRCS+=	crypto.c generic_xts.c
OBJS=	tcplay.o crc32.o safe_mem.o io.o hdr.o humanize.o
OBJS+=	crypto.o generic_xts.o

COMMON_CFLAGS= $(WARNFLAGS) -fPIC $(VER_FLAGS)

ifeq ($(DEBUG), yes)
  COMMON_CFLAGS+= -O0 -g -DDEBUG
else
  COMMON_CFLAGS+= -O3
endif

ifeq (${SYSTEM}, linux)
  COMMON_CFLAGS+=	-D_GNU_SOURCE
  LIBS+=	-lgcrypt -lgpg-error -ldevmapper -luuid
  SRCS+=	crypto-gcrypt.c
  OBJS+=	crypto-gcrypt.o
  ifeq (${PBKDF_BACKEND}, gcrypt)
    SRCS+=	pbkdf2-gcrypt.c
    OBJS+=	pbkdf2-gcrypt.o
  endif
  ifeq (${PBKDF_BACKEND}, openssl)
    SRCS+=	pbkdf2-openssl.c
    OBJS+=	pbkdf2-openssl.o
    LIBS+=	-lcrypto
  endif
endif

ifeq (${SYSTEM}, dragonfly)
  LIBS+=	-lcrypto -ldm -lprop
  SRCS+=	crypto-dev.c
  OBJS+=	crypto-dev.o
  SRCS+=	pbkdf2-openssl.c
  OBJS+=	pbkdf2-openssl.o
endif

OBJS_PROG= $(OBJS) main.o
OBJS_LIB= $(OBJS) tcplay_api.o

all: tcplay libtcplay.so libtcplay.a

%.o: %.c
	$(CC) $(COMMON_CFLAGS) $(CFLAGS)   -c -o $@ $<

tcplay: $(OBJS_PROG)
	$(CC) $(COMMON_CFLAGS) $(CFLAGS)   -o tcplay $(OBJS_PROG) $(LIBS)

libtcplay.so.$(MAJ_VER).$(MIN_VER): $(OBJS_LIB)
	$(CC) -shared -Wl,-version-script=tcplay.map -Wl,-soname=libtcplay.so.$(MAJ_VER).$(MIN_VER)  $(LDFLAGS) \
		-o libtcplay.so.$(MAJ_VER).$(MIN_VER) $(OBJS_LIB) $(LIBS)

libtcplay.so: libtcplay.so.$(MAJ_VER).$(MIN_VER)
	$(LN) -sf libtcplay.so.$(MAJ_VER).$(MIN_VER) libtcplay.so

libtcplay.a: $(OBJS_LIB)
	$(AR) -rs libtcplay.a $(OBJS_LIB)


install: install_program install_lib install_lib_static

install_program: tcplay install_man8
	$(INSTALL) -d "$(DESTDIR)$(SBINDIR)"
	$(INSTALL) tcplay "$(DESTDIR)$(SBINDIR)"

install_lib: libtcplay.so.$(MAJ_VER).$(MIN_VER) libtcplay.so install_h install_man3
	$(INSTALL) -d "$(DESTDIR)$(LIBDIR)"
	$(INSTALL) libtcplay.so.$(MAJ_VER).$(MIN_VER) "$(DESTDIR)$(LIBDIR)"
	$(LN) -sf libtcplay.so.$(MAJ_VER).$(MIN_VER) "$(DESTDIR)$(LIBDIR)/libtcplay.so"

install_lib_static: libtcplay.a install_h install_man3
	$(INSTALL) -d "$(DESTDIR)$(LIBDIR)"
	$(INSTALL) libtcplay.a "$(DESTDIR)$(LIBDIR)"

install_h: tcplay_api.h
	$(INSTALL) -d "$(DESTDIR)$(INCLUDEDIR)"
	$(INSTALL) tcplay_api.h "$(DESTDIR)$(INCLUDEDIR)"

install_man3: tcplay.3
	$(INSTALL) -d "$(DESTDIR)$(MANDIR)/man3"
	$(INSTALL) tcplay.3 "$(DESTDIR)$(MANDIR)/man3"

install_man8: tcplay.8
	$(INSTALL) -d "$(DESTDIR)$(MANDIR)/man8"
	$(INSTALL) tcplay.8 "$(DESTDIR)$(MANDIR)/man8"


clean_cmake_mess:
	$(RM) CMakeCache.txt
	$(RM) -r CMakeFiles
	$(RM) CPackConfig.cmake
	$(RM) CPackSourceConfig.cmake
	$(RM) Makefile
	$(RM) cmake_install.cmake
	$(RM) tcplay.pc

clean: clean_cmake_mess
	$(RM) $(OBJS_PROG)
	$(RM) $(OBJS_LIB)
	$(RM) tcplay libtcplay.so* libtcplay.a tcplay.core ktrace.out

.PHONY: install install_program install_lib install_lib_static install_man3 install_man8 install_h
