#! /bin/sh
##
## Rdiff -- Diff 'without R version'

## Usage:
##   R CMD Rdiff from-file to-file exitstatus
## where from-file equal to '-' means STDIN

ffile=${1}
tfile=${2}
exitstatus=${3}

## sed script to get rid of the startup message
script='/^R : Copyright/,/quit R.$/{d;}'
## egrep pattern to get rid of some more???
pattern='(^Number of.*:|^Time |^Loading required package.*'
case "${ffile}" in
  *primitive-funs*)
    pattern=${pattern}'|^\[1\] [19][0-9][0-9])'
    ;;
  *)
    pattern=${pattern}'|^<environment)'
    ;;
esac  

tmpfile=${TMPDIR-/tmp}/${ffile}-d.${$}

if test ${ffile} = '-' ; then 
  ffile=''
fi

sed -e "${script}" ${ffile} | ${EGREP} -v "${pattern}" > ${tmpfile}
(sed -e "${script}" ${tfile} | \
  ${EGREP} -v "${pattern}" | \
  diff -bw ${tmpfile} - ) && exitstatus=0

rm -f ${tmpfile}

exit ${exitstatus}
