# By Muayyad Alsadi<alsadi@gmail.com>
# This program is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License ...

#### Start of system configuration section. ####
SHELL = /bin/sh
srcdir = ./
CC = gcc
INSTALL = install -c
INSTALLDATA = install -c -m 644
### on non GNU add `libintl'
LIBS = -lz -lbz2 `pkg-config --libs gtk+-2.0`
CDEBUG = -g
CFLAGS = -Wall -g -lm `pkg-config --cflags gtk+-2.0`
LDFLAGS = -g
PREFIX = /usr/local
bindir = $(PREFIX)/bin
#### End of system configuration section. ####
SRC = libitar/libitar.c libthwab-lib.c thwab-lib.c
OBJ = libitar/libitar.o libthwab-lib.o thwab-lib.o
# ar -q  libmart.a reverse.o for static library

# while for dynamic use gcc -fpic -g -c -Wall mylist.cpp
# then gcc -shared -Wl,-soname,libmylist.so.1 -o libmylist.so.1.0. 1mylist.o -lc
all:    itar thwab-lib
	@echo "Done"
itar:
	cd src/libitar && $(MAKE)

thwab-lib:
	cd src && 	$(MAKE)

install: install-data
	/usr/sbin/groupadd -f thwab
	/usr/bin/gpasswd -r thwab
	/usr/bin/gpasswd -R thwab
	
	install -cvp -g root -o root -m 0755 -d $(PREFIX)/bin $(PREFIX)/share/  $(PREFIX)/share/thwab-lib $(PREFIX)/share/doc/thwab-lib $(PREFIX)/share/locale
	install -cvp -g root -o root -m 0755 bin/thwab-lib  $(PREFIX)/bin/
	install -cvp -g root -o root -m 0755 bin/itar  $(PREFIX)/bin/
	install -cvp -g root -o root -m 0755 scripts/th*[^~]  $(PREFIX)/bin/
	find share/ -type d -exec install -cvp -g root -o root -m 0755 -d $(PREFIX)/'{}' \;

	install -cvp -g thwab -o root -m 0755 -d $(PREFIX)/share/thwab-lib/resources
	chown -R root:thwab $(PREFIX)/share/thwab-lib/resources

	find share/ -type f \
 -not -name '*~' \
 -not -wholename '*/experimental/*' \
 -not -wholename '*/resources/*' \
 -exec install -cvp -g root -o root -m 0644 '{}' $(PREFIX)/'{}' \;

# install docs
	cd doc && find ./ -type d -exec install -cvp -g root -o root -m 0755 -d $(PREFIX)/share/doc/thwab-lib/'{}' \;

	cd doc && find ./ -type f \
 -not -name '*~' \
 -exec install -cvp -g root -o root -m 0644 '{}' $(PREFIX)/share/doc/thwab-lib/'{}' \;
	cd doc && find ./ -type l \
 -not -name '*~' -exec cp -af '{}' $(PREFIX)/share/doc/thwab-lib/'{}' \;

	install -cvp -g root -o root -m 0644 TODO* README* INSTALL COPYING* $(PREFIX)/share/doc/thwab-lib/
	install -cvp -g root -o root -m 0644 src/libitar/README $(PREFIX)/share/doc/thwab-lib/README.iTar.txt

	@echo "Type 'gpasswd -M user1,user2...  thwab' to allow those users to install or remove system wide Thwab files"
install-data:
	/usr/sbin/groupadd -f thwab
	install -cvp -g thwab -o root -m 0775 -d $(PREFIX)/share/thwab-lib/resources/
	install -cvp -g thwab -o root -m 0664 share/thwab-lib/resources/*  $(PREFIX)/share/thwab-lib/resources

uninstall:
	@echo "The group 'thwab' is kept, type 'groupdel thwab' to remove it"
	/bin/rm -f $(PREFIX)/bin/thwab-lib
	/bin/rm -f $(PREFIX)/bin/itar
	for i in scripts/th*[^~]; do /bin/rm -f $(PREFIX)/bin/$${i/scripts\//}; done
	@echo "only Arabic MO files are removed"
	/bin/rm -f $(PREFIX)/share/locale/ar/LC_MESSAGES/thwab.mo
	@echo "Data files are not uninstalled, type 'make uninstall-data' to remove all Thwab-data"
	
uninstall-data:
	/bin/rm  -R $(PREFIX)/share/thwab-lib

.PHONY : clean
clean :
	-rm bin/thwab-lib bin/itar
	cd src/libitar && $(MAKE) clean
	cd src && $(MAKE) clean
