~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.local/bin/dmenu/dsnip-seed -rwxr-xr-x 1.8 KiB
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
###############################################################################
# dsnip-seed — Import legacy karbs/snippets file into bookmarks.db
#
# Run once after creating the db. Safe to re-run (UNIQUE on url skips dupes).
###############################################################################

DB="${XDG_DATA_HOME:-$HOME/.local/share}/karbs/bookmarks.db"
SNIPPETS="${XDG_DATA_HOME:-$HOME/.local/share}/karbs/snippets"

[ -f "$SNIPPETS" ] || { echo "snippets file not found: $SNIPPETS"; exit 1; }

category="Uncategorized"
inserted=0
skipped=0

while IFS= read -r line; do
    # Strip trailing whitespace
    line=$(printf '%s' "$line" | sed 's/[[:space:]]*$//')

    # Skip blank lines
    [ -z "$line" ] && continue

    # Category header (must check before stripping inline comments)
    if [[ "$line" == \#* ]] && [[ "$line" != http* ]]; then
        category=$(printf '%s' "$line" | sed 's/^# *//')
        continue
    fi

    # Strip inline comments (e.g. "https://... # Profile")
    line=$(printf '%s' "$line" | sed 's/ *#[^/].*//')

    # Skip non-URL lines
    [[ "$line" != http* ]] && continue

    url=$(printf '%s' "$line" | sed 's/[[:space:]]*$//')
    # Derive a display name: strip scheme + www, truncate at 60 chars
    name=$(printf '%s' "$url" | sed 's|https\?://||; s|^www\.||; s|/$||')
    name="${name:0:60}"

    # Escape single quotes
    safe_url="${url//\'/\'\'}"
    safe_name="${name//\'/\'\'}"
    safe_cat="${category//\'/\'\'}"

    result=$(sqlite3 "$DB" "
        INSERT OR IGNORE INTO bookmarks (name, url, category)
        VALUES ('${safe_name}', '${safe_url}', '${safe_cat}');
        SELECT changes();
    ")

    if [ "$result" -eq 1 ]; then
        ((inserted++))
    else
        ((skipped++))
    fi
done < "$SNIPPETS"

echo "Done. Inserted: $inserted  Skipped (already exist): $skipped"