#!/bin/sh
set -ev

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

if [ x${REMOTE_DIR} = x ]; then
  REMOTE_DIR=sshtest
fi

if [ x"${USE_PUTTY}" != x ]; then
  export DARCS_SSH=plink
  export DARCS_SCP=pscp
  export DARCS_SFTP=psftp
fi

if [ x"${USE_CONTROL_MASTER}" != x ]; then
  DARCS_SSH_FLAGS="--ssh-cm"
  export DARCS_SSH_FLAGS
fi

if [ x"${DARCS_SSH}" = x ]; then
  SSH=ssh
else
  SSH=${DARCS_SSH}
fi

rm -rf tempssh
mkdir tempssh
cd tempssh

cleanup () {
  cd ..
  rm -rf tempssh
}

if [ x${REMOTE} = x ]; then
  echo
  echo "Note: to enable full SSH testing, set REMOTE to some SSH path first,"
  echo "      e.g. REMOTE=you@server.org $0"
  cleanup
  exit 0 # exit normally in case we are being run from the shell harness
fi

# ================ Setting up remote repositories ===============
${SSH} ${REMOTE} "\
rm -rf ${REMOTE_DIR}; \
mkdir ${REMOTE_DIR}; \
cd ${REMOTE_DIR}; \
\
mkdir testrepo; cd testrepo; \
darcs init; \
echo moi > _darcs/prefs/author; \
touch a; darcs add a; darcs record a -a -m 'add file a'; \
echo 'first line' > a; darcs record a -a -m 'add first line to a'; \
cd ..; \
\
darcs get testrepo testrepo-pull; \
cd testrepo-pull; \
echo moi > _darcs/prefs/author; \
touch b; darcs add b; darcs record b -a -m 'add file b'; \
echo 'other line' > b; darcs record b -a -m 'add other line to b'; \
cd ..; \
\
darcs get testrepo testrepo-push; \
darcs get testrepo testrepo-send; \
"

# ================ Settings ===============
echo ${DARCS_SSH_FLAGS}
echo ${DARCS_SSH}
echo ${DARCS_SCP}
echo ${DARCS_SFTP}

# ================ Checking darcs get ==================
${DARCS} get ${DARCS_SSH_FLAGS} ${REMOTE}:${REMOTE_DIR}/testrepo ${DARCS_SSH_FLAGS}
# check that the test repo made it over
[ -d testrepo ]
[ -d testrepo/_darcs ]
[ -e testrepo/a ]

# if the above test is disabled we just init a blank repo
# so that the other tests can continue
if [ ! -d testrepo ]; then
  mkdir testrepo
  cd testrepo
  darcs init
  cd ..
fi

# ================ Checking darcs pull =================
${DARCS} get ${DARCS_SSH_FLAGS} testrepo testrepo-pull
cd testrepo-pull
echo yy | ${DARCS} pull ${DARCS_SSH_FLAGS} ${REMOTE}:${REMOTE_DIR}/testrepo-pull
# see if the changes got pulled over
grep "other line" b

cd ..

# ================ Checking darcs push and send ================="
${DARCS} get ${DARCS_SSH_FLAGS} testrepo testrepo-push
cd testrepo-push
echo moi > _darcs/prefs/author
echo "second line" >> a; ${DARCS} record a -am "add second line to a"
touch c; ${DARCS} add c
${DARCS} record -am "add file c" c
echo yy | ${DARCS} push ${DARCS_SSH_FLAGS} ${REMOTE}:${REMOTE_DIR}/testrepo-push
# check that the file c got pushed over
${SSH} ${REMOTE} "[ -e ${REMOTE_DIR}/testrepo-push/c ]"
echo yy | ${DARCS} send ${DARCS_SSH_FLAGS} ${REMOTE}:${REMOTE_DIR}/testrepo-send -o mybundle.dpatch
# check that the bundle was created
grep "add file c" mybundle.dpatch
cd ..

# ================ Checking darcs put =================="
cd testrepo
${DARCS} put ${DARCS_SSH_FLAGS} ${REMOTE}:${REMOTE_DIR}/testrepo-put
# check that the put was successful
${SSH} ${REMOTE} "[ -d ${REMOTE_DIR}/testrepo-put/_darcs ]"
${SSH} ${REMOTE} "[ -e ${REMOTE_DIR}/testrepo-put/a ]"
cd ..

cleanup
