## Copyright (C) 1993,1994 by the author(s).
# 
# This software is published in the hope that it will be useful, but
# WITHOUT ANY WARRANTY for any part of this software to work correctly
# or as described in the manuals. See the ShapeTools Public License
# for details.
#
# Permission is granted to use, copy, modify, or distribute any part of
# this software but only under the conditions described in the ShapeTools 
# Public License. A copy of this license is supposed to have been given
# to you along with ShapeTools in a file named LICENSE. Among other
# things, this copyright notice and the Public License must be
# preserved on all copies.

#
# shape_CM environment -- standard configuration targets
#
# Authors: Andreas Lampen (Andreas.Lampen@cs.tu-berlin.de)
#          Axel Mahler (Axel.Mahler@cs.tu-berlin.de)
#
# $Header: stdconf[6.0] Thu Sep  9 15:53:17 1993 andy@cs.tu-berlin.de frozen $
#
# $__xpoff$

check_releasable:-
	*, attrnot (state, busy), attrmax (version), 
	attr (locker, $(USERATDOMAIN)),
	msg ($_af_unixname$ locked by us);
	*, attrnot (state, busy), attrmax (version), attrnot (locker, ), 
	msg ($_af_unixname$ locked by somebody else -- STOP), -;
	*, attrnot (state, busy), attrmax(version),
	msg ($_af_unixname$ is available);
	*, attr (state, busy),
	msg ($_af_unixname$ is a new busy version -- lock assumed).

most_recently_released:-
	*, attr (__SymbolicName__, $(LASTNODEPRERELEASE)),
	confirm ($_af_unixname$: $(LASTNODEPRERELEASE) more recent than $(LASTNODERELEASE)! Ignore ?, no), -;
	*, attr (__SymbolicName__, $(LASTNODERELEASE)).

most_recently_prereleased:-
	*, attr (__SymbolicName__, $(LASTNODEPRERELEASE));
	*, attr (__SymbolicName__, $(LASTNODERELEASE)).

prerelease:+
	RELEASENAME=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) prerelease \
	$(NODENAME) $(VERSIONFILE) $(PRERELSEQ) $(PATCHLEVEL)`
	NODERELEASERULE=check_releasable
	SUBNODERELEASERULE=most_recently_prereleased
	NODERELEASE=_elementary_prerelease
	SUBNODERELEASE=_include_prerelease
	SETSTATE=_set_accessed
	UNLOCKEDCHANGES=_check_unlocked_changes

fullrelease:+
	RELEASENAME=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) release \
	$(NODENAME) $(VERSIONFILE) $(PRERELSEQ) $(PATCHLEVEL)`
	NODERELEASERULE=most_recently_prereleased
	SUBNODERELEASERULE=most_recently_released
	NODERELEASE=_elementary_release
	SUBNODERELEASE=_include_release
	SETSTATE=_set_frozen_or_published
	UNLOCKEDCHANGES=

USERATDOMAIN:=`$(SHAPELIBPATH)/user_at`
NOTOPRELEASE=$(NODEPATH)
LASTNODERELEASE:=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) release $(NODENAME) \
	$(BASE)/$(NODEPATH)/$(VERSIONFILE) $(BASE)/$(NODEPATH)/$(PRERELSEQ) \
	$(BASE)/$(NODEPATH)/$(PATCHLEVEL)`
LASTNODEPRERELEASE:=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) prerelease $(NODENAME) \
	$(BASE)/$(NODEPATH)/$(VERSIONFILE) $(BASE)/$(NODEPATH)/$(PRERELSEQ) \
	$(BASE)/$(NODEPATH)/$(PATCHLEVEL)`
LASTRELEASE:=`vl -format '$$__lastrelease$$' -lastsaved $(BASE)/$(NODEPATH)/$(VERSIONFILE)`
PRERELSEQ=AtFS/.prerelseq
PLPRERELSEQ=AtFS/.plprerelseq
PATCHLEVEL=AtFS/.patchlevel
RETRVOPTS=
#"vetags extension" TAGS=TAGS
TAGS=

INDENT=
INDENTINCR="  "

#
# This file defines the targets ``release'' and ``prerelease'' to
# generate a new release of an AtFS application.
# Target ``save'' is useful to make an intermediate save of all components.
#
# You should not use targets beginning with an underscore (`_').

release: +fullrelease _start_release _checkrelease _newrel _xrelease \
	_resetseq _resetpl _end_release

_patchlevel: +fullrelease _start_release _checkrelease _newpl _xrelease \
	_resetseq _end_release

