# working variables

SRCS := $(wildcard *.c)
HDRS := $(wildcard *.h)
DEP_IN = $(SRCS) $(HDRS)
OBJS := $(patsubst %.c,%.o,$(SRCS))

# static libraries

ifeq ($(OUT_TYPE),static-lib)

$(OUT_NAME):	$(OBJS)
		ar -r $@ $(OBJS)

install:	$(OUT_NAME)

endif

# dynamic libraries

ifeq ($(OUT_TYPE),lib)
$(OUT_NAME):	$(OBJS)
		$(CC) -shared $(DEFS) $(CFLAGS) $(INCLUDES) $(LIBS) -o $@ $(OBJS)

install:	$(OUT_NAME) $(LIBDIR)
			$(INSTALL-TOUCH) $(LIBDIR)/$(OUT_NAME)
			$(INSTALL-LIB) $(OUT_NAME) $(LIBDIR)

$(LIBDIR):
		mkdir -p $(LIBDIR)
endif

# common rules 

%.o:	%.c
		$(CC) $(DEFS) $(CFLAGS) $(INCLUDES) -c $<

.PHONY:	clean install proper

proper: clean

clean:
		-@rm -f $(OUT_NAME) *.o *.so *.d core core.* *~ Makefile.deps


ifneq ($(MAKECMDGOALS),clean)
-include $(SRCS:.c=.d)
endif

%.d:	%.c
		@$(CC) -M $(DEFS) $(CFLAGS) $(INCLUDES) $< > $@.$$$$; \
		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
		rm -f $@.$$$$