# clang-format off
GEO_REP_TIMEOUT=120
CHECK_MOUNT_TIMEOUT=50
#check for mount point
function check_mounted () {
  df | grep $1 | wc -l
}

function check_status_num_rows()
{
    local search_key=$1
    $GEOREP_CLI $primary $secondary status | grep -F "$search_key" | wc -l
}

function check_fanout_status_num_rows()
{
    local search_key=$1
    $GEOREP_CLI $primary status | grep -F "$search_key" | wc -l
}

function check_fanout_status_detail_num_rows()
{
    local search_key=$1
    $GEOREP_CLI $primary status detail | grep -F "$search_key" | wc -l
}

function check_all_status_num_rows()
{
    local search_key=$1
    $GEOREP_CLI status | grep -F "$search_key" | wc -l
}

function check_all_status_detail_num_rows()
{
    local search_key=$1
    $GEOREP_CLI status detail | grep -F "$search_key" | wc -l
}

function verify_checkpoint_met()
{
    local primary=$1
    local secondary=$2
    $GEOREP_CLI $primary $secondary status detail| grep -F "Yes" | wc -l
}

function check_keys_distributed()
{
    local search_key=$(cat /var/lib/glusterd/geo-replication/primary_secondary_common_secret.pem.pub)
    grep -F "$search_key" ~/.ssh/authorized_keys > /dev/null
    echo $?
}

function check_common_secret_file()
{
    stat /var/lib/glusterd/geo-replication/primary_secondary_common_secret.pem.pub
    echo $?
}

function create_rename_symlink_case()
{
    mkdir ${primary_mnt}/MUL_REN_SYMLINK
    cd ${primary_mnt}/MUL_REN_SYMLINK
    mkdir sym_dir1
    ln -s "sym_dir1" sym1
    mv sym1 sym2
    mv sym2 sym3
    mv sym3 sym4
    cd -
}

function create_data()
{
    prefix=$1

    # GF_FOP_MKNOD
    # GF_FOP_MKDIR
    # GF_FOP_UNLINK
    # GF_FOP_RMDIR
    # GF_FOP_SYMLINK
    # GF_FOP_RENAME
    # GF_FOP_LINK
    # GF_FOP_SETXATTR
    # GF_FOP_REMOVEXATTR
    # GF_FOP_CREATE
    # GF_FOP_SETATTR

    # Regular file + data
    echo "HelloWorld!" > ${primary_mnt}/${prefix}_f1
    touch ${primary_mnt}/${prefix}_f2
    touch ${primary_mnt}/${prefix}_f3

    # non-ascii filename test
    echo "Hello non-ascii" > ${primary_mnt}/${prefix}_f1_ಸಂತಸ
    touch ${primary_mnt}/${prefix}_f2_ಸಂತಸ
    touch ${primary_mnt}/${prefix}_f3_ಸಂತಸ

    # dir
    mkdir ${primary_mnt}/${prefix}_d1
    mkdir ${primary_mnt}/${prefix}_d2
    mkdir ${primary_mnt}/${prefix}_d3

    # non-ascii dir and filename test
    mkdir ${primary_mnt}/${prefix}_d1_ನನ್ನ
    mkdir ${primary_mnt}/${prefix}_d2_ಸಂತಸ
    mkdir ${primary_mnt}/${prefix}_d3_ಸಂತಸ
    echo "Hello non-ascii" >  ${primary_mnt}/${prefix}_d1_ನನ್ನ/ಸಂತಸ

    # Hardlink + non-ascii name
    ln ${primary_mnt}/${prefix}_f1 ${primary_mnt}/${prefix}_hl1
    ln ${primary_mnt}/${prefix}_f1 ${primary_mnt}/${prefix}_hl1_ಸಂತಸ

    # Symlink
    cd ${primary_mnt}
    ln -s ${prefix}_f1 ${prefix}_sl1
    ln -s ${prefix}_f1 ${prefix}_sl1_ಸಂತಸ
    cd -

    # UNLINK
    rm ${primary_mnt}/${prefix}_f2
    rm ${primary_mnt}/${prefix}_f2_ಸಂತಸ

    # RMDIR
    rmdir ${primary_mnt}/${prefix}_d2
    rmdir ${primary_mnt}/${prefix}_d2_ಸಂತಸ

    # Rename - File
    mv ${primary_mnt}/${prefix}_f3 ${primary_mnt}/${prefix}_f4
    mv ${primary_mnt}/${prefix}_f3_ಸಂತಸ  ${primary_mnt}/${prefix}_f4_ಸಂತಸ

    # Rename - Dir
    mv ${primary_mnt}/${prefix}_d3 ${primary_mnt}/${prefix}_d4
    mv ${primary_mnt}/${prefix}_d3_ಸಂತಸ  ${primary_mnt}/${prefix}_d4_ಸಂತಸ

    # chown
    touch ${primary_mnt}/${prefix}_chown_f1
    chown 1000:1000 ${primary_mnt}/${prefix}_chown_f1
    touch ${primary_mnt}/${prefix}_chown_f1_ಸಂತಸ
    chown 1000:1000 ${primary_mnt}/${prefix}_chown_f1_ಸಂತಸ
}

