SHELL=/bin/bash

GREEN="\\033[1\;32m"
RED="\\033[1\;31m"
WHITE="\\033[01\;37m"
YELLOW="\\033[01\;33m"
CYAN="\\033[01\;36m"
BLUE="\\033[01\;34m"
MAGENTA="\\033[01\;35m"

ifneq "$(TERM)" "xterm"
	SMOOTHBLUE=$(BLUE)
else
	SMOOTHBLUE="\\033[01\;38\;5\;111m"
endif

EARGS=""

all: mo
	@echo -e "$(MAGENTA)compiling: 	$(SMOOTHBLUE).terminfo files:"
	@echo -e "\t$(GREEN) [01/03] mostlike"
	@tic $(PWD)/data/terminfo/mostlike.terminfo -o $(PWD)/data/terminfo/
	@echo -e "\t$(GREEN) [02/03] bold"
	@tic $(PWD)/data/terminfo/bold.terminfo -o $(PWD)/data/terminfo/
	@echo -e "\t$(GREEN) [03/03] nebula"
	@tic $(PWD)/data/terminfo/nebula.terminfo -o $(PWD)/data/terminfo/
	@tput sgr0

pot:
	@echo -e "$(MAGENTA)creating:	$(SMOOTHBLUE).pot file"
	@mkdir -p translations/pot/
	@intltool-extract --type=gettext/glade data/bashstyle7.ui >/dev/null
	@xgettext --language=Python --keyword=_ --keyword=N_ --output=translations/pot/bs-ng.pot \
		$(PWD)/code/bsng.py.in $(PWD)/data/bashstyle7.ui.h >/dev/null
	@tput sgr0

po: pot
	@echo -e "$(MAGENTA)updating: 	$(SMOOTHBLUE).po files:"
	@mkdir -p translations/po/
	@echo -e "\t$(GREEN) [01/04] german (de)"
	@msgmerge -q -o translations/po/bs-ng-de.po translations/po/bs-ng-de.po translations/pot/bs-ng.pot >/dev/null
	@echo -e "\t$(GREEN) [02/04] italian (it)"
	@msgmerge -q -o translations/po/bs-ng-it.po translations/po/bs-ng-it.po translations/pot/bs-ng.pot >/dev/null
	@echo -e "\t$(GREEN) [03/04] russian (ru)"
	@msgmerge -q -o translations/po/bs-ng-ru.po translations/po/bs-ng-ru.po translations/pot/bs-ng.pot >/dev/null
	@echo -e "\t$(GREEN) [04/04] spanish (es)"
	@msgmerge -q -o translations/po/bs-ng-es.po translations/po/bs-ng-es.po translations/pot/bs-ng.pot >/dev/null
	@tput sgr0

mo:
	@echo -e "$(MAGENTA)creating:	$(SMOOTHBLUE).mo files:"
	@mkdir -p translations/mo/
	@echo -e "\t$(GREEN) [01/04] german (de)"
	@msgfmt --output-file=translations/mo/bs-ng-de.mo translations/po/bs-ng-de.po
	@echo -e "\t$(GREEN) [02/04] italian (it)"
	@msgfmt --output-file=translations/mo/bs-ng-it.mo translations/po/bs-ng-it.po
	@echo -e "\t$(GREEN) [03/04] russian (ru)"
	@msgfmt --output-file=translations/mo/bs-ng-ru.mo translations/po/bs-ng-ru.po
	@echo -e "\t$(GREEN) [04/04] spanish (es)"
	@msgfmt --output-file=translations/mo/bs-ng-es.mo translations/po/bs-ng-es.po
	@tput sgr0

distclean: clean

