# Makefile
# ex.  > make PLATFORM_BITS=64 INST_PREFIX=/usr
#
NAME = websock
LIBWEBSOCKETS_NAME = libwebsockets
DATE_TAG:=`date +%Y%m%d`

INST_PREFIX = /usr/local

CCOPT = -I.   -I$(INST_PREFIX)/include
LDOPT =  -L$(INST_PREFIX)/lib

CFLAGS = -Wall -O2 $(CCOPT) -DLWS_OPENSSL_SUPPORT
AZ_CCOPT = -Wall -O2 $(CCOPT) -DLWS_OPENSSL_SUPPORT
AZ_LIB_EXT = $(INST_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(INST_PREFIX)/lib/azprolog/obj

LIBS=-lwebsockets -lssl
AZPC_LIBS="-lwebsockets -lssl"

EXEC_TARGETS = client websock
TARGETS = $(EXEC_TARGETS) websock.so lib_websock.a
OBJS = client.o util.o
SERVER_OBJS = client.o server_side.o util.o



default: websock.so lib_websock.a

%: %.c Makefile
	$(CC) $(CFLAGS) $< -o $@ $(LDOPT) $(LIBS)

%.o: %.c client.h util.h Makefile
	$(CC) $(CFLAGS) -c $<

websock: websock_userfile.c
	$(CC) $(CFLAGS) -c $<
	gcc -o $@ websock_userfile.o $(AZ_LIB_OBJ)/azpi.o $(AZ_LIB_OBJ)/curses.o -L$(AZ_LIB_EXT) -lazp -l_websock -lwebsockets -lssl -lcurses -lm -ldl

websock.so: websock.c client.c util.c
	$(CC) $(CFLAGS) -fPIC -c websock.c
	$(CC) $(CFLAGS) -fPIC -c client.c
	$(CC) $(CFLAGS) -fPIC -c util.c
	$(CC) -shared -dynamiclib -o $@ websock.o client.o util.o $(LDOPT) $(LIBS)
	rm -f *.o

lib_websock.a: websock.c client.c util.c
	$(CC) $(CFLAGS) -c websock.c
	$(CC) $(CFLAGS) -c client.c
	$(CC) $(CFLAGS) -c util.c
	ar rv $@ websock.o
	ar rv $@ client.o
	ar rv $@ util.o
	ranlib $@
	rm -f *.o

azwebsock: websock.c client.h util.h $(OBJS)
	azpc -p $< /i /e $@ /curses  /ccopt "$(AZ_COPT)" /link_opt2 "$(OBJS) $(LDOPT)" /link_lib $(AZPC_LIBS)
	rm -f *.o

client: client.c util.o client.h Makefile
	$(CC) $(CFLAGS) -DTEST $< util.o -o $@ $(LDOPT) $(LIBS)

clean:
	rm -f *~ *.o mkaz userfile.c $(EXEC_TARGETS) *.so *.a

full-clean:
	rm -f *~ *.o mkaz userfile.c $(TARGETS)

tar:
	cd ..; tar --exclude='cert' -cvf $(NAME)-$(DATE_TAG).tar $(NAME)
	gzip ../$(NAME)-$(DATE_TAG).tar
