summaryrefslogtreecommitdiff
path: root/install.sh
blob: e51ce0a845729e81242752cbede2b9cc8cd8f2e7 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash

home=/home/urbain
dir=$home/dotfiles
olddir=$home/dotfiles_old

declare -A repodirs
repodirs[Anthony25/gnome-terminal-colors-solarized]=$home/.solarized/gnome-terminal-colors-solarized
repodirs[alols/xcape]=$home/xcape
repodirs[altercation/mutt-colors-solarized]=$home/.solarized/mutt-colors-solarized
repodirs[gmarik/vundle]=$dir/bundle/vundle
repodirs[icholy/ttygif]=$home/ttygif
repodirs[junegunn/fzf]=$home/.fzf
repodirs[junegunn/vim-plug]=$dir/nvim/vim-plug
repodirs[seebi/dircolors-solarized]=$home/.solarized/dircolors-solarized
repodirs[tmux-plugins/tpm]=$home/.tmux/plugins/tpm
repodirs[uvaes/fuzzy-zsh-marks]=$home/github/fuzzy-zsh-marks

function after_vimplug {
    cd ..
    rm -rf autoload
    mkdir -p autoload
    cd autoload
    ln -s ../vim-plug/plug.vim;
}

declare -A actions
actions[Anthony25/gnome-terminal-colors-solarized]=''
actions[alols/xcape]='make'
actions[altercation/mutt-colors-solarized]=''
actions[gmarik/vundle]=''
actions[icholy/ttygif]='make'
actions[junegunn/fzf]='./install'
actions[junegunn/vim-plug]='after_vimplug'
actions[seebi/dircolors-solarized]=''
actions[tmux-plugins/tpm]=''
actions[uvaes/fuzzy-zsh-marks]=''

function fetch_repo {
    cd $1
    echo "Fetching origin of git repository stored in $1 ..."
    git fetch -q origin master
}

function clone_repo {
    githubDir=https://github.com/$1
    echo "Cloning $repo in $2"
    git clone -q $githubDir $2
    cd $2
}

function waitjobs {
    for job in `jobs -p`
    do
        wait $job
    done
}

function install_repos {
    echo -e "\n*** \e[1mInstalling git repositories\e[0m ***"

    for repo in "${!repodirs[@]}"; do

        repodir=${repodirs[$repo]}
        action=${actions[$repo]}

        if [ ! -d $repodir ]; then
            clone_repo $repo $repodir $action &
        else
            echo -e "\e[0mRepository $repo already installed in ${repodir}.\e[0m"
        fi
    done
    waitjobs
    echo "--> Done!"; echo

    echo -e "*** \e[1mExectutiong actions after installation\e[0m ***"
    for repo in "${!repodirs[@]}"; do
        cd ${repodirs[$repo]}
        eval ${actions[$repo]}
    done
}

function update_repos {
    echo -e "\n*** \e[1mUpdating git repositories\e[0m ***"

    for repo in "${!repodirs[@]}"; do

        repodir=${repodirs[$repo]}

        if [ -d $repodir ]; then
            fetch_repo $repodir &
        fi
    done

    for job in `jobs -p`
    do
        wait $job
    done

    echo "--> Done!"; echo

    for repo in "${!repodirs[@]}"; do

        repodir=${repodirs[$repo]}

        if [ -d $repodir ]; then
            cd $repodir
            echo "Merging upstream updates of git repository stored in ${repo}..."
            cat <(git log --reverse --pretty=format:"-- %h %s (%cr)" -4); echo -e "\e[36m"
            output=$(git log HEAD..origin)
            if [[ ! -z $output ]]; then
                cat <(git log --reverse --pretty=format:"-- %h %s (%cr)" HEAD..origin); echo -e "\e[0m"
            else
                echo -e "-- No updates since last pull\e[0m"
            fi
            git merge -q origin/master
            echo
        fi
    done
}

function install_dotfiles {
    echo -e "\n*** \e[1mInstalling dotfiles\e[0m ***"

    rm -rf $olddir
    mkdir -p $olddir

    listFiles=`ls --ignore="make" --ignore="README.md"`

    cd $dir
    for file in $listFiles; do
        if [ -e ~/.$file ]; then
            mv ~/.$file $olddir
            printf '~/.%-15s exists. ' $file
            echo -n "Moving it to $olddir. "
        fi
        ln -s $dir/$file ~/.$file
        echo "(Re)-creating symbolic link of $file."
    done

    cd $dir
    mkdir -p nvim/vimundo
    mkdir -p mutt/temp
    mkdir -p mutt/cache
    mkdir -p mutt/cache/bodies
}

function clean {
    echo -e "\n*** \e[1mCleaning repositories\e[0m ***"
    for repo in "${!repodirs[@]}"; do
        rm -rfv ${repodirs[$repo]}
    done

    echo -e "\n*** \e[1mCleaning dotfiles\e[0m ***"
    for file in `ls`; do
        rm -rfv  ~/.$file
    done
}

function install_packages {
    apt-get install ttyrec mutt msmtp
}

function update_dotfiles {
    cd $dir
    echo -e "\n*** \e[1m Updating dotfiles repository\e[0m ***"
    git fetch -q origin master
    cat <(git log --reverse --pretty=format:"-- %h %s (%cr)" -4); echo -e "\e[36m"
    output=$(git log HEAD..origin)
    if [[ ! -z $output ]]; then
        cat <(git log --reverse --pretty=format:"-- %h %s (%cr)" HEAD..origin); echo -e "\e[0m"
    else
        echo -e "-- No updates since last pull\e[0m"
    fi
}

if [[ $# = 0 ]]; then
    update_dotfiles
    install_dotfiles
    update_repos
fi

while [[ $# > 0 ]]
do
    key="$1"
    case $key in
        -d|--dotfiles)
            install_dotfiles
            ;;
        -i|--repositories)
            install_repos
            ;;
        -u|--repositories)
            update_repos
            ;;
        -p|--packages)
            install_packages
            ;;
        -c|--clean)
            clean
            ;;
        -a|--all)
            clean
            install_dotfiles
            install_repos
            update_repos
            ;;
        *)
            ;;
    esac
    shift
done