# -*- sh -*- common set-up code for each test

# We use a trap below for cleanup.  This requires us to go through
# hoops to get the right exit status transported through the signal.
# So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
# sh inside this function.
Exit()
{
  set +e
  (exit $1)
  exit $1
}

skip_test_()
{
  echo "$0: skipping test: $@" | head -1 1>&9
  echo "$0: skipping test: $@" 1>&2
  Exit 77
}

require_bzr_()
{
  have_bzr=1
  bzr_version=`(bzr --version) 2> /dev/null | sed 1q`
  if test $? != 0; then
    have_bzr=0
  else
    case $bzr_version in
      "Bazaar (bzr) "1.[3-9]*) ;;
      "Bazaar (bzr) "1.1[0-9]*) ;;
      "Bazaar (bzr) "[2-9]*) ;;
      *) have_bzr=0 ;;
    esac
  fi

  test $have_bzr = 0 &&
    skip_test_ "this test requires bzr"
}

require_git_()
{
  have_git=1
  git_version=`(git --version) 2>/dev/null`
  if test $? != 0; then
    have_git=0
  else
    case $git_version in
      "git version "1.[3-9]*) ;;
      "git version "2*) ;;
      *) have_git=0 ;;
    esac
  fi

  test $have_git = 0 &&
    skip_test_ "this test requires git"
}

require_cvs_()
{
  have_cvs=1
  cvs_version=`(cvs --version) 2>/dev/null`
  if test $? != 0; then
    have_cvs=0
  fi

  test $have_cvs = 0 &&
    skip_test_ "this test requires cvs"
}

require_hg_()
{
  have_hg=1
  hg_version=`(hg --version) 2> /dev/null | sed 1q`
  if test $? != 0; then
    have_hg=0
  else
    case $hg_version in
      "Mercurial Distributed SCM (version 0.9"*) ;;
      "Mercurial Distributed SCM (version "[a-f1-9]*) ;;
      *) have_hg=0 ;;
    esac
  fi

  test $have_hg = 0 &&
    skip_test_ "this test requires hg"
}

pwd=`pwd`
t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$

# Don't clean up if $DEBUG is set.
if test "${DEBUG+set}" != set; then
  trap 'st=$?; cd "$pwd" && chmod -R u+rwx $t0 && rm -rf $t0 && exit $st' 0
  trap '(exit $?); exit $?' 1 2 13 15
fi
