#!/bin/sh
# http://www.berklix.com/~jhs/src/bsd/jhs/bin/local/mail/ctm-freebsd-svn-incoming
# ~jhs/public_html/src/bsd/jhs/bin/local/mail/ctm-freebsd-svn-incoming
# By "Julian H. Stacey" from

type=svn-cur
. /usr/local/bin/ctm-freebsd-common
for object in $log $pieces/.dummy $deltas/.dummy
	do
	touch $object || \
		( printf "${id_failed_to}touch $object\n" | \
			mail -s "$prog_failed_error" $notify ; \
		exit 1 )
	done
$CTMRMAIL
cd $deltas
# Find name of next delta to apply:
# Fairly often the deltas arrive by mail out of order, especialy if one
# is big, & perhaps made out of pieces, eg:
#        21996 Mar  5 13:20 svn-cur.02293.xz
#	770424 Mar  6 11:24 svn-cur.02294.xz
#	 63352 Mar  6 11:15 svn-cur.02295.xz
# Avoid files with names like eg:
#	svn-cur.01900xEmpty.xz.sig
#	svn-cur.01900xEmpty.xz
#	svn-cur.02295.xz.sig
# If we use: xx=`ls -1 | grep .xz | grep -v Empty | tail -1`
#	On 1st receiving 5.xz it would fail to apply 5.xz, as no 4.xz.
#	On 2nd receiving 4.xz it would fail to apply 5.xz probably,
#	as /usr/sbin/ctm not intelligent enough to apply previous 4.xz.
# If we use: xx=`ls -r | grep .xz | grep -v Empty | head -1`
#	It would also try 5.xz twice, & fail.
# So be clever, & feed it the last few filenames, probably most of
# time all but last of which have already been applied.
xxr=`ls -t | grep .xz | grep -v .xz.sig | grep -v Empty | head -1`
xx1=`ls -1 | grep .xz | grep -v .xz.sig | grep -v Empty | tail -1`
xx2=`ls -1 | grep .xz | grep -v .xz.sig | grep -v Empty | tail -2 | head -1`
xx3=`ls -1 | grep .xz | grep -v .xz.sig | grep -v Empty | tail -3 | head -1`
xx4=`ls -1 | grep .xz | grep -v .xz.sig | grep -v Empty | tail -4 | head -1`
xx5=`ls -1 | grep .xz | grep -v .xz.sig | grep -v Empty | tail -5 | head -1`
chmod 444 $xxr
nice md5 $xxr >> $logdir/$type.md5
# JJLATER I think there''s no longer (if there ever was?)
# a benefit to noting these .md5 ,
# Possibly I just generated the md5 to compare with other sites where
# I could also run md5 ?
# JJLATER I guess the .sig files on ftp sites are equivalent of
#  the trailing sig. in:
#	-----BEGIN PGP SIGNED MESSAGE-----
#	Hash: SHA256
#
#	CTM_MAIL BEGIN ports-cur.11073.gz 1 1
#	...... chunk ....
#	CTM_MAIL END 22154
#	-----BEGIN PGP SIGNATURE-----
#	Version: GnuPG v2
#
#	...... trailing sig chunk ....
#	-----END PGP SIGNATURE-----
# but how is the .sig file used with the non .sig file ?
# I have inspected
#	ftp://ctm.berklix.org/pub/FreeBSD/development/CTM/scripts/\
#		library-no-keys.tar.gz
#		library/ctm/bin/create-INDEX
#		library/ctm/bin/purge-ftp
#		library/ctm/bin/purge-ftp-test
#		library/ctm/bin/test-all-sigs
# but still not clear.




# ctm-freebsd-src-12-incoming has:
## if ! [ -x $CTMPATH$CTM ]; then  # {
##         echo "Skipping applying to a tree as deltas for $type need to be able to create symbolic links" \
##                 | mail -s "$prog_skipped_error" $notify
##         exit 0
## else
##         # echo "Continuing, Not skipping, will apply to trees for $type" \
##  	#	| mail -s "$prog_skipped_error" $notify
## fi                              # }
# The above code segment not needed here
# as SVN tree does not need symbolic links, at least yet.

bases=""
# bases="$bases /usr/svn"
#	/usr/svn -> /pri/FreeBSD/development/FreeBSD-SVN

bases="$bases /pub/FreeBSD/development/FreeBSD-SVN"
# no equiv in ctm-freebsd-svn-incoming:
#	bases="$bases /pub/FreeBSD/development/FreeBSD-SVN"

bases="$bases /pri/FreeBSD/development/FreeBSD-SVN"
#	Uncommenting /pri might be dangerous, as it might upgrade a /usr/svn
#	in the middle of a long running svn operation.

# Might hang or break script if not found by amd
# bases="$bases /host/lapr/usr/svn"

for another_base in ${bases}; do
	# -d ${another_base}	Insufficient as some dirs are not from ctm.
	# echo "DEBUG another_base=${another_base}"
	if [ -f ${another_base}/.ctm_status ]; then
		cd ${another_base}
		# /usr/svn -> /pri/FreeBSD/development/FreeBSD-SVN
		# it makes it to next echo.
		# ----------
		# I''m not aware we ever
                # had a problem with not creating symbolic link from
                # ctm command in svn tree, so I dont believe the $CTMPATH prefix
                # is necessary before $CTMPATH$CTM , so CTMPATH can be nulled
                CTMPATH=
		$CTMPATH$CTM $deltas/$xx5	2>&1 > /dev/null
		$CTMPATH$CTM $deltas/$xx4	2>&1 > /dev/null
		$CTMPATH$CTM $deltas/$xx3	2>&1 > /dev/null
		$CTMPATH$CTM $deltas/$xx2	2>&1 > /dev/null
		$CTMPATH$CTM $deltas/$xx1	|| \
		  echo "Failed in ${another_base} : $CTMPATH$CTM $deltas/$xx1" \
			| mail -s "$prog_failed_error" $notify
			# ctm above fails if one or more previous .xz have not
			# been applied, when one sees eg:
			#	cat .ctm_status # svn-cur 2103
			#	ctm /pub/FreeBSD/development/CTM/svn-cur/\
			#	 svn-cur.02134.xz
			#	  FS: .ctm_status md5 mismatch.
			#	  Could have forced.
			#	Release number mismatch: found 260031,\
			#	 need 260456
			#	/pub/FreeBSD/development/CTM/svn-cur/\
			#	 svn-cur.02134.xz Fatal error: Assert failed.
			#	ctm: exit(96)
	fi
done
exit 0
