#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later

set -euo pipefail

. "$(dirname "$0")/lib.sh"

setup "$1"
plan 24

# copy files
mkdir -p "$TESTDIR/copy-src"
echo "copy-data" > "$TESTDIR/copy-src/data.txt"
write_conf "C $TESTDIR/copy-dst - - - - $TESTDIR/copy-src"
run_test "copy files command" "$SEEDFILES" --create "$TESTDIR/conf"
assert_exists "$TESTDIR/copy-dst/data.txt"
assert_file_equals "$TESTDIR/copy-dst/data.txt" "copy-data"

# copy preserves mtime
REF_TS="199807060102.03"
REF_EPOCH=$(date -d "1998-07-06 01:02:03" +%s)

mkdir -p "$TESTDIR/copy-mtime-src/sub"
echo "top" > "$TESTDIR/copy-mtime-src/top.txt"
echo "nested" > "$TESTDIR/copy-mtime-src/sub/nested.txt"
touch -m -t "$REF_TS" "$TESTDIR/copy-mtime-src/top.txt"
touch -m -t "$REF_TS" "$TESTDIR/copy-mtime-src/sub/nested.txt"

write_conf "C $TESTDIR/copy-mtime-dst - - - - $TESTDIR/copy-mtime-src"
run_test "copy mtree command" "$SEEDFILES" --create "$TESTDIR/conf"
assert_exists "$TESTDIR/copy-mtime-dst/top.txt"
assert_exists "$TESTDIR/copy-mtime-dst/sub/nested.txt"

got=$(stat -c '%Y' "$TESTDIR/copy-mtime-dst/top.txt")
if [ "$got" = "$REF_EPOCH" ]; then ok "top.txt mtime preserved"; else not_ok "top.txt mtime preserved" "got $got, expected $REF_EPOCH"; fi

got=$(stat -c '%Y' "$TESTDIR/copy-mtime-dst/sub/nested.txt")
if [ "$got" = "$REF_EPOCH" ]; then ok "nested.txt mtime preserved"; else not_ok "nested.txt mtime preserved" "got $got, expected $REF_EPOCH"; fi

# single file copy mtime
touch "$TESTDIR/copy-mtime-src2"
touch -m -t "$REF_TS" "$TESTDIR/copy-mtime-src2"
write_conf "C $TESTDIR/copy-mtime-dst2 - - - - $TESTDIR/copy-mtime-src2"
run_test "copy single file mtime command" "$SEEDFILES" --create "$TESTDIR/conf"
assert_exists "$TESTDIR/copy-mtime-dst2"
got=$(stat -c '%Y' "$TESTDIR/copy-mtime-dst2")
if [ "$got" = "$REF_EPOCH" ]; then ok "single file mtime preserved"; else not_ok "single file mtime preserved" "got $got, expected $REF_EPOCH"; fi

# copy preserves permissions
mkdir -p "$TESTDIR/copy-perms-src/sub"
echo "top" > "$TESTDIR/copy-perms-src/top.txt"
echo "nested" > "$TESTDIR/copy-perms-src/sub/nested.txt"
chmod 0750 "$TESTDIR/copy-perms-src"
chmod 0640 "$TESTDIR/copy-perms-src/top.txt"
chmod 0700 "$TESTDIR/copy-perms-src/sub"
chmod 0600 "$TESTDIR/copy-perms-src/sub/nested.txt"
SRC_DIR_OWNER=$(stat -c '%u:%g' "$TESTDIR/copy-perms-src")
SRC_TOP_OWNER=$(stat -c '%u:%g' "$TESTDIR/copy-perms-src/top.txt")
SRC_SUB_OWNER=$(stat -c '%u:%g' "$TESTDIR/copy-perms-src/sub")
SRC_NESTED_OWNER=$(stat -c '%u:%g' "$TESTDIR/copy-perms-src/sub/nested.txt")

write_conf "C $TESTDIR/copy-perms-dst - - - - $TESTDIR/copy-perms-src"
run_test "copy perms command" "$SEEDFILES" --create "$TESTDIR/conf"

assert_exists "$TESTDIR/copy-perms-dst"
got=$(stat -c '%a' "$TESTDIR/copy-perms-dst")
if [ "$got" = "750" ]; then ok "dst dir mode"; else not_ok "dst dir mode" "got $got, expected 750"; fi
got=$(stat -c '%u:%g' "$TESTDIR/copy-perms-dst")
if [ "$got" = "$SRC_DIR_OWNER" ]; then ok "dst dir owner"; else not_ok "dst dir owner" "got $got, expected $SRC_DIR_OWNER"; fi

assert_exists "$TESTDIR/copy-perms-dst/top.txt"
got=$(stat -c '%a' "$TESTDIR/copy-perms-dst/top.txt")
if [ "$got" = "640" ]; then ok "top.txt mode"; else not_ok "top.txt mode" "got $got, expected 640"; fi
got=$(stat -c '%u:%g' "$TESTDIR/copy-perms-dst/top.txt")
if [ "$got" = "$SRC_TOP_OWNER" ]; then ok "top.txt owner"; else not_ok "top.txt owner" "got $got, expected $SRC_TOP_OWNER"; fi

assert_exists "$TESTDIR/copy-perms-dst/sub"
got=$(stat -c '%a' "$TESTDIR/copy-perms-dst/sub")
if [ "$got" = "700" ]; then ok "sub dir mode"; else not_ok "sub dir mode" "got $got, expected 700"; fi
got=$(stat -c '%u:%g' "$TESTDIR/copy-perms-dst/sub")
if [ "$got" = "$SRC_SUB_OWNER" ]; then ok "sub dir owner"; else not_ok "sub dir owner" "got $got, expected $SRC_SUB_OWNER"; fi

assert_exists "$TESTDIR/copy-perms-dst/sub/nested.txt"
got=$(stat -c '%a' "$TESTDIR/copy-perms-dst/sub/nested.txt")
if [ "$got" = "600" ]; then ok "nested.txt mode"; else not_ok "nested.txt mode" "got $got, expected 600"; fi
got=$(stat -c '%u:%g' "$TESTDIR/copy-perms-dst/sub/nested.txt")
if [ "$got" = "$SRC_NESTED_OWNER" ]; then ok "nested.txt owner"; else not_ok "nested.txt owner" "got $got, expected $SRC_NESTED_OWNER"; fi

done_testing
