summaryrefslogtreecommitdiff
path: root/fish/conf.d/supersync.fish
blob: 0fdaef6a51794713e6c85afd2a1a5e5cb1e9cea3 (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
108
109
110
111
112
113
114
115
116
117
118
119
set -x SUPERSYNC_STAMP_FILE ~/.supersync.last

# Only sync the HabitsAndQuotes when it's asked for via function parameter
function supersync::worktime
    set -l worktime_dir ~/git/worktime

    if not test -d $worktime_dir
        echo "Warning: Directory $worktime_dir does not exist"
        return 1
    end
    cd $worktime_dir

    if test (count $argv) -gt 0 -a $argv[1] = sync_quotes
        if test -d ~/Notes/HabitsAndQuotes
            echo "" >work-wisdoms.md.tmp
            for notes in ~/Notes/random/{Productivity,Mentoring}.md
                grep '^\* ' $notes >>work-wisdoms.md.tmp
            end
            sort -u work-wisdoms.md.tmp >work-wisdoms.md
            rm work-wisdoms.md.tmp
            git add work-wisdoms.md
            grep '^\* ' ~/Notes/random/Exercise.md >exercises.md
            git add exercises.md
        end
    end

    find . -name '*.txt' -exec git add {} \;
    find . -name '*.json' -exec git add {} \;
    find . -name '*.csv' -exec git add {} \;
    git commit -a -m sync

    git pull origin master
    git push origin master

    cd -
end

function supersync::gitsyncer
    set enable_file ~/.gitsyncer_enable
    set now (date +%s)
    set weekly_interval (math 7 \* 24 \* 60 \* 60)

    if not test -f $enable_file
        # echo Gitsyncer is not enabled
        return
    end

    set last_run (cat $enable_file)
    if test (math $now - $last_run) -lt $weekly_interval
        return
    end

    if test -f ~/go/bin/gitsyncer
        ~/go/bin/gitsyncer sync bidirectional --auto-create-releases --create-repos --throttle && ~/go/bin/gitsyncer showcase
    end
    if test $status -eq 0
        echo $now >$enable_file
    end
end

function supersync::prompts
    # Since files might have been added and/or modified withoug being
    # committed to git yet.
    if test -d ~/git/dotfiles/prompts
        cd ~/git/dotfiles/prompts
        find . -type f -name \*.md | xargs git add
        find . -type f -name \*.md | xargs git commit -m 'update prompts'
        git push
        cd -
    else if test -d ~/git/helpers/prompts
        cd ~/git/helpers/prompts
        find . -type f -name \*.md | xargs git add
        find . -type f -name \*.md | xargs git commit -m 'update prompts'
        git push
        cd -
    end
end

function supersync
    if test -f ~/.supersync_disable
        echo Supersync is disabled
        return
    end

    supersync::worktime sync_quotes
    taskwarrior::invoke
    supersync::worktime no_sync_quotes
    supersync::prompts

    if test -f ~/.gos_enable
        if test -f ~/go/bin/gos
            # Go social media tool
            ~/go/bin/gos
        end
        if test -f ~/go/bin/snonux
            snonux::sync
        end
    end

    supersync::gitsyncer
    # update::tools

    date +%s >$SUPERSYNC_STAMP_FILE.tmp
    mv $SUPERSYNC_STAMP_FILE.tmp $SUPERSYNC_STAMP_FILE
end

function supersync::is_it_time_to_sync
    set -l max_age 86400
    set -l now (date +%s)
    if test -f $SUPERSYNC_STAMP_FILE
        set -l diff (math $now - (cat $SUPERSYNC_STAMP_FILE))
        if test $diff -lt $max_age
            return 0
        end
    end
    read -P "It's time to run supersync! Run it? (y/n) " answer; and test "$answer" = y; and supersync
end

abbr -a supersynct 'supersync; track'