# Makefile to build sar and iostat commands
# (C) 1999 Sebastien GODARD (sebastien.godard@wanadoo.fr)

# Version
VERSION = 2.0

# Compiler to use
CC = gcc
# 'ar' command
AR = ar

# Compiler flags
CFLAGS = -Wall -Wstrict-prototypes -s -pipe -O2 -fno-strength-reduce
DFLAGS =
LFLAGS = -L. -lsysstat
IOS_DFLAGS =

# Directories
PREFIX = /usr/local
BIN_DIR = $(PREFIX)/bin
MAN_DIR = $(PREFIX)/man/man8
SA_DIR  = /var/log/sa

# NLS (National Language Support)
# Package name
PACKAGE = sysstat
### Uncomment this if you want NLS ###
#REQUIRE_NLS = -DUSE_NLS -DPACKAGE=\"$(PACKAGE)\" -DLOCALEDIR=\"$(PREFIX)/share/locale\"
ifdef REQUIRE_NLS
   DFLAGS += $(REQUIRE_NLS)
endif
# The msgfmt command
MSGFMT = /usr/bin/msgfmt

# At the present time, linux doesn't support iowait nor tty accounting...
# IOS_DFLAGS += -DCPU_WAIT
# IOS_DFLAGS += -DTTY
# Uncomment this if you have applied my disk accounting patch
#IOS_DFLAGS += -DHAS_DISK_ACCT

# DFLAGS += -DDEBUG

# Man page group
MAN_GROUP = root

all: sar iostat locales


common.o: common.c
	$(CC) -c -o $@ $(CFLAGS) $(DFLAGS) $<

libsysstat.a: common.o
	$(AR) r $@ $<
	$(AR) s $@
	
sar: sar.c sar.h libsysstat.a
	$(CC) -o $@ $(CFLAGS) $(DFLAGS) $< $(LFLAGS)

iostat: iostat.c iostat.h libsysstat.a
	$(CC) -o $@ $(CFLAGS) $(DFLAGS) $(IOS_DFLAGS) $< $(LFLAGS)

locales: fr/$(PACKAGE).po de/$(PACKAGE).po
ifdef REQUIRE_NLS
	$(MSGFMT) -o fr/$(PACKAGE).mo fr/$(PACKAGE).po
	$(MSGFMT) -o de/$(PACKAGE).mo de/$(PACKAGE).po
endif

# Phony targets
.PHONY: clean install uninstall dist

uninstall:
	rm -f $(BIN_DIR)/sar
	rm -f $(MAN_DIR)/sar.8
	rm -f $(BIN_DIR)/iostat
	rm -f $(MAN_DIR)/iostat.8
	rm -f $(PREFIX)/share/locale/fr/LC_MESSAGES/$(PACKAGE).mo
	rm -f $(PREFIX)/share/locale/de/LC_MESSAGES/$(PACKAGE).mo

install: all sar.8 iostat.8
	install -m 755 -o root -g root sar $(BIN_DIR)
	install -m 644 -o root -g $(MAN_GROUP) sar.8 $(MAN_DIR)
	if [ ! -d $(SA_DIR) ]; then mkdir $(SA_DIR) ; fi
	install -m 755 -o root -g root iostat $(BIN_DIR)
	install -m 644 -o root -g $(MAN_GROUP) iostat.8 $(MAN_DIR)
ifdef REQUIRE_NLS	
	if [ ! -d $(PREFIX)/share ]; then mkdir $(PREFIX)/share ; fi
	if [ ! -d $(PREFIX)/share/locale ]; then mkdir $(PREFIX)/share/locale ; fi
	if [ ! -d $(PREFIX)/share/locale/fr ]; then mkdir $(PREFIX)/share/locale/fr ; fi
	if [ ! -d $(PREFIX)/share/locale/fr/LC_MESSAGES ]; then \
	    mkdir $(PREFIX)/share/locale/fr/LC_MESSAGES; \
	fi
	install -m 644 -o root -g root fr/$(PACKAGE).mo $(PREFIX)/share/locale/fr/LC_MESSAGES
	if [ ! -d $(PREFIX)/share/locale/de ]; then mkdir $(PREFIX)/share/locale/de ; fi
	if [ ! -d $(PREFIX)/share/locale/de/LC_MESSAGES ]; then \
	    mkdir $(PREFIX)/share/locale/de/LC_MESSAGES; \
	fi
	install -m 644 -o root -g root de/$(PACKAGE).mo $(PREFIX)/share/locale/de/LC_MESSAGES
endif

clean:
	rm -f sar iostat fr/*.mo de/*.mo *.o *.a core TAGS data

dist:
	cd .. && (tar -cvf - sysstat-$(VERSION) | gzip -v9 > sysstat-$(VERSION).tar.gz)

tags:
	etags ./*.[hc]

