#! /bin/sh
#
# $Header: /cvs/inplace/do-test,v 1.2 2001/05/06 11:52:20 richard Exp $
#

set -e

trap "rm -rf t" EXIT INT HUP TERM

fatal() {
  echo "$@" 1>&2
  exit 1
}

fresh() {
  rm -rf t
  mkdir t
  x=1
  while test $x -lt 1000; do
    echo "file $x" > t/$x
    x=`expr $x + 1`
  done
}

fresh
inplace -p "t/1*" -- sed s/file/new/

x=1
while test $x -lt 1000; do
  read a b < t/$x
  if test "$b" != $x; then
    fatal "t/$x mangled"
    
  fi
  case $x in
  1* )
    if test "$a" != new; then
      fatal "t/$x not updated"
    fi
    ;;
  * )
    if test "$a" != file; then
      fatal "t/$x mangled"
    fi
  esac
  x=`expr $x + 1`
done

fresh
inplace -l -p "t/1*" -- \
    sh -c 'read a b
	   if test $b = 109; then
	     exit 1
           else
             echo "modified $a $b"
           fi' > t/output

if grep t/109 t/output > /dev/null 2>&1; then
  :
else
  fatal "t/109 missing from -l output"
fi

fresh
inplace -c -l -p "t/1*" -- \
    sh -c 'read a b
	   if test $b = 109; then
	     exit 1
           else
             echo "modified $a $b"
           fi' > t/output

if grep t/109 t/output > /dev/null 2>&1; then
  :
else
  fatal "t/109 missing from -l output"
fi

if test `wc -l < t/109` != 1; then
  fatal "-c -l output should be only 1 line"
fi
