###############################################################################
# $Id: Makefile,v 1.12 2010/12/31 02:38:03 tw Exp $
#
# Build and install GDS Desktop support
#
# Copyright (C) 2010 Thomas Weidenfeller
# 
# This file is part of gds2000tools.
# 
# gds2000tools 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.
# 
# gds2000tools 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 gds2000tools.  If not, see <http://www.gnu.org/licenses/>.
###############################################################################

TOP	= ..
include $(TOP)/config.mk


#
# User's nautilus script directory
#
NSCRIPTS_DIR_USER	= $$HOME/.gnome2/nautilus-scripts

#
# nautilus-script-manager directory. Requires nautilus-script-manager
# for distribution to users
#
NSCRIPTS_DIR_SYSTEM	= /usr/share/nautilus-scripts

#
# Gnome (and nautilus) icon dirs
# TODO: Are these configurabel? And why doesn't xdp-icon-resource
# 	copy the icons to this directory?
#
ISIZE	= 48#
GNOME_ICON_DIR_SYSTEM	= /usr/share/icons/gnome/$(ISIZE)x$(ISIZE)/apps
GNOME_ICON_DIR_USER	= $$HOME/.icons/gnome/$(ISIZE)x$(ISIZE)/apps

#
# freedesktop installers
#
XDG_DESKTOP_MENU	= xdg-desktop-menu install --novendor
XDG_ICON_RESOURCE	= xdg-icon-resource install --novendor --size $(ISIZE)
XDG_MIME		= xdg-mime install --novendor
XDG_DESKTOP_MENU	= env XDG_UTILS_DEBUG_LEVEL=99 xdg-desktop-menu install --novendor
XDG_ICON_RESOURCE	= env XDG_UTILS_DEBUG_LEVEL=99 xdg-icon-resource install --novendor --size $(ISIZE)
XDG_MIME		= env XDG_UTILS_DEBUG_LEVEL=99 xdg-mime install --novendor

TARGET		= gds-csv-info
DESKTOP_ENTRIES	= gdsh.desktop gds-analyse.desktop
DESKTOP_ICONS	= gds.png gds-analyse.png
DESKTOP_MIME	= gds2000tools.xml
SRCS		= $(TARGET:=.sh) $(DESKTOP_ENTRIES:=.in)
PRGS		= $(TARGET)
MANS1		= $(TARGET).1
MANS7		= gds-desktop.7
MANS		= $(MANS1) $(MANS7)


###############################################################################
# Targets
###############################################################################

default: all
all: $(PRGS) $(DESKTOP_ENTRIES) $(DESKTOP_MIME) $(MANS)
.PHONY: default all
clean::
	-rm -f $(PRGS) 2>/dev/null
	-rm -f $(DESKTOP_ENTRIES) 2>/dev/null
.PHONY: clean


%: %.sh
	-rm -f $@ 2>/dev/null
	sed 's:@version@:$(VERSION):g; s:@libdir@:$(LIBDIR):g; s:@bindir@:$(BINDIR):g; s:@docdir@:$(DOCDIR):g;' $< >$@ || { rm -f $@; exit 1; }
	chmod a+x $@

%: %.in
	-rm -f $@ 2>/dev/null
	sed 's:@version@:$(VERSION):g; s:@libdir@:$(LIBDIR):g; s:@bindir@:$(BINDIR):g; s:@docdir@:$(DOCDIR):g;' $< >$@ || { rm -f $@; exit 1; }

###############################################################################
# Installation
###############################################################################

install: install-bin install-man install-icon install-menu install-mime
.PHONY: install
install-bin: $(PRGS)
	if [ `id -u` -eq 0 ] ;						\
	then	 							\
		UG="-o root -g root";					\
		DESTDIR="$(NSCRIPTS_DIR_SYSTEM)";			\
	else 			 					\
		UG="" ;							\
		DESTDIR="$(NSCRIPTS_DIR_USER)";				\
	fi;								\
	install $(INSTALL_FLAGS) -m 755 $$UG -d "$$DESTDIR";		\
	install $(INSTALL_FLAGS) -m 755 $$UG $(PRGS) "$$DESTDIR"
	if [ `id -u` -eq 0 ] ;						\
	then	 							\
		UG="-o bin -g bin";					\
		install $(INSTALL_FLAGS) -m 755 $$UG -d $(BINDIR);	\
		( cd $(BINDIR); rm -f $(PRGS) 2>/dev/null; ln -s	\
				"$(NSCRIPTS_DIR_SYSTEM)/$(PRGS)" . ; );	\
	fi
