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

type=ports-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
xxr=`ls -t | grep .gz | grep -v .gz.sig | grep -v Empty | head -1`
xx1=`ls -1 | grep .gz | grep -v .gz.sig | grep -v Empty | tail -1`
xx2=`ls -1 | grep .gz | grep -v .gz.sig | grep -v Empty | tail -2 | head -1`
xx3=`ls -1 | grep .gz | grep -v .gz.sig | grep -v Empty | tail -3 | head -1`
xx4=`ls -1 | grep .gz | grep -v .gz.sig | grep -v Empty | tail -4 | head -1`
xx5=`ls -1 | grep .gz | grep -v .gz.sig | grep -v Empty | tail -5 | head -1`
chmod 444 $xxr
nice md5 $xxr >> $logdir/$type.md5

bases=""
bases="$bases /pub/FreeBSD/branches/-current/ports"
bases="$bases /pri/FreeBSD/branches/-current/ports"

# bases="$bases /pri/FreeBSD/branches/amd64/-current/ports"
# bases="$bases /pri/FreeBSD/branches/i386/-current/486/ports"
# bases="$bases /pri/FreeBSD/branches/i386/-current/586/ports"
# bases="$bases /pri/FreeBSD/branches/i386/-current/686/ports"
# bases="$bases /pri/FreeBSD/branches/i386/-current/ports"
#	Uncommenting /pri is risky, as it may upgrade a /usr/ports
#	in the middle of a long running make.
# bases="$bases /usr/ports"
#	Uncommenting /usr/ports may not work as its probably owned by jhs,
#	so he can build in there, not owned by mailnull, so automatic 
#	application of ctm deltas would fail.

for another_base in ${bases}; do
	if [ -f ${another_base}/.ctm_status ]; then
		cd ${another_base}
		# I''m not aware we ever
                # had a problem with not creating symbolic link from
                # ctm command, in ports/ 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        2>&1 || \
		  echo "Failed in ${another_base} : $CTMPATH$CTM $deltas/$xx1" \
			| mail -s "$prog_failed_error" $notify 
	fi
done
exit 0
