#!/bin/sh
# The workspace's .pic folder: it ignores itself (a .gitignore holding "*", so git add -A in the
# workspace adds nothing under it; the project's own ignore files are never touched), says when a
# fossil or zit checkout with dotfiles on would list it anyway, and keeps the journal's files to
# the user (0600). Usage: test/pic/folder.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-folder.XXXXXX")
export HOME="$T/home" # pic keeps user state under HOME: never the real one
mkdir -p "$HOME"
pids=
cleanup() {
    for p in $pids; do kill "$p" 2>/dev/null || true; done
    rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() {
    printf 'FAIL: %s\n' "$*" >&2
    exit 1
}
mode() { stat -f %Lp "$1" 2>/dev/null || stat -c %a "$1"; }
run() { # WS: pic check in the workspace; stderr in $T/err
    "$PIC" --workspace "$1" check > "$T/out" 2> "$T/err" || { cat "$T/err" >&2; fail "pic check in $1"; }
}

# ---- the folder ignores itself
ws="$T/ws"
mkdir -p "$ws"
printf 'keep\n' > "$ws/.gitignore"
printf 'hello\n' > "$ws/a.txt"
run "$ws"
[ "$(cat "$ws/.pic/.gitignore")" = '*' ] || fail ".pic/.gitignore is not '*'"
[ "$(cat "$ws/.gitignore")" = keep ] || fail "the project's .gitignore was touched"
[ ! -s "$T/err" ] || fail "a plain workspace warned: $(cat "$T/err")"
if command -v git > /dev/null; then
    (cd "$ws" && git init -q . && git add -A && git ls-files > "$T/tracked")
    grep -q '^\.pic' "$T/tracked" && fail "git add -A added $(grep '^\.pic' "$T/tracked")"
    grep -q '^a.txt$' "$T/tracked" || fail "git add -A did not add the project's own file"
fi
# a file the user wrote there is theirs; one that is missing comes back
printf 'mine\n' > "$ws/.pic/.gitignore"
run "$ws"
[ "$(cat "$ws/.pic/.gitignore")" = mine ] || fail "an existing .pic/.gitignore was rewritten"
rm "$ws/.pic/.gitignore"
run "$ws"
[ "$(cat "$ws/.pic/.gitignore")" = '*' ] || fail "a missing .pic/.gitignore was not written again"

# ---- the journal's files are the user's alone
db="$ws/.pic/journal.sqlite"
[ "$(mode "$db")" = 600 ] || fail "a new journal.sqlite is $(mode "$db"), want 600"
# one an earlier pic made (here a 0644 file with a conversation) is tightened when it is opened
ws="$T/old"
db="$ws/.pic/journal.sqlite"
mkdir -p "$ws/.pic"
sqlite3 "$db" < "$(dirname "$0")/journal-v3.sql"
[ "$(mode "$db")" = 644 ] || fail "the fixture is $(mode "$db"), want 644"
run "$ws"
chmod 644 "$db"
"$PIC" --workspace "$ws" attach --format json < /dev/null > "$T/attach.out" 2> "$T/attach.err" &
pids="$pids $!"
i=0
until ls "$db.pic-holders" 2>/dev/null | grep -q "^$!\."; do
    i=$((i + 1))
    [ "$i" -lt 300 ] || fail "the follower did not open the journal"
    sleep 0.1
done
[ "$(mode "$db")" = 600 ] || fail "an existing journal.sqlite stayed $(mode "$db")"
for f in "$db-wal" "$db-shm" "$db.pic-lock" "$db.pic-holders"/*; do
    [ -e "$f" ] || continue
    m=$(mode "$f")
    case "$m" in *00) ;; *) fail "$f is $m, want no access for group and others" ;; esac
done
[ -e "$db-wal" ] && [ -e "$db-shm" ] || fail "no -wal and -shm to check while the journal is open"
# the files of a journal made before this: tightened when it is opened
kill "$!" 2>/dev/null || true
wait "$!" 2>/dev/null || true
chmod 644 "$db" "$db-wal" "$db-shm" 2>/dev/null || true
run "$ws"
for f in "$db" "$db-wal" "$db-shm"; do
    [ -e "$f" ] || continue
    [ "$(mode "$f")" = 600 ] || fail "$f stayed $(mode "$f")"
done

# ---- a zit checkout with dotfiles on
checkout() { # DIR: a zit checkout with dotfiles on, by its versioned setting
    mkdir -p "$1/.zit-settings"
    : > "$1/.zit-checkout"
    printf 'on\n' > "$1/.zit-settings/dotfiles"
}
want='the line .pic/* to .zit-settings/ignore-glob'
z="$T/z"
checkout "$z"
run "$z"
grep -q "zit checkout with the dotfiles setting on" "$T/err" || fail "no dotfiles warning: $(cat "$T/err")"
grep -qF "$want" "$T/err" || fail "the warning does not give the line to add: $(cat "$T/err")"
[ -f "$z/.pic/.gitignore" ] || fail "the zit workspace's .pic/.gitignore is missing"
run "$z"
[ ! -s "$T/err" ] || fail "the warning came twice: $(cat "$T/err")"
"$PIC" --workspace "$z" doctor > "$T/out" 2> "$T/err" || fail "pic doctor in the zit checkout"
grep -qF "$want" "$T/out" || fail "pic doctor does not warn: $(cat "$T/out")"
"$PIC" --workspace "$z" doctor --json > "$T/out" 2> "$T/err" || fail "pic doctor --json"
grep -qF '"message":"' "$T/out" && grep -qF "$want" "$T/out" || fail "pic doctor --json does not warn: $(cat "$T/out")"

# covered by an ignore-glob (or off): no warning
z2="$T/z2"
checkout "$z2"
printf '.pic/*\n' > "$z2/.zit-settings/ignore-glob"
run "$z2"
[ ! -s "$T/err" ] || fail "a covered .pic warned: $(cat "$T/err")"
z3="$T/z3"
checkout "$z3"
printf 'off\n' > "$z3/.zit-settings/dotfiles"
run "$z3"
[ ! -s "$T/err" ] || fail "dotfiles off warned: $(cat "$T/err")"
"$PIC" --workspace "$z3" doctor > "$T/out" 2>&1 || fail "pic doctor, dotfiles off"
grep -q 'dotfiles' "$T/out" && fail "pic doctor warned with dotfiles off"

# a setting that lives outside the tree is asked of the tool, run in the checkout
z4="$T/z4"
mkdir -p "$T/bin" "$z4"
: > "$z4/.zit-checkout"
cat > "$T/bin/zit" << 'SH'
#!/bin/sh
[ "$1 $2 $3" = "settings --value dotfiles" ] && { echo on; exit 0; }
[ "$1 $2 $3" = "settings --value ignore-glob" ] && { echo "build/*"; exit 0; }
exit 1
SH
chmod +x "$T/bin/zit"
(PATH="$T/bin:$PATH"; run "$z4")
grep -qF "$want" "$T/err" || fail "the tool's dotfiles setting was not used: $(cat "$T/err")"
# a workspace below the root names its own path
mkdir -p "$T/z5/sub"
checkout "$T/z5"
run "$T/z5/sub"
grep -qF 'the line sub/.pic/* to' "$T/err" || fail "a subfolder workspace: $(cat "$T/err")"
printf 'ok\n'
