PREFIX = __META_PREFIX__
BIN    = $(PREFIX)/bin
ETC    = $(PREFIX)/etc
SBIN   = $(PREFIX)/sbin
MAN    = $(PREFIX)/man

default: vnstat

vnstat:
	make -C src

all:
	make -C src all

clean:
	make -C src clean

install:
	@echo "Installing vnStat..."

# check that there's something to install
	@if [ ! -f "src/vnstat" ] || [ ! -f "src/vnstatd" ]; \
	then echo "Nothing to install, run make first."; \
	false; \
	fi

# install default config if such doesn't exist
	@if [ ! -f "$(ETC)/vnstat.conf" ]; \
	then echo "Installing config to $(ETC)/vnstat.conf"; \
	install -D -m 644 cfg/vnstat.conf $(ETC)/vnstat.conf; \
	fi

# install everything else
	install -d -m 755 $(BIN) $(SBIN) $(MAN)/man1 $(MAN)/man5 $(DESTDIR)/var/lib/vnstat
	install -s -m 755 src/vnstat $(BIN)
	install -s -m 755 src/vnstatd $(SBIN)
	@if [ -f "src/vnstati" ]; \
	then echo install -s -m 755 src/vnstati $(BIN); \
	install -s -m 755 src/vnstati $(BIN); \
	fi

# update man pages, gzip it if previous version was done so	
	install -m 644 man/vnstat.1 $(MAN)/man1
	install -m 644 man/vnstatd.1 $(MAN)/man1
	install -m 644 man/vnstat.conf.5 $(MAN)/man5
	@if [ -f "src/vnstati" ]; \
	then echo install -m 644 man/vnstati.1 $(MAN)/man1; \
	install -m 644 man/vnstati.1 $(MAN)/man1; \
	fi
	
	@if [ -f $(MAN)/man1/vnstat.1.gz ]; \
	then gzip -f9 $(MAN)/man1/vnstat.1; \
	gzip -f9 $(MAN)/man1/vnstatd.1; \
	gzip -f9 $(MAN)/man5/vnstat.conf.5; \
	if [ -f "src/vnstati" ]; \
	then gzip -f9 $(MAN)/man1/vnstati.1; \
	fi; \
	fi

# remove vnstat.conf.1 is such exists in the wrong place
	@if [ -f $(MAN)/man1/vnstat.conf.1.gz ]; \
	then rm -f $(MAN)/man1/vnstat.conf.1.gz; \
	fi
	@if [ -f $(MAN)/man1/vnstat.conf.1 ]; \
	then rm -f $(MAN)/man1/vnstat.conf.1; \
	fi
