#!/bin/sh
# Initialize the history database (encrypted with SQLCipher)
DB_DIR="$HOME/.local/share/history"
DB_PATH="$DB_DIR/history.db"
KEY_FILE="$DB_DIR/.key"
mkdir -p "$DB_DIR"
# Generate encryption key if it doesn't exist
if [ ! -f "$KEY_FILE" ]; then
head -c 32 /dev/urandom | base64 > "$KEY_FILE"
chmod 600 "$KEY_FILE"
echo "Generated new encryption key at $KEY_FILE"
fi
KEY=$(cat "$KEY_FILE")
sqlcipher "$DB_PATH" <<EOF
PRAGMA key = '$KEY';
-- Terminal command history
CREATE TABLE IF NOT EXISTS terminal_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
command TEXT NOT NULL,
exit_code INTEGER,
pwd TEXT,
shell TEXT DEFAULT 'fish'
);
-- Clipboard history
CREATE TABLE IF NOT EXISTS clipboard_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
content TEXT NOT NULL,
content_type TEXT DEFAULT 'text',
hash TEXT UNIQUE
);
-- Browser search history
CREATE TABLE IF NOT EXISTS search_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
query TEXT NOT NULL,
browser TEXT NOT NULL,
url TEXT,
hash TEXT UNIQUE
);
-- Indexes for fast queries
CREATE INDEX IF NOT EXISTS idx_terminal_timestamp ON terminal_history(timestamp);
CREATE INDEX IF NOT EXISTS idx_terminal_command ON terminal_history(command);
CREATE INDEX IF NOT EXISTS idx_clipboard_timestamp ON clipboard_history(timestamp);
CREATE INDEX IF NOT EXISTS idx_search_timestamp ON search_history(timestamp);
CREATE INDEX IF NOT EXISTS idx_search_browser ON search_history(browser);
CREATE INDEX IF NOT EXISTS idx_search_query ON search_history(query);
EOF
chmod 600 "$DB_PATH"
echo "Encrypted database initialized at $DB_PATH"
echo "Key stored at $KEY_FILE (keep this safe!)"