#!/bin/bash
cvmfs_test_name="Show snapshot change set"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

create_repository_content() {
	local working_dir=$1

	pushdir $working_dir

	touch file
  ln -s file symlink
  mkdir directory
  mkdir directory/nested
  touch directory/nested/.cvmfscatalog
  touch directory/foo
  mkdir dir2
  touch owner

  popdir
}

change_repository_content() {
	local working_dir=$1

	pushdir $working_dir

  echo content > file
  rm symlink
  ln -s file other-symlink
  rm directory/foo
  ln -s ../file directory/foo

  rm directory/nested/.cvmfscatalog
  touch dir2/.cvmfscatalog
  mkdir dir3
  touch dir3/new_file
  touch dir3/.cvmfscatalog
  sudo chown 99:99 owner

	popdir
}


cvmfs_run_test() {
  logfile=$1
  local repo_dir=/cvmfs/$CVMFS_TEST_REPO

  echo "*** create a fresh repository named $CVMFS_TEST_REPO with user $CVMFS_TEST_USER"
  create_empty_repo $CVMFS_TEST_REPO $CVMFS_TEST_USER || return $?

  echo "*** putting some stuff in the new repository"
  start_transaction $CVMFS_TEST_REPO || return $?
  create_repository_content $repo_dir || return 3
  publish_repo $CVMFS_TEST_REPO || return $?

  echo "*** check catalog and data integrity"
  check_repository $CVMFS_TEST_REPO -i  || return $?

  echo "*** check identity diff"
  cat << EOF > identity.reference
S F 0
S S 0
S D 0
S N 0
EOF
  cvmfs_server diff -m -s trunk $CVMFS_TEST_REPO > identity.output || return 10
  diff identity.reference identity.output || return 11

  echo "*** change repository content"
  start_transaction $CVMFS_TEST_REPO || return $?
  change_repository_content $repo_dir || return 20
  publish_repo $CVMFS_TEST_REPO || return $?

  echo "*** check catalog and data integrity"
  check_repository $CVMFS_TEST_REPO -i  || return $?

  echo "*** check forward changeset"
  cat << EOF > forward.reference
S F 1
S S 1
S D 1
S N 1
M[N] D /dir2
A F /dir2/.cvmfscatalog +0
A D /dir3
A F /dir3/new_file +0
A F /dir3/.cvmfscatalog +0
M[SC] F /file
M[UR] F /owner
R S /symlink -4
M[SMLC] FS /directory/foo
M[N] D /directory/nested
R F /directory/nested/.cvmfscatalog -0
A S /other-symlink +4
EOF
  cvmfs_server diff -im $CVMFS_TEST_REPO > forward.output || return 21
  diff forward.reference forward.output || return 22

  echo "*** check reverse changeset"
  cat << EOF > reverse.reference
S F -1
S S -1
S D -1
S N -1
M[N] D /dir2
R F /dir2/.cvmfscatalog -0
R D /dir3
M[SC] F /file
M[UR] F /owner
A S /symlink +4
M[SMLC] SF /directory/foo
M[N] D /directory/nested
A F /directory/nested/.cvmfscatalog +0
R S /other-symlink -4
EOF
  cvmfs_server diff -im -s trunk -d trunk-previous $CVMFS_TEST_REPO \
    > reverse.output || return 31
  diff reverse.reference reverse.output || return 32

  echo "*** check skipping of hidden .cvmfs directory"
  echo "CVMFS_VIRTUAL_DIR=true" | \
    sudo tee -a /etc/cvmfs/repositories.d/$CVMFS_TEST_REPO/server.conf || return 40
  start_transaction $CVMFS_TEST_REPO || return $?
  publish_repo $CVMFS_TEST_REPO || return $?
  check_repository $CVMFS_TEST_REPO -i  || return $?
  cat << EOF > hiddencvmfs.reference
S F 1
S S 0
S D 7
S N 1
EOF
  cvmfs_server diff -m $CVMFS_TEST_REPO > hiddencvmfs.output || return 41
  diff hiddencvmfs.reference hiddencvmfs.output || return 42

  return 0
}

