# usmb - mount SMB shares via FUSE and Samba
# Copyright (C) 2006-2008 Geoff Johnstone
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


SAMBA = /opt/samba
CC = gcc
INSTALL = install

PREFIX = /usr/local
BINDIR = $(PREFIX)/bin

CFLAGS += -Wall -Wextra -Werror -std=c99 -pedantic -O \
          -I$(SAMBA)/include -D_BSD_SOURCE -DFUSE_USE_VERSION=26 \
          -DHAVE_UTIME_H

LDLIBS = -lsmbclient
LDFLAGS = -L$(SAMBA)/lib

PACKAGES = glib-2.0 libxml-2.0 fuse

CFLAGS  += $(shell pkg-config --cflags $(PACKAGES))
LDLIBS  += $(shell pkg-config --libs-only-l $(PACKAGES))
LDFLAGS += $(shell pkg-config --libs-only-L $(PACKAGES))

SOURCES = conffile.c options.c usmb.c usmb_dir.c usmb_file.c utils.c \
          version.c xml.c
OBJECTS = $(SOURCES:.c=.o)

PROGRAM = usmb


all: $(PROGRAM)

conffile.c: config.rng.h
config.rng.h: config.rng
	sed -e 's/"/\\"/g' -e 's/\(.*\)/  "\1" \\/' \
    -e '1istatic const char *rng_$(^:.rng=) =' $^ > config.rng.h
	echo '  "";' >> config.rng.h

debug: CFLAGS += -ggdb -DDEBUG
debug: all


$(PROGRAM): $(OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)


clean:
	$(RM) $(PROGRAM) $(OBJECTS)


distclean: clean
	$(RM) core usmb-*.tar.bz2 usmb-*.tar.gz doc/*.mdzip.bak config.rng.h


install-strip: STRIPFLAGS = -s
install install-strip: $(PROGRAM)
	mkdir -p $(BINDIR)
	$(INSTALL) -m 755 $(STRIPFLAGS) $(PROGRAM) $(BINDIR)/


uninstall:
	$(RM) $(BINDIR)/$(PROGRAM)
	rmdir -p $(BINDIR)
	@echo Please delete ~/.usmb.conf manually.


dist: tar

tar: VERSION=$(shell grep 'USMB_VERSION[^_]' version.h | sed 's/.*0x//')
tar: STAGING=/tmp/usmb-$(VERSION)
tar:
	mkdir $(STAGING)
	hg log -v > $(STAGING)/Changelog
	cp -a $(PWD)/* $(STAGING)/
	rm -rf $(STAGING)/.hg
	(cd $(STAGING)/..; \
	 make -C $(STAGING) distclean ; \
	 tar jcf $(PWD)/usmb-$(VERSION).tar.bz2 usmb-$(VERSION) ; \
	 tar zcf $(PWD)/usmb-$(VERSION).tar.gz usmb-$(VERSION))
	rm -rf $(STAGING)


.PHONY: all debug dist install install-strip uninstall clean distclean tar

conffile.o: conffile.c utils.h xml.h config.rng.h
options.o: options.c options.h utils.h version.h
usmb.o: usmb.c conffile.h options.h usmb.h usmb_dir.h usmb_file.h \
        utils.h version.h
usmb_dir.o: usmb_dir.c usmb_dir.h usmb.h utils.h
usmb_file.o: usmb_file.c usmb_file.h usmb.h utils.h
utils.o: utils.c utils.h
version.o: version.c version.h
xml.o: xml.c xml.h utils.h
