summaryrefslogtreecommitdiff
path: root/sioyek_db_gen.sh
blob: 05a7b69e3e80d77c3ca127d0d38f3f103db72c49 (plain)
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
#!/bin/bash


PDFs="$HOME/PDFs"
TTRPGs="$HOME/Downloads/TTRPGs"
SIOYEK="$HOME/.local/share/sioyek"
LOCALDB="$SIOYEK/local.db"
SHAREDDB="$SIOYEK/shared.db"

#backup just in case
[[ -f "$LOCALDB" ]] && cp "$LOCALDB" "$LOCALDB.backup"
[[ -f "$SHAREDDB" ]] && cp "$SHAREDDB" "$SHAREDDB.backup"

echo "Compiling PDF list..."
mapfile -t FILES < <( ( find $PDFs -type f -iregex "^.*\.pdf$" -printf "$PDFs/%P\n"; find $TTRPGs -type f -iregex "^.*\.pdf$" -printf "$TTRPGs/%P\n" ) | cat | sort --version-sort )

# save against collision errors
echo "Calculating file hashes..."

i=0
mapfile -t HASHES < <( for FILE in "${FILES[@]}"; do md5sum "$FILE" | cut -d' ' -f1 ; done )



echo "Creating database..."
sqlite3 "$LOCALDB" "drop table if exists document_hash;"
sqlite3 "$LOCALDB" "create table if not exists document_hash (id INTEGER PRIMARY KEY AUTOINCREMENT,path TEXT,hash TEXT,UNIQUE(path, hash));"

sqlite3 "$SHAREDDB" "create table if not exists bookmarks (id INTEGER PRIMARY KEY AUTOINCREMENT,document_path TEXT,desc TEXT,creation_time timestamp,modification_time timestamp,uuid TEXT,font_size integer DEFAULT -1,color_red real DEFAULT 0,color_green real DEFAULT 0,color_blue real DEFAULT 0,font_face TEXT,begin_x real DEFAULT -1,begin_y real DEFAULT -1,end_x real DEFAULT -1,end_y real DEFAULT -1,offset_y real);"
sqlite3 "$SHAREDDB" "create table if not exists highlights (id INTEGER PRIMARY KEY AUTOINCREMENT,document_path TEXT,desc TEXT,text_annot TEXT,type char,creation_time timestamp,modification_time timestamp,uuid TEXT,begin_x real,begin_y real,end_x real,end_y real);"
sqlite3 "$SHAREDDB" "create table if not exists links (id INTEGER PRIMARY KEY AUTOINCREMENT,creation_time timestamp,modification_time timestamp,uuid TEXT,src_document TEXT,dst_document TEXT,src_offset_y REAL,src_offset_x REAL,dst_offset_x REAL,dst_offset_y REAL,dst_zoom_level REAL);"
sqlite3 "$SHAREDDB" "create table if not exists marks (id INTEGER PRIMARY KEY AUTOINCREMENT,document_path TEXT,symbol CHAR,offset_x real,offset_y real,zoom_level real,creation_time timestamp,modification_time timestamp,uuid TEXT,UNIQUE(document_path, symbol));"

sqlite3 "$SHAREDDB" "drop table if exists opened_books;"
sqlite3 "$SHAREDDB" "create table opened_books (id INTEGER PRIMARY KEY AUTOINCREMENT,path TEXT UNIQUE,document_name TEXT,zoom_level REAL,offset_x REAL,offset_y REAL,last_access_time TEXT);"

i=0
for FILE in "${FILES[@]}"; do
        FILE=$( sed -r "s|'|''|g" <<< "$FILE" )
        DATE=$( shuf -n1 -i$(date -d '1987-01-01' '+%s')-$(date '+%s') | xargs -I{} date -d '@{}' '+%Y-%m-%d %H:%M:%S' )
        echo $FILE
        sqlite3 "$LOCALDB"  "insert into document_hash (path, hash) values ('$FILE', '${HASHES[i]}');"
        sqlite3 "$SHAREDDB"  "insert into opened_books (path, document_name, zoom_level, offset_x, offset_y, last_access_time) values ('${HASHES[i]}', '$FILE', 1.5, 0, 300, '$DATE');"
        ((i++))
done

echo "Done."