#!/bin/sh
# petit shell script pour corriger des documents en envoyant un
# patch automatiquement sur un texte telecharge depuis internet.
#
# the basic way is picking text readable docs at ${ORIGURL}, showing it
# in a console text browser and in your favourite ${EDITOR}, then allow you
# to create a patch made by your code/grammatical fixes, and send 
# a "diff -u" patch to the author.
#
# 'review' Depends on wget and a minimal browser such as w3m or lynx.
#
# Copypouet Þ 2002 Benoît Rouits <brouits@free.fr>
#     released under the open mouth license v2
#
#             THE OPEN MOUTH LICENSE
#              Version 2, June 1991
#
# Nobody else than normal abnormal people are permitted to use,
# modify and distribute part or full listing of the following
# file, under the condition he/she open his/her mouth during 
# the whole process of use or modification of this piece ;-)
#

PROG=`basename $0`
MAJOR=0
MINOR=1
BROWSER=w3m # debian users might prefer 'run-mailcap' or 'sensible-browser'
test  $EDITOR || EDITOR=vi #debian users might prefer 'sensible-editor'
UA="People/$USER (compatible; $PROG/$MAJOR.$MINOR; $OSTYPE)"
MAILER=mail
LOCAL=.review

prog_init () {
ARG=$1
test $ARG || ARG="-h"
if [ \( $ARG = "-h" \) -o \( $ARG = "--help" \) ]
then
 echo "Usage: ${PROG} <url>"
 exit 0
fi
}

# main
prog_init $1
ORIGURL=$1
echo "Downloading $ORIGURL..."
wget -nv -q -o /dev/null -O $LOCAL -U "$UA"  --timestamp $ORIGURL
echo -n "Do you want to browse the file before editing it ? [Y/n]: "
read YES
test $YES || YES="y"
test $YES = "y" && $BROWSER $LOCAL
echo -n "Do you want to edit the file ? [Y/n]: "
read YES
test $YES || YES="y"
if [ $YES = "y" ]
then
 cp $LOCAL $LOCAL.edit
 $EDITOR $LOCAL.edit
 echo -n "Do you want to make a patch for this file ? [Y/n]: "
 read YES
 test $YES || YES="y"
 if [ $YES = "y" ]
 then
  diff -u $LOCAL $LOCAL.edit > $LOCAL.patch
  rm -f $LOCAL.edit
  echo -n "Do you want to send the patch to the author with $MAILER? [Y/n]: "
  read YES
  test $YES || YES="y"
  if [ $YES = "y" ]
  then
   echo -n "Enter the email adress of the author: "
   read ADDR
   echo "Enter a subject for your email: [patch]"
   read SUBJ
   test "$SUBJ" || SUBJ='[patch]'
   echo "Enter a summary of your patch for the author: (end with ^D)"
   cat > $LOCAL.mail
   echo '## patch begin here:' >> $LOCAL.mail
   cat $LOCAL.mail $LOCAL.patch | $MAILER -s "$SUBJ" $ADDR
   rm $LOCAL.mail
  fi
 fi
fi
mv $LOCAL.patch `date +%s`$LOCAL.patch
rm $LOCAL
echo "Bye"
exit 0

