#!/bin/bash
cvmfs_test_name="Repository gateway large files"
cvmfs_test_autofs_on_startup=false
cvmfs_test_suites="quick"

# This test verifies the correct publication of large files
# through a repository gateway

cvmfs_run_test() {
    set_up_repository_gateway || return 1

    echo "Checking transaction + publish"

    ## Transaction 1

    echo "  Starting transaction 1"
    cvmfs_server transaction test.repo.org || return 10

    echo "  Copying the payload into repository"
    rm -v /cvmfs/test.repo.org/new_repository || return 11
    curl -o /tmp/linux.tar.xz http://ecsft.cern.ch/dist/cvmfs/test-data/linux-4.12.8.tar.xz
    cp -v /tmp/linux.tar.xz /cvmfs/test.repo.org/ || return 12
    local hash_in_1=$(sha1sum /tmp/linux.tar.xz | cut -d' ' -f1)

    echo "  Publishing changes 1"
    cvmfs_server publish test.repo.org || return 13
    cvmfs_server check -i test.repo.org || return 14

    echo "  Computing hash of published file"
    local hash_out_1=$(sha1sum /cvmfs/test.repo.org/linux.tar.xz | cut -d' ' -f1)
    if [ x"$hash_in_1" != x"$hash_out_1" ]; then
        return 15
    fi

    echo "  Work on nested catalogs" || return 20
    cvmfs_server transaction test.repo.org
    mkdir /cvmfs/test.repo.org/nested || return 21
    cp /cvmfs/test.repo.org/linux.tar.xz /cvmfs/test.repo.org/nested/ || return 22
    touch /cvmfs/test.repo.org/nested/.cvmfscatalog || return 23
    mkdir /cvmfs/test.repo.org/nested/sub || return 24
    touch /cvmfs/test.repo.org/nested/sub/.cvmfscatalog || return 24
    mkdir /cvmfs/test.repo.org/nested/sub/dir || return 24
    cvmfs_server publish test.repo.org || return 24
    cvmfs_server check -i test.repo.org || return 25

    cvmfs_server transaction test.repo.org
    mv /cvmfs/test.repo.org/nested /cvmfs/test.repo.org/bar || return 40
    cvmfs_server publish test.repo.org || return 41
    cvmfs_server check -i test.repo.org || return 42

    cvmfs_server transaction test.repo.org
    rm /cvmfs/test.repo.org/bar/.cvmfscatalog || return 30
    cvmfs_server publish test.repo.org || return 31
    cvmfs_server check -i test.repo.org || return 32

    return 0
}