function create_data_hang()
{
    prefix=$1
    mkdir ${primary_mnt}/${prefix}
    cd ${primary_mnt}/${prefix}
    # ~1k files is required with 1 sync-job and hang happens if
    # stderr buffer of tar/ssh executed with Popen is full (i.e., 64k).
    # 64k is hit when ~800 files were  not found while syncing data
    # from primary. So around 1k files is required to hit the condition.
    for i in {1..1000}
    do
        echo "test data" > file$i
        mv -f file$i file
    done
    cd -
}

function chown_file_ok()
{
    local file_owner=$(stat --format "%u:%g" "$1")
    if test "X$file_owner" != "X1000:1000"; then echo 1; else echo 0; fi
}

function regular_file_ok()
{
    local file_type=$(stat --format "%F" "$1")
    if test "X$file_type" != "Xregular file"; then echo 1; else echo 0; fi
}

function directory_ok()
{
    file_type=$(stat --format "%F" "$1")
    if test "X$file_type" != "Xdirectory"; then echo 1; else echo 0; fi
}

function unlink_ok()
{
    stat "$1" > /dev/null 2>&1
    rc=$?
    echo $rc
}

function hardlink_file_ok()
{
    orig_file=$1
    link_file=$2

    orig_inode=$(stat --format "%i" "$orig_file")
    rc=$?
    if test $rc != 0; then
        echo $rc
    else
        link_inode=$(stat --format "%i" "$link_file")
        rc=$?
        if test $rc != 0; then
            echo $rc
        else
            if test $orig_inode != $link_inode; then
                echo 1
            else
                echo 0
            fi
        fi
    fi
}

function data_ok()
{
    path=$1
    data1="$2"
    data2=$(cat $path)
    echo "data1:$data1"
    echo "data2:$data2"
    if test "X$data1" != "X$data2"; then
        echo 1
    else
        echo 0
    fi
}

function arequal_checksum()
{
    primary=$1
    secondary=$2
    ret=$(diff <(arequal-checksum -p $primary) <(arequal-checksum -p $secondary) | wc -l)
    echo x$ret
}

function symlink_ok()
{
    local orig_file_name=$1
    local symlink_file=$2

    local file_type=$(stat --format "%F" "$symlink_file")
    if test "X$file_type" != "Xsymbolic link"; then
        echo 1
    else
        local fname=$(readlink $symlink_file)
        if test "X$fname" != "X$orig_file_name"; then
            echo 2
        else
            echo 0
        fi
    fi

}

function rename_file_ok()
{
    old_name=$1
    new_name=$2

    if [ -f $old_name ]; then
        echo 1
    elif [ ! -f $new_name ]; then
        echo 2
    else
        echo 0
    fi
}

function rename_dir_ok()
{
    old_name=$1
    new_name=$2

    if [ -d $old_name ]; then
        echo 1
    elif [ ! -d $new_name ]; then
        echo 2
    else
        echo 0
    fi
}

function create_georep_session()
{
    $CLI system:: execute gsec_create
    rc=$?
    if test $rc != 0; then
        echo $rc
    else
        $CLI volume geo-rep $primary $secondary create push-pem
        rc=$?
        if test $rc != 0; then
            echo $rc
        else
            echo 0
        fi
    fi
}

# logrotate_simulate should be called (rotate_count + 1) times to cause
# an unlink and a gfid re-allocation.
# remember to keep the file name and rotate_count the same across the
# calls
function logrotate_simulate()
{
    file_name=$1
    declare -i rotate_count=$2

    while [ $rotate_count -ge 0 ]; do
        source_file="$file_name.$((rotate_count))"
        if [ $rotate_count -eq 0 ]; then
            source_file="$file_name"
        fi
        if [ -f "${source_file}" ]; then
                mv "${source_file}" "$file_name.$((rotate_count+1))"
        fi
        ((rotate_count--))
    done

    # logrotate causes gfid to be rellocated to a new file created
    # after an unlink and a blind rename later causes georep session
    # to go Faulty
    # this should not happen if source basename on secondary is tested
    # to be linked with its own gfid as on primary, before invoking
    # the rename syscall
    touch $file_name
    rotate_count=$2
    unlink_file_name="$file_name.$((rotate_count+1))"
    unlink $unlink_file_name 2>/dev/null
}

function create_rename()
{
    file_name=$1
    echo $file_name > $file_name
    mv $file_name $file_name.bak
}

function create_rename_ok()
{
    file_name=$1
    # after a log replay, we don't expect the original file
    # to be recreated i.e. a dangling entry without a corresponding
    # back-end gfid link should not exist on the secondary
    if [ -f "$file_name" ]; then
        echo 1
    else
        echo 0
    fi
}

