#!/bin/sh
# The workspace's .pic folder: it ignores itself (a .gitignore holding "*", so git add -A in the
# workspace adds nothing under it), adds the line to the own ignore file of a zit or fossil checkout
# above it (once, keeping every other line), 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

# ---- fossil and zit checkouts: each VCS's own ignore file gets the line, once
have() { command -v "$1" > /dev/null; }
extras_under_pic() { # TOOL DIR: what TOOL extras --dotfiles lists under .pic (nothing wanted)
    (cd "$2" && "$1" extras --dotfiles 2> /dev/null) | grep '\.pic' || true
}
# zit: a .pic/ line in the root .zitignore, created when absent
z="$T/z"
mkdir -p "$z"
: > "$z/.zit-checkout"
run "$z"
[ "$(cat "$z/.zitignore")" = '.pic/' ] || fail ".zitignore is not '.pic/': $(cat "$z/.zitignore" 2>&1)"
grep -qF "added .pic/ to .zitignore" "$T/err" || fail "no plain line about .zitignore: $(cat "$T/err")"
[ "$(wc -l < "$T/err" | tr -d ' ')" = 1 ] || fail "more than one line said: $(cat "$T/err")"
run "$z"
[ ! -s "$T/err" ] || fail "said again: $(cat "$T/err")"
[ "$(grep -c '^\.pic/$' "$z/.zitignore")" = 1 ] || fail "the line was added twice"
# other lines are kept, in order, and a missing final newline is not an excuse to join lines
z2="$T/z2"
mkdir -p "$z2"
: > "$z2/.zit-checkout"
printf 'build/\n*.o' > "$z2/.zitignore"
run "$z2"
[ "$(cat "$z2/.zitignore")" = 'build/
*.o
.pic/' ] || fail "an existing .zitignore was not kept: $(cat "$z2/.zitignore")"
# a line that already covers it is enough
z3="$T/z3"
mkdir -p "$z3"
: > "$z3/.zit-checkout"
printf '.pic\n' > "$z3/.zitignore"
run "$z3"
[ "$(cat "$z3/.zitignore")" = '.pic' ] && [ ! -s "$T/err" ] || fail "a covered .pic was changed or said"
# a workspace below the root: the path from the root
mkdir -p "$T/z5/sub"
: > "$T/z5/.zit-checkout"
run "$T/z5/sub"
[ "$(cat "$T/z5/.zitignore")" = 'sub/.pic/' ] || fail "a subfolder workspace: $(cat "$T/z5/.zitignore")"
[ ! -e "$T/z5/sub/.zitignore" ] || fail "an ignore file was written below the root"
# no zit checkout above: nothing written
run "$T/ws"
[ ! -e "$T/ws/.zitignore" ] && [ ! -e "$T/ws/.fossil-settings" ] || fail "an ignore file was written with no checkout"
# a file that cannot be written: the warning, with the exact line
z6="$T/z6"
mkdir -p "$z6"
: > "$z6/.zit-checkout"
mkdir "$z6/.zitignore"
run "$z6"
grep -qF "add the line .pic/ to .zitignore" "$T/err" || fail "no fallback warning: $(cat "$T/err")"
# doctor reports each VCS present
"$PIC" --workspace "$z" doctor > "$T/out" 2> "$T/err" || fail "pic doctor in the zit checkout"
grep -qF "zit: .pic/ is covered" "$T/out" || fail "doctor, zit covered: $(cat "$T/out")"
# (doctor opens the journal like any command, so a line it can add is added; one it cannot, is reported)
"$PIC" --workspace "$z6" doctor > "$T/out" 2> "$T/err" || fail "pic doctor, uncovered zit"
grep -qF "zit: .pic/ is not covered; add the line .pic/ to .zitignore" "$T/out" || fail "doctor, zit not covered: $(cat "$T/out")"
"$PIC" --workspace "$z6" doctor --json > "$T/out" 2> "$T/err" || fail "pic doctor --json"
grep -qF '"message":"' "$T/out" && grep -qF "zit: .pic/ is not covered" "$T/out" || fail "doctor --json: $(cat "$T/out")"
# fossil: .pic/* in .fossil-settings/ignore-glob
f="$T/f"
mkdir -p "$f"
: > "$f/.fslckout"
run "$f"
[ "$(cat "$f/.fossil-settings/ignore-glob")" = '.pic/*' ] || fail "ignore-glob: $(cat "$f/.fossil-settings/ignore-glob" 2>&1)"
grep -qF "added .pic/* to .fossil-settings/ignore-glob" "$T/err" || fail "fossil said: $(cat "$T/err")"
run "$f"
[ ! -s "$T/err" ] && [ "$(wc -l < "$f/.fossil-settings/ignore-glob" | tr -d ' ')" = 1 ] || fail "fossil: added or said twice"
# newline-separated and comma-separated lists keep their shape
f2="$T/f2"
mkdir -p "$f2/.fossil-settings"
: > "$f2/.fslckout"
printf 'build/*\n*.o\n' > "$f2/.fossil-settings/ignore-glob"
run "$f2"
[ "$(cat "$f2/.fossil-settings/ignore-glob")" = 'build/*
*.o
.pic/*' ] || fail "newline list: $(cat "$f2/.fossil-settings/ignore-glob")"
f3="$T/f3"
mkdir -p "$f3/.fossil-settings"
: > "$f3/.fslckout"
printf 'build/*,*.o\n' > "$f3/.fossil-settings/ignore-glob"
run "$f3"
[ "$(cat "$f3/.fossil-settings/ignore-glob")" = 'build/*,*.o,.pic/*' ] || fail "comma list: $(cat "$f3/.fossil-settings/ignore-glob")"
mkdir -p "$T/f5/sub"
: > "$T/f5/.fslckout"
run "$T/f5/sub"
[ "$(cat "$T/f5/.fossil-settings/ignore-glob")" = 'sub/.pic/*' ] || fail "fossil subfolder: $(cat "$T/f5/.fossil-settings/ignore-glob")"
"$PIC" --workspace "$f" doctor > "$T/out" 2> "$T/err" || fail "pic doctor in the fossil checkout"
grep -qF "fossil: .pic/ is covered" "$T/out" || fail "doctor, fossil: $(cat "$T/out")"
# a setting the repository holds outside the tree is not overridden by a new versioned file
f6="$T/f6"
mkdir -p "$T/bin" "$f6"
: > "$f6/.fslckout"
cat > "$T/bin/fossil" << 'SH'
#!/bin/sh
[ "$1 $2" = "settings ignore-glob" ] && { echo "ignore-glob       (local)  *.o"; exit 0; }
exit 1
SH
chmod +x "$T/bin/fossil"
(PATH="$T/bin:$PATH"; run "$f6")
[ ! -e "$f6/.fossil-settings" ] || fail "a new versioned ignore-glob would override the repository's setting"
grep -qF "add the line .pic/* to .fossil-settings/ignore-glob" "$T/err" || fail "fossil fallback: $(cat "$T/err")"
# git: its own .gitignore, reported by doctor
g="$T/g"
mkdir -p "$g/.git"
run "$g"
"$PIC" --workspace "$g" doctor > "$T/out" 2> "$T/err" || fail "pic doctor in a git checkout"
grep -qF "git: .pic/ is covered" "$T/out" || fail "doctor, git: $(cat "$T/out")"

# the real tools, when they are installed: nothing under .pic is listed even with dotfiles on
if have zit; then
    zr="$T/zreal"
    mkdir -p "$zr"
    zit init "$zr" > /dev/null 2>&1 || true
    if [ -e "$zr/.zit-checkout" ]; then
        (cd "$zr" && zit settings dotfiles on > /dev/null 2>&1) || true
        printf 'x\n' > "$zr/a.txt"
        run "$zr"
        [ -z "$(extras_under_pic zit "$zr")" ] || fail "zit extras lists .pic: $(extras_under_pic zit "$zr")"
        (cd "$zr" && zit extras --dotfiles 2> /dev/null | grep -q 'a.txt') || fail "zit extras does not list the project's own file"
        mkdir -p "$zr/sub"
        run "$zr/sub"
        [ -z "$(extras_under_pic zit "$zr")" ] || fail "zit extras lists sub/.pic"
    else
        printf 'note: no scratch zit checkout could be made; zit skipped\n'
    fi
else
    printf 'note: zit is not on PATH; its extras check is skipped\n'
fi
if have fossil; then
    fr="$T/freal"
    mkdir -p "$fr"
    (cd "$fr" && fossil init "$T/freal.fossil" > /dev/null 2>&1 && fossil open "$T/freal.fossil" > /dev/null 2>&1) || true
    if [ -e "$fr/.fslckout" ] || [ -e "$fr/_FOSSIL_" ]; then
        (cd "$fr" && fossil settings dotfiles on > /dev/null 2>&1) || true
        printf 'x\n' > "$fr/a.txt"
        run "$fr"
        [ -z "$(extras_under_pic fossil "$fr")" ] || fail "fossil extras lists .pic: $(extras_under_pic fossil "$fr")"
        (cd "$fr" && fossil extras --dotfiles 2> /dev/null | grep -q 'a.txt') || fail "fossil extras does not list the project's own file"
        mkdir -p "$fr/sub"
        run "$fr/sub"
        [ -z "$(extras_under_pic fossil "$fr")" ] || fail "fossil extras lists sub/.pic"
    else
        printf 'note: no scratch fossil checkout could be made; fossil skipped\n'
    fi
else
    printf 'note: fossil is not on PATH; its extras check is skipped\n'
fi
printf 'ok\n'
