#-- uncomment this to enable debugging
#DEBUG:=-g -DDEBUG

#-- what compiler are you using?
CC:=gcc


###### YOU SHOULD NOT CHANGE BELOW THIS LINE ######

VERSION:=1.4.0
SRCS:=api.c

CFLAGS:=-Wall -Wstrict-prototypes -Wno-variadic-macros -pedantic -c -fPIC ${DEBUG}
CLINKS:=-lpthread -lrt ${DEBUG}
DEFINES:=-D__UMAKEFILE

SRCS:=${sort ${SRCS}}

.PHONY: all run new clean main


# all - do everything (default) #
all: ./lib/libxbee.so.$(VERSION) main
	@echo "*** Done! ***"


# run - remake main and then run #
run: main
	./bin/main


# new - clean and do everything again #
new: clean all


# clean - remove any compiled files and PDFs #
clean:
	rm -f ./*~
	rm -f ./sample/*~
	rm -f ./obj/*.o
	rm -f ./lib/libxbee.so*
	rm -f ./bin/main
  
# install - installs library #
install: /usr/lib/libxbee.so.$(VERSION) /usr/include/xbee.h

/usr/lib/libxbee.so.$(VERSION): ./lib/libxbee.so.$(VERSION)
	cp ./lib/libxbee.so.$(VERSION) /usr/lib/libxbee.so.$(VERSION) -f
	@chmod 755 /usr/lib/libxbee.so.$(VERSION)
	@chown root:root /usr/lib/libxbee.so.$(VERSION)
	ln ./libxbee.so.$(VERSION) /usr/lib/libxbee.so.1 -sf
	@chown root:root /usr/lib/libxbee.so.1
	ln ./libxbee.so.$(VERSION) /usr/lib/libxbee.so -sf
	@chown root:root /usr/lib/libxbee.so

/usr/include/xbee.h: ./xbee.h
	cp ./xbee.h /usr/include/xbee.h -f
	@chmod 644 /usr/include/xbee.h
	@chown root:root /usr/include/xbee.h

uninstall:
	rm /usr/lib/libxbee.so.$(VERSION) -f
	rm /usr/lib/libxbee.so.1 -f
	rm /usr/lib/libxbee.so -f	
	rm /usr/include/xbee.h -f
  
# main - compile & link objects #
main: ./bin/main

./bin/main: ./obj/api.o ./bin/ ./main.c
	${CC} ${CLINKS} ./main.c ./obj/api.o -o ./bin/main ${DEBUG}

./bin/:
	mkdir ./bin/

./lib/libxbee.so.$(VERSION): ./lib/ ${addprefix ./obj/,${SRCS:.c=.o}} ./xbee.h
	gcc -shared -Wl,-soname,libxbee.so.1 -o ./lib/libxbee.so.$(VERSION) ./obj/*.o -lrt
	ln ./libxbee.so.$(VERSION) ./lib/libxbee.so.1 -sf
	ln ./libxbee.so.$(VERSION) ./lib/libxbee.so -sf

./lib/:
	mkdir ./lib/

./obj/:
	mkdir ./obj/

./obj/%.o: ./obj/ %.c %.h xbee.h
	${CC} ${CFLAGS} ${DEFINES} ${DEBUG} $*.c -o $@

./obj/%.o: ./obj/ %.c xbee.h
	${CC} ${CFLAGS} ${DEFINES} ${DEBUG} $*.c -o $@
