#!/bin/bash

cvmfs_test_name="Delete and Recreate Directory"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

produce_files_in() {
  local working_dir=$1

  pushdir $working_dir

  mkdir dir1 # stays empty
  mkdir dir2
  mkdir dir3
  mkdir dir4
  mkdir dir5
  mkdir dir6 # stays empty

  mkdir dir2/ndir1
  mkdir dir2/ndir2

  mkdir dir3/ndir3

  mkdir dir5/ndir4
  mkdir dir5/ndir5
  mkdir dir5/ndir6

  mkdir dir2/ndir1/nndir1
  mkdir dir2/ndir1/nndir2

  mkdir dir5/ndir5/nndir3

  touch dir2/ndir1/foo
  touch dir2/ndir1/bar
  touch dir2/ndir1/nndir2/file

  echo "meaningless content" > dir4/meaningless_file
  ln -s meaningless_file dir4/symlink_to_meaningless_file

  echo "Reading this text is prohibited by federal law!" > dir5/ndir5/nndir3/outlaw
  ln dir5/ndir5/nndir3/outlaw dir5/ndir5/nndir3/hardlinkToOutlaw1
  ln dir5/ndir5/nndir3/outlaw dir5/ndir5/nndir3/hardlinkToOutlaw2
  echo "NYPD! Stahp!" > dir5/ndir5/nndir3/NYPD

  popdir
}

change_files_in() {
  local working_dir=$1

  pushdir $working_dir

  rm -fR dir2 || return 201
  rm -fR dir5 || return 202

  mkdir dir2 || return 203
  mkdir dir5 || return 204

  mkdir dir2/opdir1 || return 205
  mkdir dir2/opdir2 || return 206

  mkdir dir2/opdir2/nopdir1 || return 207
  mkdir dir2/opdir2/nopdir2 || return 208

  touch dir2/file || return 209
  echo "Wheeeee!" > dir2/opdir2/rollercoaster || return 210
  echo "Wheeeeeeeeee!!" > dir2/opdir1/anotherrollercoaster || return 211
  ln dir2/opdir2/rollercoaster dir2/opdir2/fasttracktorollercoaster || return 212

  ln -s opdir1/anotherrollercoaster dir2/waitingqueueforrollercoaster || return 213

  echo "Getting tired of all this meaningless text" > dir5/meaningless || return 214
  echo "The quick brown fox jumps over the lazy dog!" > dir5/meaningful || return 215

  popdir
}

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

  local scratch_dir=$(pwd)
  mkdir reference_dir
  local reference_dir=$scratch_dir/reference_dir

  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 "*** putting some stuff in the new repository"
  produce_files_in $repo_dir || return 3

  echo "*** putting exactly the same stuff in the scratch space for comparison"
  produce_files_in $reference_dir || return 4

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

  echo "*** compare the results of cvmfs to our reference copy"
  compare_directories $repo_dir $reference_dir $CVMFS_TEST_REPO || return $?

  # ============================================================================

  echo "*** init a new transaction to change something in repository $CVMFS_TEST_REPO"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "*** change stuff in the repository"
  change_files_in $repo_dir || return $?

  echo "*** change exactly the same stuff in the scratch space"
  change_files_in $reference_dir || return $?

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

  echo "*** compare the changed directories"
  compare_directories $repo_dir $reference_dir $CVMFS_TEST_REPO || return $?

  # ============================================================================

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

  return 0
}