prerelease: +prerelease _start_prerelease _checkseq _checkrelease \
	_newprel _xrelease _end_prerelease

plrelease:
	@shape _patchlevel PRERELSEQ=$(PLPRERELSEQ) PATCH=-p

plprerelease:
	@shape prerelease PRERELSEQ=$(PLPRERELSEQ) PATCH=-p

extractrelease: _extract_subreleases partial_extractrelease \
	partial_extractrelman

_start_release:
	@echo; echo Starting release process for $(NODENAME); echo;
	@echo Checking for release preconditions...; echo;

_end_release:
	@echo Construction of release $(RELEASENAME) finished \!; echo

_start_prerelease:
	@echo; echo Starting pre-release process for $(NODENAME); echo
	@echo Checking for pre-release preconditions...; echo;

_end_prerelease:
	@echo Construction of pre-release $(RELEASENAME) finished \!; echo

_end_prerelease:

_extract_subreleases:
	@-_subsystems="$(SUBSYSTEMS)"; \
	if [ -n "$(RELEASENAME)" ]; \
	then \
	  symname=$(RELEASENAME); \
	else \
	  symname=$(LASTNODERELEASE); \
	fi; \
	for i in $$_subsystems; \
	do \
	  (cd $$i; $(MAKE) RELEASENAME=$$symname \
	  NOTOPRELEASE=$(NOTOPRELEASE) \
	  PARTIALRELEASEBASE=$(PARTIALRELEASEBASE) \
	  TOTALRELEASEBASE=$(TOTALRELEASEBASE) \
	  RETRVOPTS="$(RETRVOPTS)" \
	  extractrelease); \
	done

partial_extractrelease:
	@-if [ -n "$(RELEASENAME)" ]; \
	then \
	  symname=$(RELEASENAME); \
	else \
	  symname=$(LASTNODERELEASE); \
	fi; \
	if [ -n "$(NOTOPRELEASE)" ]; \
	then \
	  relarea=$(PARTIALRELEASEBASE); \
	else \
	  relarea=$(TOTALRELEASEBASE); \
	fi; \
	if [ -n "$$relarea" ]; \
	then \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASESRCPATH); \
	  echo $(INDENT)Copying release $$symname of $(SOURCES) $(HEADERS) \
	Shapefile Makefile and Dependencies in $$relarea/$(RELEASESRCPATH); \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASESRCPATH) -bind $$symname \
		$(SOURCES) $(HEADERS) $(VERSIONFILE) Shapefile Makefile \
		Dependencies; \
	  if [ -n "$(SUBSOURCES)$(SUBHEADERS)" ]; then \
	    echo $(INDENT)Copying release $$symname of $(SUBSOURCES) \
	$(SUBHEADERS) in $$relarea/$(RELEASESRCPATH); \
	    _subsources="$(SUBSOURCES) $(SUBHEADERS)"; \
	    for i in $$_subsources; \
	    do \
	      subsrcpath=`echo $$i | sed 's,[^/]*$$,,' | sed 's,/$$,,'`; \
	      if [ -n "$$subsrcpath" ]; \
	      then \
		$(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASESRCPATH)/$$subsrcpath; \
		retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASESRCPATH)/$$subsrcpath \
			-bind $$symname $$i; \
	      else \
		retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASESRCPATH) \
			-bind $$symname $$i; \
	      fi; \
	    done; \
	  fi; \
	else \
	  if [ -n "$(NOTOPRELEASE)" ]; \
	  then \
	    echo $(INDENT)"Copying to partial-release area skipped ! (no path specified)"; \
	  else \
	    echo $(INDENT)"Copying to release area skipped ! (no path specified)"; \
	  fi; \
	fi

partial_extractrelman:
	@-if [ -n "$(RELEASENAME)" ]; \
	then \
	  symname=$(RELEASENAME); \
	else \
	  symname=$(LASTNODERELEASE); \
	fi; \
	if [ -n "$(NOTOPRELEASE)" ]; \
	then \
	  relarea=$(PARTIALRELEASEBASE); \
	else \
	  relarea=$(TOTALRELEASEBASE); \
	fi; \
	if [ -n "$$relarea" ]; \
	then \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH); \
	  if [ -n "$(MAN1)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN1)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man1; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man1 -bind $$symname $(MAN1); \
	  fi; if [ -n "$(MAN3)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN3)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man3; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man3 -bind $$symname $(MAN3); \
	  fi; if [ -n "$(MAN4)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN4)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man4; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man4 -bind $$symname $(MAN4); \
	  fi; if [ -n "$(MAN5)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN5)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man5; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man5 -bind $$symname $(MAN5); \
	  fi; if [ -n "$(MAN6)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN6)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man6; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man6 -bind $$symname $(MAN6); \
	  fi; if [ -n "$(MAN7)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN7)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man7; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man7 -bind $$symname $(MAN7); \
	  fi; if [ -n "$(MAN8)" ]; then \
	  echo $(INDENT)"Copying manual page(s) $(MAN8)"; \
	  $(SHAPELIBPATH)/mkpdir -m 775 $$relarea/$(RELEASEMANPATH)/man8; \
	  retrv -fq $(RETRVOPTS) -dest $$relarea/$(RELEASEMANPATH)/man8 -bind $$symname $(MAN8); \
	  fi; echo; \
	else \
	  echo; \
	fi

