#!/bin/sh

ENTRY=6.0

echo "Pull up the following revisions(s) (requested by X in ticket #X):" > commitmsg

runfile()
{
local IFS
IFS='
'
for line in `awk '{
       if($1=="cvs" && $2=="rdiff") line=$0
       else line=line $0
       if(substr($NF,length($NF),1)=="\\\\") {
               line=substr(line,1,length(line)-1)
       } else {
               if(substr(line,1,10)=="cvs rdiff ") printf "%s\n",line
               line=""
       }
}'`
do
       IFS='
'
       set $line
       if [ "$3" = "-u" ]; then
               shift
       fi
       opt="$3 $4"
       revfrom="${3#-r}"
       revto="${4#-r}"
       rev=`echo | awk -v from="$revfrom" -v to="$revto" '{
               sub("^1.","",from)
               sub("^1.","",to)
               if(from+1==to) printf "1.%d\n", to
               else printf "1.%d-1.%d\n", from+1, to
       }'`
       shift 4
       for file in $*
       do
               file="${file#src/}"
               echo "cvs diff -kk $opt ${file}"
               echo -n "OK? (y/n/p)[y]:" > /dev/tty
               read r < /dev/tty
               case "$r" in
               n*|N*)
                       continue
                       ;;
               p*|P*)
                       echo "Patching ${file} via patch"
                       viapatch=1
                       ;;
               *)
                       echo "Patching ${file}"
                       viapatch=0
                       ;;
               esac
               if [ "$viapatch" = 0 ]; then
                       cvs diff -kk $opt ${file} | patch
               fi
               echo "${file}                   ${rev}" >> doc/CHANGES-$ENTRY
               echo "  ${file}:        revision ${rev}" >> commitmsg
       done
done
}

if [ "$1" = "-u" ]; then
       echo "Please enter URLs"
       while read url
       do
               [ -z "$url" ] && break
               echo "Fetching $url" > /dev/tty
               ftp -4 -o - $url
       done | runfile
elif [ "$#" -ge 1 ]; then
       for url in $*
       do
               echo "Fetching $url" > /dev/tty
               ftp -4 -o - $url
       done | runfile
else
       runfile
fi