####################################################################
# CVS Activity Notifying Tool
# Author     : Hojin Choi <pynoos@users.kldp.net>
# $Id: Makefile,v 1.9 2004/03/28 02:46:05 pynoos Exp $
# Copyright (C) 2004 by Hojin Choi <pynoos@users.kldp.net>
# You can redistribute this software under GPL.
####################################################################

PACKAGE=cvsnotify
VERSION=1.0.1
PACKAGE_STRING=$(PACKAGE)-$(VERSION)
UNINSTALL = cvsnotify-uninstall.sh
BARECVSROOT=`echo $$CVSROOT | awk -F: '{print $$NF}'`

FILES = \
	.indent.pro \
	cvsnotify-commitinfo.pl \
	cvsnotify-lang-en.tpl \
	cvsnotify-lang-ko.tpl \
	cvsnotify-loginfo.pl \
	cvsnotify.cf \
	cvsnotify.pm \
	cvsnotify-filenamechk.pl \
	cvsnotify-contentsend.pl \
	cvsnotify-doxygenchk.pl \
	cvsnotify-indent.pl \
	cvsnotify-logsend.pl \
	$(UNINSTALL)

help:
	@echo "make install		install cvsnotify to your cvs repository (local, remote)"
	@echo "make uninstall		uninstall cvsnotify from your cvs repository"
	@echo "make check		check system environment, run in server side!"
	@echo ""
	@echo "Current: CVSROOT=$(CVSROOT)"
	@echo ""

.PHONY: help install clean-checkout save-revisions install-commit copy-files uninstall modify-infoes
install: clean-checkout save-revisions copy-files modify-infoes install-commit
	
clean-checkout:
	@echo "work-$$CVSROOT" | tr -c '0-9a-zA-Z' '-' | sed -e 's/^-//' -e 's/--/-/g' -e 's/-$$//' > LASTCVSROOT
	@echo ""
	@if test -z "$(CVSROOT)"; then echo "Set your environment CVSROOT" && exit 1; fi
	@if test -d `cat LASTCVSROOT`; then \
		echo "Cleaning working copy of `cat LASTCVSROOT`" && rm -rf `cat LASTCVSROOT` ; \
	fi
	@echo "Check out CVSROOT from $(CVSROOT)"
	@cvs -d$(CVSROOT) co -d `cat LASTCVSROOT` CVSROOT
	@rm -f CVSROOT && ln -sf `cat LASTCVSROOT` CVSROOT
	@rm -f LASTCVSROOT

save-revisions:
	@echo ""
	@echo "Make uninstall script.."
	-@( cd CVSROOT; cvs status ) | grep "Repository" | grep $(BARECVSROOT) | grep -v "cvsnotify-" | sed -e "s:$(BARECVSROOT)/CVSROOT/::" -e "s:,v$$::" | awk '{print "cvs up -p -r", $$3, $$4, ">", $$4 }' > $(UNINSTALL)
	@for file in $(FILES); do \
		files="`echo $$files` $$file"; \
	done; echo "cvs remove -f $$files" >> $(UNINSTALL)
	@echo "cvs commit -m Removed" >> $(UNINSTALL)
	@chmod +x $(UNINSTALL)

modify-infoes:
	@echo "New checkout list..."
	@echo "#cvsnotify checkout list" > checkoutlist
	@echo 	".indent.pro			.indent.pro checkout failure" >> checkoutlist
	@echo 	"cvsnotify.pm			cvsnotify.pm checkout failure" >> checkoutlist
	@echo 	"cvsnotify.cf			cvsnotify.cf checkout failure" >> checkoutlist
	@echo 	"cvsnotify-lang-ko.tpl		cvsnotify-lang-ko.tpl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-lang-en.tpl		cvsnotify-lang-en.tpl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-commitinfo.pl	cvsnotify-commitinfo.pl	checkout failure" >> checkoutlist
	@echo 	"cvsnotify-loginfo.pl		cvsnotify-loginfo.pl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-filenamechk.pl		cvsnotify-indent.pl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-indent.pl		cvsnotify-indent.pl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-doxygenchk.pl	cvsnotify-doxygenchk.pl	checkout failure" >> checkoutlist
	@echo 	"cvsnotify-logsend.pl		cvsnotify-logsend.pl checkout failure" >> checkoutlist
	@echo 	"cvsnotify-contentsend.pl	cvsnotify-contentsend.pl checkout failure" >> checkoutlist

	@echo "New commitinfo..."
	@echo "CVSROOT echo Skip cvsnotify-checking.. " > commitinfo
	@echo "DEFAULT \$$CVSROOT/CVSROOT/cvsnotify-commitinfo.pl" >> commitinfo
	@echo "New loginfo..."
	@echo "CVSROOT echo Skip cvsnotify-checking.. $$s" > loginfo
	@echo "DEFAULT \$$CVSROOT/CVSROOT/cvsnotify-loginfo.pl %{sVv}" >> loginfo

copy-files:
	@echo ""
	@echo "Copy cvsnotify files..."
	@if test ! -f $(UNINSTALL); then \
		touch $(UNINSTALL); \
		chmod +x $(UNINSTALL); \
	fi
	@for file in $(FILES) ; do \
		if test -f CVSROOT/$$file; then \
			cp $$file CVSROOT/ ; \
			echo "updating.. $$file" ; \
		else \
			cp $$file CVSROOT/ ; \
			echo "adding....  $$file" ; \
			files="`echo $$files` $$file"; \
		fi ; \
	done; cd CVSROOT; cvs add $$files 2>/dev/null
	@for file in checkoutlist commitinfo loginfo; do \
		if test -f $$file; then \
			echo "updating.. $$file" ; \
			cp -p $$file CVSROOT/; \
		else \
			echo "oops.. $$file" ; \
		fi; \
	done

install-commit:
	@cd CVSROOT; cvs commit -m "Installed"
	@echo "Installed successfully..."
	@echo "You must change cvsnotify.cf in CVSROOT directory in current directory"
	@echo "especially, maildomain = {YOUR_MAIL_DOMAIN}, cvsurl = {YOUR_VIEWCVS_URL} should be replaced."
	@echo "After modifying, you must commit via 'cvs commit' command"
	@echo ""
	@echo "Thank you for using!"

uninstall: clean-checkout
	@if test -x CVSROOT/$(UNINSTALL); then \
		cd CVSROOT; ./$(UNINSTALL) ; \
	else \
		echo "There is no uninstall script: $(UNINSTALL)" ; \
	fi

dist:
	-@rm -rf $(PACKAGE_STRING) $(PACKAGE_STRING).tar.gz
	@mkdir $(PACKAGE_STRING)
	@for file in History README.ko COPYING Makefile $(FILES); do \
		if test "cvsnotify.cf" = "$$file"; then \
			cp -p cvsnotify.cf.sample $(PACKAGE_STRING)/cvsnotify.cf ; \
			continue; \
		fi; \
		if test "cvsnotify-uninstall.sh" = "$$file"; then \
			continue; \
		fi; \
		echo "Copying.. $$file"; \
		cp -p $$file $(PACKAGE_STRING) ; \
	done
	tar cf - $(PACKAGE_STRING) | gzip > $(PACKAGE_STRING).tar.gz

clean:
	-test -L CVSROOT && test -d CVSROOT && cd CVSROOT && rm -rf `pwd -P`
	-rm -rf CVSROOT
