#!/bin/sh


set -ev

# We'd just use `diff -x _darcs -r' if -x was portable.
diffx () {
    { find $1 -type f; find $2 -type f; } |
      sed  -e '/.*\/_darcs\//d' -e 's;^[^/]*;;' | sort | uniq |
      {
	while read part; do
	    diff -c $1$part $2$part
	done
      }
}

test $DARCS || DARCS=$PWD/../darcs

rm -rf temp1 temp2 temp3 temp4 temp5
mkdir temp1
cd temp1
$DARCS init --hashed-inventory
touch foo
$DARCS add foo
$DARCS rec -m t1 -a -A tester
echo 1 >> foo
$DARCS what -s | grep -v No\ changes
$DARCS what -l | grep -v No\ changes
$DARCS what -sl | grep -v No\ changes

cd ..

$DARCS get temp1 temp2
cd temp2
$DARCS changes

cd ..
$DARCS get --hashed-inventory temp1 temp3
cd temp3
$DARCS changes
cp _darcs/hashed_inventory inv
$DARCS optimize
diff -c inv _darcs/hashed_inventory
rm inv
cd ..

diffx temp2 temp3
diff -rc temp1/_darcs/pristine temp3/_darcs/pristine
diff -rc temp2/_darcs/pristine temp3/_darcs/pristine

cd temp1
$DARCS record -a -A tester -m t2
$DARCS push ../temp2 -a
$DARCS push ../temp3 -a
cd ..

diffx temp2 temp3
diff -rc temp1/_darcs/pristine temp3/_darcs/pristine
diff -rc temp2/_darcs/pristine temp3/_darcs/pristine

cd temp1
date > foo
$DARCS record -a -A tester -m t3
cd ../temp2
$DARCS pull -a
cd ../temp3
$DARCS pull -a
$DARCS check
cd ..

diffx temp2 temp3
diff -rc temp1/_darcs/pristine temp3/_darcs/pristine
diff -rc temp2/_darcs/pristine temp3/_darcs/pristine

cd temp1
$DARCS put --hashed-inventory ../temp4
cd ..

diffx temp2 temp4
diff -rc temp2/_darcs/pristine temp4/_darcs/pristine

cd temp1
$DARCS tag -A tagger -m atag
$DARCS check
$DARCS optimize
$DARCS check
$DARCS changes | grep t1
cd ..

cd temp3
date > foobarpatch
$DARCS add foobarpatch
$DARCS record -a -A silly -m foobarpatch
$DARCS check
$DARCS optimize
$DARCS check
$DARCS pull -a ../temp1
$DARCS check
$DARCS optimize --reorder-patches
$DARCS check
grep 'Starting with inventory' _darcs/hashed_inventory
cd ..

cd temp1
$DARCS pull -a ../temp3
cd ..

diff -c temp1/_darcs/hashed_inventory temp3/_darcs/hashed_inventory

cd temp4
$DARCS pull -p foobarpatch -a ../temp3
$DARCS pull -a ../temp1
$DARCS optimize --reorder
$DARCS check
$DARCS push ../temp1
cd ..

diff temp1/_darcs/hashed_inventory temp4/_darcs/hashed_inventory

rm -rf temp3
$DARCS get --old-fashioned temp1 temp3

$DARCS get --hashed-inventory temp3 temp5
rm -rf temp5

$DARCS get temp1 temp5 --hashed-inventory --old-fashioned
cd temp5
$DARCS obliterate --last 3 -a
$DARCS pull ../temp1 -a
$DARCS obliterate --last 3 -a
$DARCS pull ../temp2 -a
$DARCS check
$DARCS obliterate --last 3 -a
$DARCS pull ../temp3 -a
$DARCS obliterate --last 3 -a
$DARCS pull ../temp4 -a
cd ..

cd temp4
$DARCS obliterate --last 3 -a
$DARCS pull ../temp5 -a
cd ..

cd temp3
$DARCS obliterate --last 3 -a
$DARCS pull ../temp5 -a
cd ..

cd temp2
$DARCS obliterate --last 3 -a
$DARCS pull ../temp5 -a
cd ..

cd temp1
$DARCS obliterate --last 3 -a
$DARCS pull ../temp5 -a
cd ..

rm -rf temp1 temp2 temp3 temp4 temp5