clean:
	@echo -e "$(SMOOTHBLUE)cleaning up"
	@rm -f data/bashstyle
	@rm -f code/bsng.py
	@rm -f rc/nx-rc
	@rm -f Makefile
	@rm -f data/bashstyle-ng.pc
	@rm -f data/bashstyle7.ui.h
	@rm -f translations/pot/bs-ng.pot
	@rm -f translations/mo/bs-ng-de.mo
	@rm -f translations/mo/bs-ng-it.mo
	@rm -f translations/mo/bs-ng-ru.mo
	@rm -f data/terminfo/m/mostlike
	@rm -f data/terminfo/n/nebula
	@rm -f data/terminfo/b/bold
	@rm -f */*~
	@tput sgr0

install:
	@echo -e "$(GREEN)installing:$(YELLOW)	[01/14] directories"
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/data/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/settings/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/styles/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/functions/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/ascii/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/system/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/b/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/m/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/n/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/code/
	@mkdir -p $(DESTDIR)@PREFIX@/share/bashstyle-ng/vim/colors/
	@mkdir -p $(DESTDIR)@PREFIX@/share/applications/
	@mkdir -p $(DESTDIR)@PREFIX@/bin/
	@mkdir -p $(DESTDIR)@PREFIX@/share/locale/de/LC_MESSAGES/
	@mkdir -p $(DESTDIR)@PREFIX@/share/locale/it/LC_MESSAGES/
	@mkdir -p $(DESTDIR)@PREFIX@/share/locale/ru/LC_MESSAGES/
	@mkdir -p $(DESTDIR)@PREFIX@/share/locale/es/LC_MESSAGES/
	@mkdir -p $(DESTDIR)@PREFIX@/share/doc/bashstyle-ng/
	@mkdir -p $(DESTDIR)@PREFIX@/share/gconf/schemas
	@mkdir -p $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/
	@mkdir -p $(DESTDIR)/usr/lib/pkgconfig/
	@echo -e "$(GREEN)installing:$(YELLOW) 	[02/14] bashstyle-ng"
	@install -m644 code/*[!.in] $(DESTDIR)@PREFIX@/share/bashstyle-ng/code/
	@install -m755 data/bashstyle $(DESTDIR)@PREFIX@/bin/
	@echo -e "$(GREEN)installing:$(YELLOW) 	[03/14] terminfo files:$(CYAN) bold mostlike nebula"
	@install -m644 data/terminfo/b/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/b/
	@install -m644 data/terminfo/m/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/m/
	@install -m644 data/terminfo/n/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/terminfo/n/
	@echo -e "$(GREEN)installing:$(YELLOW) 	[04/14] gtkbuilder file"
	@install -m644 data/bashstyle7.ui $(DESTDIR)@PREFIX@/share/bashstyle-ng/data/
	@echo -e "$(GREEN)installing:$(YELLOW)	[05/14] vim color schemes"
	@install -m755 data/vim/colors/*.vim $(DESTDIR)@PREFIX@/share/bashstyle-ng/vim/colors/
	@echo -e "$(GREEN)installing:$(YELLOW)	[06/14] icons into hicolor"
	@install -m644 data/icons/*.png $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/
	@echo -e "$(GREEN)installing:$(YELLOW) 	[07/14] nx-rc rcgenerator bs-ng-profiler bs-ng-wizard"
	@install -m755 rc/nx-rc $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/
	@install -m755 scripts/*[!.in] $(DESTDIR)@PREFIX@/bin/
	@echo -e "$(GREEN)installing:$(YELLOW)	[08/14] .desktop file"
	@install -m644 data/bs-ng.desktop $(DESTDIR)@PREFIX@/share/applications/
	@echo -e "$(GREEN)installing:$(YELLOW)	[09/14] documentation"
	@cp -r documentation/* $(DESTDIR)/@PREFIX@/share/doc/bashstyle-ng/
	@cp ChangeLog TODO README AUTHORS COPYING.CODE COPYING.ICONS $(DESTDIR)/@PREFIX@/share/doc/bashstyle-ng/
	@chmod -R 755 $(DESTDIR)/@PREFIX@/share/doc/bashstyle-ng/
	@echo -e "$(GREEN)installing:$(YELLOW)	[10/14] settings rc"
	@install -m755 rc/settings/*[!.in] $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/settings/
	@echo -e "$(GREEN)installing:$(YELLOW)	[11/14] styles rc"
	@install -m755 rc/styles/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/styles/
	@echo -e "$(GREEN)installing:$(YELLOW)	[12/14] functionchain"
	@install -m755 rc/functions/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/functions/
	@install -m755 rc/ascii/* $(DESTDIR)@PREFIX@/share/bashstyle-ng/rc/ascii/
	@echo -e "$(GREEN)installing:$(YELLOW)	[13/14] .pc gconf-schemas"
	@install -m644 data/bashstyle-ng.pc $(DESTDIR)/usr/lib/pkgconfig/
	@install -m644 data/deprecated.schemas $(DESTDIR)@PREFIX@/share/gconf/schemas/bashstyle-deprecated.schemas
	@install -m644 data/bashstyle.schemas $(DESTDIR)@PREFIX@/share/gconf/schemas/
	@echo -e "$(GREEN)installing:$(YELLOW)	[14/14] translations:$(CYAN) de it ru es"
	@install -m644 translations/mo/bs-ng-de.mo $(DESTDIR)/usr/share/locale/de/LC_MESSAGES/bashstyle-ng.mo
	@install -m644 translations/mo/bs-ng-it.mo $(DESTDIR)/usr/share/locale/it/LC_MESSAGES/bashstyle-ng.mo
	@install -m644 translations/mo/bs-ng-ru.mo $(DESTDIR)/usr/share/locale/ru/LC_MESSAGES/bashstyle-ng.mo
	@install -m644 translations/mo/bs-ng-es.mo $(DESTDIR)/usr/share/locale/es/LC_MESSAGES/bashstyle-ng.mo
	@if test ! $(RC_DISABLE) ; then \
		echo -e "$(MAGENTA)post-install:$(SMOOTHBLUE)	[01/05] re-adjust .bashrc entries" ; \
		for file in $$(ls /home/*/.bashrc); do \
			if [ "x$$(grep nx-rc $$file)" != "x" ] ; then \
				sed -e '/nx-rc/d' -i $$file; \
				echo "source @PREFIX@/share/bashstyle-ng/rc/nx-rc" >> $$file; \
			fi; \
		done; \
	fi
	@if test ! $(GCONF_DISABLE) ; then \
		echo -e "$(MAGENTA)post-install:	$(SMOOTHBLUE)[02/05] deregister deprecated gconf-schemas" ; \
		GCONF_CONFIG_SOURCE=xml:merged:/etc/gconf/gconf.xml.defaults \
			gconftool-2 --makefile-uninstall-rule data/deprecated.schemas >/dev/null ; \
	fi
	@if test ! $(GCONF_DISABLE) ; then \
		echo -e "$(MAGENTA)post-install:	$(SMOOTHBLUE)[03/05] register gconf-schemas" ; \
		GCONF_CONFIG_SOURCE=xml:merged:/etc/gconf/gconf.xml.defaults \
			gconftool-2 --makefile-install-rule data/bashstyle.schemas >/dev/null ; \
	fi
	@if test ! $(CACHE_DISABLE) ; then \
		echo -e "$(MAGENTA)post-install:	$(SMOOTHBLUE)[04/05] update icon cache" ; \
		gtk-update-icon-cache -f /usr/share/icons/hicolor 2>/dev/null ; \
	fi
	@if test ! $(KILL_DISABLE) ; then \
		echo -e "$(MAGENTA)post-install:	$(SMOOTHBLUE)[05/05] restart gconf-daemon" ; \
		killall -qw gconfd-2 >/dev/null || exit 0; \
	fi
	@tput sgr0

