###############################################################################
#    Copyright (C) 2002-2004 by Eric Gerbier
#    Bug reports to: gerbier@users.sourceforge.net
#    $Id: Makefile.fgz 927 2006-08-30 10:17:33Z gerbier $
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program 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.
#
###############################################################################
 
# general makefile to install afick from tgz package

# recommanded by debian install guide
DESTDIR=

# directories
TARGET_BIN = $(DESTDIR)/usr/bin
TARGET_CONF = $(DESTDIR)/etc
TARGET_CRON = $(DESTDIR)/etc/cron.daily
TARGET_MAN = $(DESTDIR)/usr/share/man
TARGET_MAN1 = $(TARGET_MAN)/man1
TARGET_MAN5 = $(TARGET_MAN)/man5
TARGET_BASE =  $(DESTDIR)/var/lib/afick
TARGET_ARCHIVE = $(TARGET_BASE)/archive
TARGET_LOG  = $(DESTDIR)/var/log/afick
TARGET_LOGROT  = $(DESTDIR)/etc/logrotate.d
# gnome
TARGET_ICON1 = $(DESTDIR)/usr/share/icons
# kde
TARGET_ICON2 = $(DESTDIR)/usr/share/pixmaps
# debian mandrake
TARGET_MENU1 = $(DESTDIR)/usr/lib/menu
# redhat kde
TARGET_MENU2 = $(DESTDIR)/usr/share/applnk/System/
# redhat gnome
TARGET_MENU3 = $(DESTDIR)/usr/share/gnome/apps/System

TARGET_DOC=$(DESTDIR)/usr/share/doc/afick

# files
BIN             =  afick.pl afick-common.pl afickonfig.pl afick_archive.pl afick_postinstall.pl afick_preinstall.pl
DOC		= AUTHORS COPYING COPYRIGHT INSTALL Changelog Changelog-gui linux.conf MANIFEST NEWS README QUICKSTART TODO afick.lsm
CONF            = afick.conf
CRON		= afick_cron
MANPAGES1       = afick.1 afickonfig.1 afick_archive.1
MANPAGES5 	= afick.conf.5
LOGROT      = afick.logrotate
MENU1        = menu
MENU2        = afick.kde
MENU3        = afick.gnome
ICON        = afick.xpm afick.png

# return install in general, installbsd on AIX
# this syntaxe seems available on all systems 
INSTALL	= `./test_os.sh`
# this syntaxe is better but seems to be only gnu :(
#INSTALL	= $(shell ./test_os.sh)

# first target : will be the default
help:
	@echo "available target are :"
	@echo "- install : install only command-line tool"
	@echo "- install-gui : install graphical (tk) interface (force command-line install)"
	@echo "- install-doc : install documentation"
	@echo "- all : install all the targets"
	@echo "- uninstall-gui : uninstall gui"
	@echo "- uninstall-doc : uninstall doc"
	@echo "- uninstall : uninstall command-line tool"
	@echo "- uninstall-all : uninstall all"
	@echo "- help : this text"

all : install install-gui install-doc

uninstall-all: uninstall-doc uninstall uninstall-gui

# because of AIX, only use short arg syntaxe
install: $(BIN) $(CONF)
	@echo "install only command-line tool"
	@echo "pre-install"
	./afick_preinstall.pl -c $(TARGET_CONF)/$(CONF)
	@echo "install afick commands on $(TARGET_BIN)"
	mkdir -p                   $(TARGET_BIN)
	$(INSTALL) -m 755 $(BIN)      $(TARGET_BIN)
	cd $(TARGET_BIN) && ln -sf afick.pl afick
	cd $(TARGET_BIN) && ln -sf afickonfig.pl afickonfig
	cd $(TARGET_BIN) && ln -sf afick_archive.pl afick_archive
	@echo "install configuration file on $(TARGET_CONF)"
	mkdir -p                   $(TARGET_CONF)
	$(INSTALL) -m 600 $(CONF)    	 $(TARGET_CONF)
	mkdir -p                   $(TARGET_CRON)
	@echo "install cron job on $(TARGET_CRON)"
	$(INSTALL) -m 700 $(CRON)    	 $(TARGET_CRON)
	mkdir -p                   $(TARGET_MAN1)
	@echo "install man pages"
	$(INSTALL) -m 644 ${MANPAGES1}  $(TARGET_MAN1)
	mkdir -p                   $(TARGET_MAN5)
	$(INSTALL) -m 644 ${MANPAGES5}  $(TARGET_MAN5)
	@echo "prepare base, archive, log directories"
	mkdir -p -m 700        $(TARGET_BASE)
	mkdir -p -m 700        $(TARGET_ARCHIVE)
	mkdir -p -m 700        $(TARGET_LOG)
	mkdir -p -m 700        $(TARGET_LOGROT)
	$(INSTALL) -m 644 ${LOGROT}     $(TARGET_LOGROT)/afick
	@echo "postinstall"
	$(TARGET_BIN)/afick_postinstall.pl -c $(TARGET_CONF)/$(CONF)

uninstall:
	rm -f $(TARGET_BIN)/afick*
	rm -f $(TARGET_CONF)/$(CONF)
	rm -f $(TARGET_CRON)/$(CRON)
	rm -f $(TARGET_MAN1)/afick*
	rm -f ${TARGET_MAN5}/afick*
	rm -rf $(TARGET_BASE)
	rm -rf $(TARGET_ARCHIVE)
	rm -rf $(TARGET_LOG)
	rm -f  $(TARGET_LOGROT)/afick

install-gui: install
	@echo "install graphical (tk) interface"
	$(INSTALL) -m 755 afick-tk.pl  $(TARGET_BIN)
	cd $(TARGET_BIN) && ln -sf afick-tk.pl afick-tk
	$(INSTALL) -m 644 afick-tk.1   $(TARGET_MAN1)
	mkdir -p                  $(TARGET_MENU1)
	$(INSTALL) -m 644 $(MENU1)     $(TARGET_MENU1)/afick
	mkdir -p                  $(TARGET_MENU2)
	$(INSTALL) -m 644 $(MENU2)     $(TARGET_MENU2)/afick.desktop
	mkdir -p                  $(TARGET_MENU3)
	$(INSTALL) -m 644 $(MENU3)     $(TARGET_MENU3)/afick.desktop
	mkdir -p                  $(TARGET_ICON1)
	$(INSTALL) -m 644 $(ICON)      $(TARGET_ICON1)
	mkdir -p                  $(TARGET_ICON2)
	$(INSTALL) -m 644 $(ICON)      $(TARGET_ICON2)

uninstall-gui:
	rm -f $(TARGET_BIN)/afick-tk*
	rm -f $(TARGET_MENU1)/afick*
	rm -f $(TARGET_MENU2)/afick*
	rm -f $(TARGET_MENU3)/afick*
	rm -f $(TARGET_ICON1)/afick*
	rm -f $(TARGET_ICON2)/afick*

install-doc:
	@echo "install documentation on $(TARGET_DOC)"
	mkdir -p			 $(TARGET_DOC)
	$(INSTALL) -m 644 ${DOC}	 $(TARGET_DOC)
	mkdir -p			 $(TARGET_DOC)/html
	$(INSTALL) -m 644 *.html *.gif *.jpg $(TARGET_DOC)/html

uninstall-doc:
	rm -rf $(TARGET_DOC)

clean :
	@echo "no temporary files : no cleaning"

