#!/usr/bin/make -s

.SILENT :
CC = g++
target = sharedmime

# build mode, should be 'Release' or 'Debug'
ifndef BUILD
	BUILD = Debug
endif

VPATH = %.cpp \
	$(BUILD)

ccflags = -fPIC -w -fno-inline -fexceptions
defs = -DLINUX -D_REENTRANT

ifeq ($(BUILD),Debug)
	#debug builds..
	flags = -g $(ccflags) $(defs)
	defs += -D_DEBUG 
	tag = d
else
	#release builds
	flags = -s -Os $(ccflags) $(defs)
endif

releasefile = lib$(target)
targetfile = $(releasefile)1$(tag).so

# libraries
libs =

#include paths for .h files
incpath =	

# all the objects required
depends =	mime-types.o \
			mime-types-priv.o \
			glob.o \
			magic.o

# executable target
$(targetfile) : $(depends)
	@echo Linking $(targetfile) [$(BUILD)]
	$(CC) -nodefaultlibs -lc -shared \
		-Wl,-soname,$(targetfile),-export-dynamic \
		-o $(BUILD)/$(targetfile) $(addprefix $(BUILD)/,$(^F)) \
		$(libs) 
	@echo Done.

# different paths to source files
%.o : %.cpp
	@echo $(<F)
	ls $(BUILD) > /dev/null 2> /dev/null || mkdir $(BUILD)
	$(CC) $(incpath) $(flags) -c $< -o $(BUILD)/$(@F)

# clean the directory out
clean :
	rm -f $(BUILD)/*

# install the files
install :
	cp $(BUILD)/$(targetfile) /usr/local/lib
	chmod 755 /usr/local/lib/$(targetfile)
	cp mime-types.h /usr/local/include
	chmod 644 /usr/local/include/mime-types.h
	@echo Installed $(BUILD) Library.

# build disribution archive
release :
	-rm $(releasefile).tar.bz2 > /dev/null 2> /dev/null
	cd .. ; \
	tar -cf libsharedmime/$(releasefile).tar \
		libsharedmime/*.h \
		libsharedmime/*.cpp \
		libsharedmime/makefile \
		libsharedmime/README \
		libsharedmime/LICENSE \
		libsharedmime/mls/*.cpp \
		libsharedmime/mls/makefile
	bzip2 -z $(releasefile).tar
	ls -l $(releasefile).tar.bz2