_checkrelease: _checksub _checkmain 

#
# This predicate checks whether all necessary preconditions for a 
# release of all subsystems are met, i.e. the subrelease must be
# identifyable.
#
_checksub:
	@echo $(INDENT)$(NODENAME)...; \
	if [ -n "$(SUBSYSTEMS)" ] ; \
	then \
	  if [ -n "$(NODEPATH)" ] ; \
	  then \
	    echo $(INDENT)"Checking subsystem(s) $(NODEPATH)/{$(SUBSYSTEMS)} for releasability"; \
	  else \
	    echo $(INDENT)"Checking subsystem(s) {$(SUBSYSTEMS)} for releasability"; \
	  fi; \
	fi; \
	_subsystems="$(SUBSYSTEMS)"; \
	exitcode=0; \
	for i in $$_subsystems; \
	do \
	  (cd $$i; $(SHAPE) NODERELEASERULE=$(SUBNODERELEASERULE) \
	      SUBNODERELEASERULE=$(SUBNODERELEASERULE) \
	      INDENT='$(INDENT)$(INDENTINCR)' _checksub _checkmain); \
	  if [ $$? -ne 0 ]; then exitcode=1; fi; \
	done; \
	if [ -n "$(SUBSYSTEMS)" ] ; \
	then \
	  echo $(INDENT)$(NODENAME)...; \
	fi; \
	exit $$exitcode
	

#
# This predicate checks whether all preconditions for a local
# release (i.e. of TARGET, COMPONENTS etc.) are met. In particular
# we must be able to lock all components, before we check them
# in. This is intended to prevent interference with programmers
# who might have locked particular COMPONENTS. In the case of a global
# release it is checked whether there are at all subreleases in all
# of the subsystems.
#
# For prereleases, this predicate checks, whether there is a current
# regular release that shall be included into the pre-release.
#
_checkmain: _check_foreign_locks $(UNLOCKEDCHANGES)
	@echo $(INDENT)"   ...is releasable"

_check_foreign_locks: $(NODERELEASERULE) $(COMPONENTS) $(VERSIONFILE)

_check_unlocked_changes:
	@echo -n $(INDENT)Checking for unsaved changes without lock...; \
	nll=`vfind $(COMPONENTS) -last \! -locked -print | \
	egrep '.*\[.*\..*\]' | sed -e 's/\[.*\]//'`; \
	for i in $$nll; \
	do \
	  if [ ! -f $$i ] || vdiff $$i > /dev/null ; \
	  then : ; \
	  else \
	    ulcl="$$ulcl $$i"; \
	  fi; \
	done; \
	if [ -n "$$ulcl" ] ; \
	then \
	  echo; \
	  echo $(INDENT)The following files have been changed but are not locked: ; \
	  echo $(INDENT)"	$$ulcl"; \
	  echo $(INDENT)...$(NODENAME) is currently not releasable.; \
	  exit 1; \
	fi; \
	echo OK!; \
	exit 0;

#
# This action creates a new versionid for our local release which 
# is also used to construct a symbolic name for it.
#
_newrel:
	@-if [ ! -f $(VERSIONFILE) ] ; \
	then \
	  retrv -lock -q $(VERSIONFILE); \
	else \
	  vadm -fq -lock $(VERSIONFILE); \
	fi; \
	vadm -q -attr nodename=$(NODENAME) $(VERSIONFILE); \
	if vl -attr norelease -lastsaved $(VERSIONFILE) > /dev/null 2>&1; \
	then \
	  vadm -q -lastsaved -delattr norelease $(VERSIONFILE); \
	else \
	  save -fq -lock $(VERSIONFILE); \
	fi; \
	vadm -q -lastsaved -attr lastrelease=$(RELEASENAME) \
		-alias $(RELEASENAME) -unlock $(VERSIONFILE); \
	echo; echo New release name is $(RELEASENAME); echo
	
