#!/bin/sh

# This test script is in the public domain.

set -ev

test $DARCS || DARCS=$PWD/../darcs
ACTUAL_DARCS=`which $DARCS`
DARCSPATH=`dirname $ACTUAL_DARCS`
PATH="$DARCSPATH:$PATH"
export PATH

rm -rf temp1 temp2 temp3 temp4 temp5

mkdir temp1
cd temp1
cat > foo <<EOF
line1
line2
line3
line4
line5
line6
line7
EOF
$DARCS initialize
echo record author me > _darcs/prefs/defaults
$DARCS add foo
$DARCS record -a -m addfoo

cd ..
$DARCS get temp1 temp4

chgrec () {
    set -ev
    sed -e "$1" foo > foo2
    mv foo2 foo
    $DARCS record -a --ignore-times -m "$2"
}

cd temp1
chgrec '3i\
line2.1\
line2.2' inssub2
chgrec '6s/line4/Line 4/' Line4

cd ..
$DARCS get temp1 temp2
$DARCS get temp1 temp3
cd temp1

chgrec '1i\
line0' line0
chgrec '7s/Line 4/LINE FOUR/' LINE4
chgrec '$i\
/LastLine/' LastLine
chgrec '8i\
line4.1' line4.1

cd ../temp3
$DARCS pull -p LastLine -av
echo record author me > _darcs/prefs/defaults
chgrec '1,$s/line1/FirstLine/' FirstLine

cd ../temp4

pullcheck () {
    $DARCS pull $* | grep '  \*'
}

pullcheck ../temp1 --dry-run > p1.out
cat > p1.req <<EOF
  * inssub2
  * Line4
  * line0
  * LINE4
  * LastLine
  * line4.1
EOF
diff p1.req p1.out

pullcheck ../temp1 --dry-run --complement > p2.out
diff p1.out p2.out

pullcheck --dry-run --complement ../temp1 ../temp2 > p3.out
cat > p3.req <<EOF
  * line0
  * LastLine
EOF
diff p3.req p3.out

pullcheck --dry-run --complement ../temp1 ../temp3 > p4.out
cat > p4.req <<EOF
  * line0
EOF
diff p3.req p3.out

pullcheck --dry-run --complement ../temp1 ../temp2 ../temp3 > p5.out
diff p4.out p5.out

pullcheck --dry-run --complement ../temp1 ../temp2 ../temp3 ../temp2 ../temp2 ../temp3 ../temp3 ../temp2 > p6.out
diff p4.out p6.out

pullcheck --dry-run --complement ../temp3 ../temp2 > p7.out
cat > p7.req <<EOF
  * LastLine
EOF

$DARCS pull --dry-run --complement ../temp2 ../temp3 > p8.out
grep "No remote changes to pull in!" p8.out

# because duplicates are stripped before performing action,
# this is the same as: darcs pull ../temp1
pullcheck --dry-run --complement ../temp1 ../temp1 > p9.out
diff p1.req p9.out

# so the "null" pull must be tested this way:
$DARCS get ../temp1 ../temp5
$DARCS pull --dry-run --complement ../temp1 ../temp5 > p9.out
grep "No remote changes to pull in!" p9.out

$DARCS pull -av --complement ../temp1 ../temp3
$DARCS check

cd ..
rm -rf temp1 temp2 temp3 temp4 temp5