uninstall:
	@echo -e "$(RED)removing:$(CYAN)	[01/08] bashstyle-ng"
	@rm -rf @PREFIX@/share/bashstyle-ng
	@rm -f  @PREFIX@/bin/bashstyle
	@echo -e "$(RED)removing:$(CYAN)	[02/08] rcgenerator bs-ng-profiler bs-ng-wizard"
	@rm -f  @PREFIX@/usr/bin/rcgenerator
	@rm -f  @PREFIX@/usr/bin/bs-ng-profiler
	@rm -f  @PREFIX@/usr/bin/bs-ng-wizard
	@echo -e "$(RED)removing:$(CYAN)	[03/08] .desktop file"
	@rm -f  @PREFIX@/share/applications/bs-ng.desktop
	@echo -e "$(RED)removing:$(CYAN)	[04/08] translations:$(BLUE) de it ru es"
	@rm -f  /usr/share/locale/de/LC_MESSAGES/bashstyle-ng.mo
	@rm -f  /usr/share/locale/it/LC_MESSAGES/bashstyle-ng.mo
	@rm -f  /usr/share/locale/ru/LC_MESSAGES/bashstyle-ng.mo
	@rm -f  /usr/share/locale/es/LC_MESSAGES/bashstyle-ng.mo
	@echo -e "$(RED)removing:$(CYAN)	[05/08] gconf schemas"
	@rm -f  @PREFIX@/share/gconf/schemas/bashstyle-deprecated.schemas
	@rm -f  @PREFIX@/share/gconf/schemas/bashstyle.schemas
	@echo -e "$(RED)removing:$(CYAN)	[06/08] icons from hicolor"
	@rm -f  /usr/share/icons/hicolor/32x32/apps/bs-ng-*.png
	@echo -e "$(RED)removing:$(CYAN)	[07/08] documentation"
	@rm -rf @PREFIX@/share/doc/bashstyle-ng/
	@echo -e "$(RED)removing:$(CYAN)	[08/08] .pc file"
	@rm -f  /usr/lib/pkgconfig/bashstyle-ng.pc
	@echo -e "$(MAGENTA)post-remove:$(YELLOW)	[01/04] re-adjust .bashrc entries"
	@sed -e '/nx-rc/d' -i /home/*/.bashrc
	@if test ! $(GCONF_DISABLE) ; then \
		echo -e "$(MAGENTA)post-remove:	$(YELLOW)[02/04] deregister gconf-schemas" ; \
		GCONF_CONFIG_SOURCE=xml:merged:/etc/gconf/gconf.xml.defaults \
		gconftool-2 --makefile-uninstall-rule data/bashstyle.schemas >/dev/null ; \
	fi
	@if test ! $(CACHE_DISABLE) ; then \
		echo -e "$(MAGENTA)post-remove:	$(YELLOW)[03/04] update icon cache" ; \
		gtk-update-icon-cache -f /usr/share/icons/hicolor 2>/dev/null ; \
	fi
	@if test ! $(KILL_DISABLE) ; then \
		echo -e "$(MAGENTA)post-remove:	$(YELLOW)[04/04] restart gconf-daemon" ; \
		killall -qw gconfd-2 >/dev/null || exit 0 ; \
	fi
	@tput sgr0