_newprel: _incseq
	@-if vl -lastsaved $(VERSIONFILE) > /dev/null 2>&1; \
	then :; \
	else \
	  save -fq -lock $(VERSIONFILE); \
	  vadm -q -lastsaved -attr norelease=true -unlock $(VERSIONFILE); \
	fi; \
	vadm -q -lastsaved -attr nodename=$(NODENAME) \
		-attr lastrelease=$(RELEASENAME) \
		-alias $(RELEASENAME) $(VERSIONFILE); \
	echo; echo New pre-release name is $(RELEASENAME); echo

_newpl: _incpl
	@-if vl -lastsaved $(VERSIONFILE) > /dev/null 2>&1; \
	then :; \
	else \
	  echo *** Error ***; \
	  echo --- You need to release $(NODENAME) before creating \
		patchlevels ---; \
	  exit 1; \
	fi; \
	vadm -q -lastsaved -attr nodename=$(NODENAME) \
		-attr lastrelease=$(RELEASENAME) \
		-alias $(RELEASENAME) $(VERSIONFILE); \
	echo; echo New patchlevel-release name is $(RELEASENAME); echo

_xrelease: _subreleases $(NODERELEASE) $(SETSTATE) _newline \
	partial_extractrelease partial_extractrelman

_newline:
	@echo

#
# This action visits all subsystems and triggers global releases for
# them. Subsystems of subsystems are globally released recursively.
# The maintarget is globally released after the subsystems.
#
_subreleases:
	@-_subsystems="$(SUBSYSTEMS)"; \
	for i in $$_subsystems; \
	do \
	  (symName=$(RELEASENAME); cd $$i; \
		$(SHAPE) _xrelease \
		INDENT='$(INDENT)$(INDENTINCR)' \
		NODERELEASE=$(SUBNODERELEASE) \
		SUBNODERELEASE=$(SUBNODERELEASE) \
		UNLOCKEDCHANGES= \
		NODERELEASERULE=$(SUBNODERELEASERULE) \
		SUBNODERELEASERULE=$(SUBNODERELEASERULE) \
		RELEASENAME=$$symName \
		NOTOPRELEASE=$(NOTOPRELEASE) \
		SETSTATE=$(SETSTATE) \
		PARTIALRELEASEBASE=$(PARTIALRELEASEBASE) \
		TOTALRELEASEBASE=$(TOTALRELEASEBASE)); \
	done

#
# This action takes care that all yet unsubmitted work results are
# deposited in the version object base and assigned the status "proposed".
# The new, locally generated releasename is attached to all COMPONENTS.
#

_elementary_prerelease:
	@-symName=$(RELEASENAME); \
	echo Pre-releasing $$symName...; \
	save -q $(COMPONENTS); \
	vadm -q -lastsaved -alias $$symName -attr lastrelease=$$symName $(COMPONENTS); \
	retrv -xpoff -fq $(VERSIONFILE); \
	touch -c -f $(VERSIONFILE); \
	sbmt -q -bind $$symName $(COMPONENTS) $(VERSIONFILE) $(TAGS)

# vetags extension
#_elementary_prerelease:
#	@-symName=$(RELEASENAME); \
#	echo Pre-releasing $$symName...; \
#	save -q $(COMPONENTS); \
#	vadm -q -lastrelease -alias $$symName -attr lastrelease=$$symName $(COMPONENTS); \
#	rm -f $(TAGS) 2>&1 > /dev/null; \
#	vadm -q -lock $(TAGS); \
#	vetags -bind $$symName $(SOURCES); \
#	save -alias $$symName -q $(TAGS); \
#	retrv -xpoff -fq $(VERSIONFILE); \
#	touch -c -f $(VERSIONFILE); \
#	sbmt -q -bind $$symName $(COMPONENTS) $(VERSIONFILE) $(TAGS)

