summaryrefslogtreecommitdiff
path: root/firefox-sideloader/functions.sh
blob: 7b144df3a846da795d2ba9e84b7bf85e53ae45a3 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh

profiles_ini=~/.mozilla/firefox/profiles.ini

get_default_firefox_profile_basename()
{
    < "$profiles_ini" \
    perl -ne 'BEGIN { $/ = "" }
              /^Default=1$/m || next;
              /^Path=(.+)$/m || die;
              print "$1\n";
              $printed = 1;
              last;

              END { die unless $printed }'
}

get_default_firefox_profile_dir()
{
    local prof
    prof=$(get_default_firefox_profile_basename) || return
    printf '%s/%s\n' "${profiles_ini%/*}" "$prof"
}

get_basename_from_profile_name()
{
    < "$profiles_ini" \
      ARG="$1" \
      perl -ne 'BEGIN { $/ = "" }
                /^Name=(.+)$/m || next;
                $1 eq $ENV{ARG} || next;
                /^Path=(.+)$/m || next;
                print "$1";
                $printed = 1;
                last;

                END { die unless $printed }'
}
get_profile_dir_from_profile_name()
{
    local prof
    prof=$(get_basename_from_profile_name "$1") || return
    printf '%s/%s\n' "${profiles_ini%/*}" "$prof"
}

get_profile_dir()
{
    if [ "$1" ]
    then get_profile_dir_from_profile_name "$1"
    else get_default_firefox_profile_dir
    fi
}

illustrate_leak()
{
    local prof
    which jq >/dev/null || sudo apt-get install jq
    prof=$(get_default_firefox_profile_dir) && jq . < "$prof"/extensions.json | grep "$HOME"
}

copy_extensions_json()
{
    local new="$1" old="$2" f='extensions.json' filter_json
    filter_json="s/${old//\//\\/}/${new//\//\\/}/g"
    [ -d "$old" -a -d "$new" ] || return
    sed -e "$filter_json" > "$new"/"$f" < "$old"/"$f"
}

clone_profile_raw()
{
    local old_profile_dir="$1" new_profile_name="$2"
    [ -d "$old_profile_dir" ] || return
    [ -d "$old_profile_dir"/extensions ] || return

    firefox -createProfile "$new_profile_name" || return
    new_profile_dir=$(get_profile_dir "$new_profile_name") || return
    [ "$new_profile_dir" ] || return
    [ "$new_profile_dir" != "$old_profile_dir" ] || return

    cp -r --preserve=mode,timestamps -t "$new_profile_dir" -- "$old_profile_dir"/extensions/ || return

    copy_extensions_json "$new_profile_dir" "$old_profile_dir"
}

clone_profile()
{
    local old_profile_name="$1" old_profile_dir
    shift
    old_profile_dir=$(get_profile_dir "$old_profile_name") || return
    clone_profile_raw "$old_profile_dir" "$@"
}

clone_default_profile()
{
    new_name=$1
    [ "$new_name" ] || return
    old_dir=$(get_default_firefox_profile_dir) || return
    [ "$old_dir" ] || return
    clone_profile_raw "$old_dir" "$new_name"
}

runtest()
{
    . functions.sh
    clone_default_profile "test.$(date -I)"
    echo r=$?
}