function hardlink_rename()
{
    file_name=$1
    echo $file_name > $file_name
    ln $file_name $file_name.hl
    mv $file_name.hl $file_name.hl1
}

function hardlink_rename_ok()
{
    file_name=$1
    # the hardlink file should not exist on the secondary after renaming
    # to one of its links on changelog reprocessing
    if [ ! -f "$file_name" ]; then
        echo 1
    elif [ ! -f "$file_name.hl1" ]; then
        echo 2
    elif [ -f "$file_name.hl" ]; then
        echo 3
    else
        echo 0
    fi
}

function create_symlink_rename_mkdir_data()
{
    mkdir ${primary_mnt}/symlink_test1
    touch ${primary_mnt}/symlink_test1/file1
    ln -s "./file1" ${primary_mnt}/symlink_test1/sym_link
    mv ${primary_mnt}/symlink_test1/sym_link ${primary_mnt}/symlink_test1/rn_sym_link
    mkdir ${primary_mnt}/symlink_test1/sym_link
}
function verify_symlink_rename_mkdir_data()
{
    sym_dir=$1
    if [ ! -f $sym_dir/file1 ]; then
        echo 1
    elif [ ! -h $sym_dir/rn_sym_link ]; then
        echo 2
    elif [ ! -d $sym_dir/sym_link ]; then
        echo 3
    else
        echo 0
    fi
}

function create_rsnapshot_data()
{
    rm -rf /tmp/rsnapshot_symlinkbug
    mkdir /tmp/rsnapshot_symlinkbug
    ln -f -s /does/not/exist /tmp/rsnapshot_symlinkbug/a_symlink
    rsync -a /tmp/rsnapshot_symlinkbug ${primary_mnt}/
    cp -al ${primary_mnt}/rsnapshot_symlinkbug ${primary_mnt}/rsnapshot_symlinkbug.0
    ln -f -s /does/not/exist2 /tmp/rsnapshot_symlinkbug/a_symlink
    rsync -a /tmp/rsnapshot_symlinkbug ${primary_mnt}/
    cp -al ${primary_mnt}/rsnapshot_symlinkbug ${primary_mnt}/rsnapshot_symlinkbug.1
}

function verify_rsnapshot_data()
{
    dir="$1/rsnapshot_symlinkbug"
    dir0="$1/rsnapshot_symlinkbug.0"
    dir1="$1/rsnapshot_symlinkbug.1"
    if [ ! -d "$dir" ]; then
        echo 1
    elif [ ! -h $dir/a_symlink ]; then
        echo 2
    elif test "X$(readlink $dir/a_symlink)" != "X/does/not/exist2"; then
        echo 3
    elif [ ! -h $dir0/a_symlink ]; then
        echo 4
    elif test "X$(readlink $dir0/a_symlink)" != "X/does/not/exist"; then
        echo 5
    elif [ ! -h $dir1/a_symlink ]; then
        echo 6
    elif test "X$(readlink $dir1/a_symlink)" != "X/does/not/exist2"; then
        echo 7
    else
        echo 0
    fi
}

function create_hardlink_rename_data()
{
    dir=${primary_mnt}/hardlink_rename_issue
    mkdir $dir
    echo "test_data" > $dir/f1
    ln $dir/f1 $dir/f2
    mv $dir/f2 $dir/f3
    unlink $dir/f1
}

function verify_hardlink_rename_data()
{
    dir=$1/hardlink_rename_issue
    if [ ! -d $dir ]; then
        echo 1
    elif [ -f $dir/f1 ]; then
        echo 2
    elif [ -f $dir/f2 ]; then
        echo 3
    elif [ ! -f $dir/f3 ]; then
        echo 4
    elif test "Xtest_data" != "X$(cat $dir/f3)"; then
        echo 5
    else
        echo 0
    fi
}

function check_secondary_read_only()
{
    volum=$1
    gluster volume info $1 | grep 'features.read-only: on'
    echo $?
}

function create_rename_with_existing_destination()
{
    dir=$1/rename_with_existing_destination
    mkdir $dir
    for i in {1..5}
    do
        echo "Data_set$i" > $dir/data_set$i
        mv $dir/data_set$i $dir/data_set -f
    done
}

function verify_rename_with_existing_destination()
{
    dir=$1/rename_with_existing_destination

    if [ ! -d $dir ]; then
        echo 1
    elif [ ! -f $dir/data_set ]; then
        echo 2
    elif [ -f $dir/data_set1 ]; then
        echo 3
    elif [ -f $dir/data_set2 ]; then
        echo 4
    elif [ -f $dir/data_set3 ]; then
        echo 5
    elif [ -f $dir/data_set4 ]; then
        echo 6
    elif [ -f $dir/data_set5 ]; then
        echo 7
    elif test "XData_set5" != "X$(cat $dir/data_set)"; then
        echo 8
    else
        echo 0
    fi
}