_elementary_release:
	@-last_elementary_prerelease=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) lastprerelease \
		$(NODENAME) $(VERSIONFILE) $(PRERELSEQ) $(PATCHLEVEL)`; \
	if vl -bind $$last_elementary_prerelease $(VERSIONFILE) > /dev/null 2>&1; \
	then \
	  echo Releasing $(RELEASENAME) \(consists of $$last_elementary_prerelease\); \
	  vadm -q -bind $$last_elementary_prerelease -alias $(RELEASENAME) \
		-attr lastrelease=$(RELEASENAME) $(COMPONENTS) $(TAGS); \
	else \
	  last_elementary_release=`$(SHAPELIBPATH)/gen_rel_id $(PATCH) lastrelease \
		$(NODENAME) $(VERSIONFILE) $(PRERELSEQ) $(PATCHLEVEL)`; \
	  echo Releasing $(RELEASENAME) \(consists of $$last_elementary_release \); \
	  vadm -q -bind $$last_elementary_release -alias $(RELEASENAME) \
		-attr lastrelease=$(RELEASENAME) $(COMPONENTS) $(TAGS); \
	fi; \
	vadm -fq -bind $(RELEASENAME) -lock $(COMPONENTS) $(TAGS); \
	vadm -q -bind $(RELEASENAME) -newgen -unlock $(COMPONENTS) $(TAGS)

#
# This action is triggered by an incoming global release process, i.e.
# when we are in the role of a subtarget. It retrieves the most recent
# local release of TARGET (see selection rule) and marks its COMPONENTS
# with the global releasename which is passed down to us in the macro
# RELEASENAME. If this is an intermediate global release, i.e. the release
# was initiated by a non-toplevel subsystem of the overall system, no 
# status-change of the involved COMPONENTS takes place. If this is a "real"
# TOPLEVEL release of the entire system, all COMPONENTS are set to status 
# "frozen". 
#
_include_release:
	@-last_elementary_release=$(LASTNODERELEASE); \
	echo $(INDENT)Including $$last_elementary_release into $(RELEASENAME); \
	vadm -q -bind $$last_elementary_release -alias $(RELEASENAME) \
		$(COMPONENTS) $(VERSIONFILE)

_include_prerelease:
	@-last_elementary_prerelease=$(LASTNODEPRERELEASE); \
	if vl -bind $$last_elementary_prerelease $(VERSIONFILE) > /dev/null 2>&1; \
	then \
	  echo $(INDENT)Including $$last_elementary_prerelease into $(RELEASENAME); \
	  vadm -q -bind $$last_elementary_prerelease -alias $(RELEASENAME) $(COMPONENTS) $(VERSIONFILE); \
	else \
	  last_elementary_release=$(LASTNODERELEASE); \
	  echo $(INDENT)Including $$last_elementary_release into $(RELEASENAME); \
	  vadm -q -bind $$last_elementary_release -alias $(RELEASENAME) $(COMPONENTS) $(VERSIONFILE); \
	fi

_set_accessed:
	@-last_elementary_release=$(RELEASENAME); \
	if [ -n "$(NOTOPRELEASE)" ] ; \
	then :; \
	else \
	  echo $(INDENT)Accessing $(NODENAME) components; \
	  accs -q -bind $$last_elementary_release $(COMPONENTS) \
		$(VERSIONFILE); \
	fi

_set_frozen_or_published:
	@-last_elementary_release=$(RELEASENAME); \
	if [ -n "$(NOTOPRELEASE)" ] ; \
	then \
	  echo $(INDENT)Publishing $(NODENAME) components; \
	  publ -q -bind $$last_elementary_release $(COMPONENTS) $(VERSIONFILE); \
	else \
	  echo $(INDENT)Freezing $(NODENAME) components; \
	  frze -q -bind $$last_elementary_release $(COMPONENTS) \
		$(VERSIONFILE); \
	fi

#
# make sure that prerelase sequence number is present and initialized
#

_checkseq: $(PRERELSEQ)

_resetseq: $(PRERELSEQ)
	@rm -f $(PRERELSEQ) 2> /dev/null; \
	echo 0 > $(PRERELSEQ); \
	chmod 664 $(PRERELSEQ)

_incseq: $(PRERELSEQ)
	@awk < $(PRERELSEQ) '{ print $$1+1; }' > AtFS/.prereltmp; \
	mv AtFS/.prereltmp $(PRERELSEQ);\
	chmod 664 $(PRERELSEQ)

_resetpl:
	@rm -f $(PATCHLEVEL) 2> /dev/null; \
	echo 0 > $(PATCHLEVEL); \
	chmod 664 $(PATCHLEVEL)

_incpl:
	@awk < $(PATCHLEVEL) '{ print $$1+1; }' > AtFS/.patchtmp; \
	mv AtFS/.patchtmp $(PATCHLEVEL);\
	chmod 664 $(PATCHLEVEL)

$(PRERELSEQ):
	@-if [ ! -f $(PRERELSEQ) ]; \
	then \
		echo 0 > $(PRERELSEQ); \
		chmod 664 $(PRERELSEQ); \
	fi


