summaryrefslogtreecommitdiffstats
path: root/bin/convert-ublock-settings
blob: 9fa48b3edbf0888e62a452d90cb04b9ccc8a15f1 (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
#!/bin/sh

set -e
set -u

display_help_and_exit () {
   echo "Usage: $(basename "$0") INPUT_FILE" >&2
}

[ $# -eq 1 ] || display_help_and_exit

INPUT_FILE="$1"

[ -f "$INPUT_FILE" ] || exit 2

# For posterity: the general idea is to introduce \r\n as a token
# where we have made a line break to make the dump more diff-friendly
# (and, hence, Git-friendly). The most complex expression is the one
# done with perl, where we employ negative lookahead. What it means,
# is: replace single occurrences of | except when followed by \\n.

echo '.dump' \
    | sqlite3 "$INPUT_FILE" | \
    grep -v "cached_asset_content://cache://compiled-" | \
    awk '!/^INSERT/; /^INSERT/ {print $0 | "sort -n"}' | \
    sed 's_\\n_\\n\r\n_g' | \
    sed 's_,_,\r\n_g' | \
    perl -p -e 's/([^|])\|((?!\||\\n).)/\1\|\r\n\2/g' | \
    sed "/^INSERT INTO \"settings\" VALUES('\(remoteBlacklists\|cached_asset_entries\)'/"'s_,_,\r\n_g'