#!/bin/sh
#	This file is part of the Concurrent Versions System CVS.
#	Written by Dick Grune, Vrije Universiteit, Amsterdam.
#	$Header: hist2,v 3.2 89/10/13 11:54:54 dick Exp $

#
#	Historian: ad-hoc configuration history reconstruction program, part 2
#
#		hist2 [ repository ]
#	reads the output of hist1 from standard input and writes on standard
#	output a shell script that will reconstruct the configuration history
#	file of the repository. If no repository is given, the repository of
#	the present directory is used.
#
Name=hist2; export Name

# CVSBIN, CVSLIB and RCSBIN directories
CVSBIN=/home/top/dick/cvs
CVSLIB=/home/top/dick/cvs
RCSBIN=${RCSBIN-/usr/local/bin}
export CVSBIN CVSLIB RCSBIN

# avoid spurious identifications
PATH=${CVSPATH-/bin:/usr/bin}; export PATH

# determine the name of the repository
Repository=$1
. $CVSLIB/NR.aux

# write the header
echo '#!/bin/sh'
echo ''
echo '#'
echo '#	This file was created by the CVS historian to reconstruct'
echo '#	the configuration history file of repository'
echo '#	'$Repository
echo '#'
echo 'Name=confrec.reconstructor; export Name'
echo ''
echo 'set -e'
echo ''
echo '# CVSBIN, CVSLIB and RCSBIN directories'
echo 'CVSBIN='$CVSBIN
echo 'CVSLIB='$CVSLIB
echo 'RCSBIN='$RCSBIN
echo 'export CVSBIN CVSLIB RCSBIN'
echo ''
echo '# avoid spurious identifications'
echo 'PATH=${CVSPATH-/bin:/usr/bin}; export PATH'
echo ''
echo '# determine the name of the repository'
echo 'Repository='$Repository
echo ''
echo 'Admin=$Repository/Admin'
echo 'Cfr=CVS.confrec'
echo 'RcsCfr=$Admin/$Cfr,v'
echo 'UsrCfr=$Admin/$Cfr'
echo ''
echo 'if	# there is a configuration history file'
echo '	[ -r $RcsCfr ]'
echo 'then	# we are not going to overwrite it'
echo '	echo $Name: there is a configuration history file already, $RcsCfr >&2'
echo '	exit 1'
echo 'fi'
echo ''
echo '# we need the Admin directory'
echo 'if	# $Admin exists'
echo '	[ -d $Admin ]'
echo 'then	:'
echo 'else	# $Admin must be made'
echo '	if	mkdir $Admin >/dev/null 2>/dev/null'
echo '	then	:'
echo '	else	echo $Name: could not make $Admin >&2'
echo '		exit 1'
echo '	fi'
echo 'fi'
echo ''
echo 'ACT='
echo 'Revision='
echo ''

# create a shell script with the appropriate calls to $RCSBIN/ci

# construct the successive configuration records
awk '
	BEGIN {				# awk has problems with empty arrays
		rev[""] = "-";		# so we "declare" it
	}
	NF == 0 {			# empty line, end of one CM
		print "# New configuration record";
		print "cp /dev/null $UsrCfr";
		for (f in rev) {
			if (rev[f] != "-") {
				print "echo '\''" rev[f] "| " f "|'\'' >>$UsrCfr";
			}
		}
		print "CI " msg;	# provisional form of call to ci
		print "DT " date "@" author;# provisional form of dating
	}
	NF == 1 {			# single file name, to be removed
		rev[$1] = "-";
	}
	NF > 1 {			# normal modification
		date = $1 "." $2;
		rev[$3] = $4;
		author = $5;
		msg = $0;		# to preserve the message
	}
' |

# double all backslashes; the shell "read" seems to strip them off again
sed 's/\\/&&/g' |

# expand the CI and DT lines
while read LINE
do
	case "$LINE" in
	CI*)				# the ci lines
		echo '# check it in'
		echo "$LINE" |
		sed '
			# mark first semicolon
			s/;.*/;;;&/
			# discard anything in front of it and itself
			s/.*;;;; //
			# quote funny characters
			s/\\/\\&/g
			s/"/\\&/g
			s/`/\\&/g
			s/\$/\\&/g
			# edit into $Message for $CVSLIB/CC.aux
			s/.*/Message="&"/
			s/\\\\n/\
/g
		'
		echo '. $CVSLIB/CC.aux			# commit confrec file'
		echo ''
		;;

	DT*)					# the dating lines
		echo '# fake the creation date and author of this entry'
		echo '# the RCS file is unwritable; correct'
		echo 'chmod +w $RcsCfr'
		echo '# edit the $RcsCfr by brute force'
		echo 'ed - $RcsCfr <<\!'
		echo '/^date     /s/^date     .................;//'
		echo 's/^ *author[^;]*;//'
		echo "$LINE" |
		sed '
			s/\//./g
			s/:/./g
			s/DT //
			s/@/;  author /
			s/.*/s\/^\/date     &;\//
		'
		echo 'w'
		echo 'q'
		echo '!'
		echo '# make the RCS unwritable again'
		echo 'chmod -w $RcsCfr'
		echo ''
		;;

	*)					# normal lines
		echo "$LINE"
		;;
	esac
done

echo 'exit 0'

exit 0

