#!/bin/bash
cvmfs_test_name="Recreate nested catalog tree"
cvmfs_test_autofs_on_startup=false

produce_files_in() {
  local working_dir=$1

  pushdir $working_dir

  mkdir -p nested/foo/bar
  touch nested/.cvmfscatalog

  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 "starting transaction to edit repository"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "createing nested catalog structure in repository"
  produce_files_in $repo_dir || return 3

  echo "creating CVMFS snapshot"
  publish_repo $CVMFS_TEST_REPO || return $?

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

  echo "starting transaction to recreate nested catalog tree"
  start_transaction $CVMFS_TEST_REPO || return $?

  rm -rf /cvmfs/$CVMFS_TEST_REPO/nested || return 5
  produce_files_in $repo_dir || return 6

  echo "creating new CVMFS snapshot"
  publish_repo $CVMFS_TEST_REPO || return $?

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

  return 0
}

