#!/bin/bash
cvmfs_test_name="Test publishing process with and without local cache"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

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

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

  # add caching to config
  local repo_server_config=/etc/cvmfs/repositories.d/$CVMFS_TEST_REPO/server.conf
  cat $repo_server_config
  echo "CVMFS_SERVER_USE_CATALOG_CACHE=true" | sudo tee -a "$repo_server_config"

  # new transaction to have some cached catalogs
  start_transaction "$CVMFS_TEST_REPO" || return $?
  mkdir -p /cvmfs/"$CVMFS_TEST_REPO"/foo/bar
  touch /cvmfs/"$CVMFS_TEST_REPO"/foo/.cvmfscatalog
  touch /cvmfs/"$CVMFS_TEST_REPO"/foo/bar/.cvmfscatalog
  publish_repo "$CVMFS_TEST_REPO" || return 2

  # mount repo to extract hash
  echo "*** Mount repo to extract catalog hash of /cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog"
  local nested_ctlg_hash
  local catalog_counters
  catalog_counters=$(get_xattr catalog_counters "/cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog")
  nested_ctlg_hash=$(echo "$catalog_counters"  | grep "catalog_hash" | cut -d':' -f 2 | cut -d' ' -f 2)
  echo "   Hash found $nested_ctlg_hash"
  sudo umount $CVMFS_TEST_REPO
  echo "   Umount"


  # delete nested catalog from backend --> caching must work otherwise it fails
  echo "*** Delete /cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog"
  local retval
  ### add C flag at the end to hash to find proper file
  nested_ctlg_hash="${nested_ctlg_hash}C"
  peek_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash" || return 3
  delete_hash_from_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash"
  # echo "    Check if file was really removed"
  peek_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash" && return 5

  # check local catalog caching availability
  echo "*** Add new files to subdirectory part of deleted nested catalog."
  echo "    Will only work if catalog was cached locally"
  start_transaction $CVMFS_TEST_REPO || return $?
  touch /cvmfs/$CVMFS_TEST_REPO/foo/text1.txt
  touch /cvmfs/$CVMFS_TEST_REPO/foo/bar/text3.txt
  publish_repo $CVMFS_TEST_REPO || return 7

  # Test the reverse
  echo "*** NOW TEST THE REVERSE: fail if no caching is available"
  echo "*** Remove local caching for publishing from config."
  sudo sed -i '/CVMFS_SERVER_USE_CATALOG_CACHE/d' "$repo_server_config"
  cat $repo_server_config


  # mount repo to extract hash
  echo "*** Mount repo to extract catalog hash of /cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog"
  local nested_ctlg_hash
  local catalog_counters
  catalog_counters=$(get_xattr catalog_counters "/cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog")
  nested_ctlg_hash=$(echo "$catalog_counters"  | grep "catalog_hash" | cut -d':' -f 2 | cut -d' ' -f 2)
  echo "   Hash found $nested_ctlg_hash"
  sudo umount $CVMFS_TEST_REPO
  echo "   Umount"


  # delete nested catalog from backend --> caching must work otherwise it fails
  echo "*** Delete /cvmfs/$CVMFS_TEST_REPO/foo/bar/.cvmfscatalog"
  local retval
  ### add C flag at the end to hash to find proper file
  nested_ctlg_hash="${nested_ctlg_hash}C"
  peek_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash" || return 8
  delete_hash_from_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash"
  peek_backend "$CVMFS_TEST_REPO" "$nested_ctlg_hash" && return 9

  # check local catalog caching is unavailable
  echo "*** Try to add random file in subdirectory of delete nested catalog"
  echo "    This must fail because local catalog caching is off"
  start_transaction $CVMFS_TEST_REPO || return $?
  touch /cvmfs/$CVMFS_TEST_REPO/foo/text1.txt
  touch /cvmfs/$CVMFS_TEST_REPO/foo/bar/text3.txt
  publish_repo $CVMFS_TEST_REPO && return 10

  return 0
}
