#
# Copyright (c) 2003,2004 ALT Linux Ltd.
# Copyright (c) 2003,2004 Stanislav Ievlev <inger@altlinux.org>
#
# Makefile for alternatives subsystem project
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
NAME = alternatives
VERSION = 0.2.0

CVSROOT=:ext:ievlev@cvs.sourceforge.net:/cvsroot/alternatives

DESTDIR=$(RPM_BUILD_ROOT)
BINDIR=/usr/bin
SBINDIR=/usr/sbin
DATADIR=/usr/share/alternatives
MANDIR = /usr/share/man
MAN1DIR = $(MANDIR)/man1
ALTDIR = /etc/alternatives

HELP2MAN1 = help2man -N -s1
INSTALL = install
LN_S = ln -s
MKDIR_P = mkdir -p
TOUCH_R = touch -r

LIBRARY=list.awk upgrade.awk validate.awk display.awk config.awk functions
PROGRAMS=upgrade list install uninstall manual auto update validate display config
MAN1PAGES = $(PROGRAMS:%=alternatives-%.1)
TARGETS= functions $(MAN1PAGES)

.PHONY: all install install-bin install-lib clean

all: $(TARGETS)

%: %.in
	sed -e 's/@VERSION@/$(VERSION)/g' <$< >$@
	$(TOUCH_R) $< $@
	chmod --reference=$< $@

%.1: % doc/%.1.inc functions
	$(HELP2MAN1) -i doc/$@.inc ./$< >$@


install: all install-bin install-compat install-lib install-man install-cfg

install-bin:
	$(MKDIR_P) -m755 $(DESTDIR)$(BINDIR)
	$(INSTALL) -p -m755 $(PROGRAMS:%=alternatives-%) $(DESTDIR)$(BINDIR)
	$(INSTALL) -p -m755 alternatives-helper $(DESTDIR)/$(BINDIR)

install-compat:
	$(MKDIR_P) -m755 $(DESTDIR)$(BINDIR) $(DESTDIR)$(SBINDIR)
	$(LN_S)	$(BINDIR)/alternatives-helper $(DESTDIR)/$(SBINDIR)/alternatives-helper
	$(LN_S)	$(BINDIR)/alternatives-update $(DESTDIR)/$(SBINDIR)/alternatives-update

install-lib:
	$(MKDIR_P) -m755 $(DESTDIR)$(DATADIR)
	$(INSTALL) -p -m755 $(LIBRARY) $(DESTDIR)$(DATADIR)
	

install-man:
	$(MKDIR_P) -m755 $(DESTDIR)$(MAN1DIR)
	$(INSTALL) -p -m644 $(MAN1PAGES) $(DESTDIR)$(MAN1DIR)/

install-cfg:
	$(INSTALL) -d $(DESTDIR)$(ALTDIR)/{auto,packages.d,links}

clean:
	$(RM) $(TARGETS) *~

distrib:
	@cvs commit -m ' - Auto commit'
	@mkdir -p $(NAME)-$(VERSION)
	@CVSROOT=$(CVSROOT) cvs co $(NAME)
	@mv $(NAME)/* $(NAME)-$(VERSION)
	@./cvs2cl.pl -b --fsf -f $(NAME)-$(VERSION)/ChangeLog
	@rmdir $(NAME)
	@find $(NAME)-$(VERSION) -type d -name 'CVS'|xargs rm -rf
	@tar -cjpf $(NAME)-$(VERSION).tar.bz2 $(NAME)-$(VERSION)
	@rm -rf $(NAME)-$(VERSION)