.PHONY: install-bin

install-man: $(MANS)
	if [ `id -u` -eq 0 ] ;						\
	then	 							\
		UG="-o bin -g bin";					\
	else 								\
		UG="" ;							\
	fi;								\
	install $(INSTALL_FLAGS) -m 755 $$UG -d $(MAN1DIR); 		\
	install $(INSTALL_FLAGS) -m 644 $$UG -t $(MAN1DIR) $(MANS1);	\
	install $(INSTALL_FLAGS) -m 755 $$UG -d $(MAN7DIR); 		\
	install $(INSTALL_FLAGS) -m 644 $$UG -t $(MAN7DIR) $(MANS7)

.PHONY: install-man

#
# Install the desktop icons.
# Use the local xdg-icon-resource program if one exists. Contrary to the
# freedesktop.org recommendation gds2000tools doesn't come with
# an own version of xdg-icon-resource. This is to avoid having to
# think about how to merge xdg-icon-resource's license with
# gds2000tools' GPL v3 only license.
# 
install-icon: $(DESKTOP_ICONS)
	@echo "*** Installing desktop icons $(DESKTOP_ICONS)..."
	#
	# For whatever reason xdg-icon-resource does not install the icons
	# in a way nautilus finds them. We do that separately first.
	#
	if [ `id -u` -eq 0 ] ;						\
	then	 							\
		UG="-o root -g root";					\
		DESTDIR="$(GNOME_ICON_DIR_SYSTEM)";			\
	else 			 					\
		UG="" ;							\
		DESTDIR="$(GNOME_ICON_DIR_USER)";			\
	fi;								\
	install $(INSTALL_FLAGS) -m 755 $$UG -d "$$DESTDIR";		\
	install $(INSTALL_FLAGS) -m 644 $$UG $(DESKTOP_ICONS) "$$DESTDIR"
	#
	# Now the xdg-icon-resource stuff
	#
	@f=0;								\
	for i in $(DESKTOP_ICONS) ; do					\
		echo "   *** Trying to install $$i ...";		\
		if $(XDG_ICON_RESOURCE) $$i ;				\
		then							\
			echo "   *** Desktop icon $$i installed." >&2;	\
			f=1;						\
		else							\
			echo "   *** Desktop icon $$i not installed." >&2;\
		fi;							\
	done;								\
	[ $$f -ne 0 ] && echo "*** Desktop icons installed."

.PHONY: install-icon

#
# Install the desktop entries.
# Use the local xdg-desktop-menu program if one exists. Contrary to the
# freedesktop.org recommendation gds2000tools doesn't come with
# an own version of xdg-desktop-menu. This is to avoid having to
# think about how to merge xdg-desktop-menu's license with
# gds2000tools' GPL v3 only license.
#
# NOTE: Even with extra-xdg-menus installed it is a gamble if
# 	and where (Electronics, Education, Other, ...) the menu
# 	items appear on Ubuntu.
# 
install-menu: $(DESKTOP_ENTRIES)
	@echo "*** Installing desktop menu entries ..."
	-if [ `id -u` -eq 0 ] ;					\
	then	 						\
		SCOPE="--system";				\
	else 			 				\
		SCOPE="--local";				\
	fi;							\
	exmenen $$SCOPE electronics
	@if $(XDG_DESKTOP_MENU) $(DESKTOP_ENTRIES) ;		\
	then							\
		echo "*** Desktop menu entries installed.";	\
	else							\
		echo "*** Desktop menu entries not installed." >&2; \
	fi

.PHONY: install-menu

#
# Install the desktop entries.
# Use the local xdg-mime program if one exists. Contrary to the
# freedesktop.org recommendation gds2000tools doesn't come with
# an own version of xdg-mime. This is to avoid having to
# think about how to merge xdg-mime's license with
# gds2000tools' GPL v3 only license.
# 
install-mime: $(DESKTOP_MIME)
	@echo "*** Installing desktop mime types ..."
	@if $(XDG_MIME) $(DESKTOP_MIME) ;		\
	then							\
		echo "*** Desktop mime types installed.";	\
	else							\
		echo "*** Desktop mime types not installed." >&2; \
	fi

.PHONY: install-menu

