#!/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"