#!/bin/bash
cvmfs_test_name="Merge Nested Catalog Containing Chunked File"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

inflate_file() {
  local destination_file=$1
  local source_file=$2
  local desired_file_size=$3

  touch $destination_file
  while [ $(stat -c %s $destination_file) -lt $desired_file_size ]; do
    cat $source_file >> $destination_file
  done
}

produce_files_in() {
  local working_dir=$1

  pushdir $working_dir

  # create some small files that most likely get not chunked
  mkdir small_files
  echo "Die Sonne tönt nach alter Weise"     > small_files/verse1
  echo "In Brudersphären Wettgesang,"        > small_files/verse2
  echo "Und ihre vorgeschriebne Reise"       > small_files/verse3
  echo "Vollendet sie mit Donnergang."       > small_files/verse4
  echo "Ihr Anblick gibt den Engeln Stärke," > small_files/verse5
  echo "Wenn keiner sie ergründen mag;"      > small_files/verse6
  echo "Die unbegreiflich hohen Werke"       > small_files/verse7
  echo "Sind herrlich wie am ersten Tag."    > small_files/verse8

  # create a full poem in one file that will get concatinated later
  local bigtxtfile=small_files/heidenroeslein
  touch $bigtxtfile
  echo "Heidenröslein"                     >> $bigtxtfile
  echo ""                                  >> $bigtxtfile
  echo "Sah ein Knab' ein Röslein stehn, " >> $bigtxtfile
  echo "Röslein auf der Heiden, "          >> $bigtxtfile
  echo "War so jung und morgenschön, "     >> $bigtxtfile
  echo "Lief er schnell es nah zu sehn, "  >> $bigtxtfile
  echo "Sah's mit vielen Freuden. "        >> $bigtxtfile
  echo "Röslein, Röslein, Röslein rot, "   >> $bigtxtfile
  echo "Röslein auf der Heiden. "          >> $bigtxtfile
  echo ""                                  >> $bigtxtfile
  echo "Knabe sprach: ich breche diche, "  >> $bigtxtfile
  echo "Röslein auf der Heiden! "          >> $bigtxtfile
  echo "Röslein sprach: ich steche dich, " >> $bigtxtfile
  echo "Daß du ewig denkst an mich, "      >> $bigtxtfile
  echo "Und ich will's nicht leiden. "     >> $bigtxtfile
  echo "Röslein, Röslein, Röslein rot, "   >> $bigtxtfile
  echo "Röslein auf der Heiden. "          >> $bigtxtfile
  echo ""                                  >> $bigtxtfile
  echo "Und der wilde Knabe brach"         >> $bigtxtfile
  echo "'s Röslein auf der Heiden; "       >> $bigtxtfile
  echo "Röslein wehrte sich und stach, "   >> $bigtxtfile
  echo "Half ihr doch kein Weh und Ach, "  >> $bigtxtfile
  echo "Mußt' es eben leiden. "            >> $bigtxtfile
  echo "Röslein, Röslein, Röslein rot, "   >> $bigtxtfile
  echo "Röslein auf der Heiden."           >> $bigtxtfile
  echo ""                                  >> $bigtxtfile
  echo "  Johann Wolfgang von Goethe"      >> $bigtxtfile
  echo ""                                  >> $bigtxtfile
  echo ""                                  >> $bigtxtfile

  # create a big binary file that will get chunked
  mkdir big_file
  touch big_file/.cvmfscatalog
  inflate_file big_file/1megabyte /bin/ls 1000000
  inflate_file big_file/10megabyte big_file/1megabyte 1000000
  inflate_file big_file/50megabyte big_file/10megabyte 50000000

  # create a big ascii text file that will get chunked
  inflate_file big_file/einige_heidenroeslein $bigtxtfile 100000
  inflate_file big_file/ein_paar_heidenroeslein big_file/einige_heidenroeslein 1000000
  inflate_file big_file/ein_paar_mehr_heidenroeslein big_file/ein_paar_heidenroeslein 10000000
  inflate_file big_file/viele_heidenroeslein big_file/ein_paar_mehr_heidenroeslein 60000000

  popdir
}

change_files_in() {
  local working_dir=$1

  pushdir $working_dir

  rm big_file/.cvmfscatalog

  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 || return $?

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

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo "starting transaction to edit repository"
  start_transaction $CVMFS_TEST_REPO || return $?

  echo "remove the nested catalog (chunked entry must be merged into parent)"
  change_files_in $repo_dir || return 5

  echo "remove the nested catalog mark in the reference directory"
  change_files_in $reference_dir || return 6

  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 || return $?

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

  return 0
}

