summaryrefslogtreecommitdiff
path: root/install
blob: 90d91d4dd86bbe32f845a9d011401944f90c2417 (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/bash
# dotfiles -- install
# author: johannst

gInstallConfigFile=install.config

#"<config_tag>:<dep_binary>:<installer_fct_name>"
gToolsConfig=(
"bashrc:bash:bashrcInstaller"
"gdb:gdb:gdbrcInstaller"
"cgdb:cgdb:cgdbrcInstaller"
"conkyrc:conky:conkyrcInstaller"
"gitconfig:git:gitConfigInstaller"
"tmux.conf:tmux:tmuxConfigInstaller"
"vimrc:vim:vimConfigInstaller"
"nvim:nvim:nvimConfigInstaller"
"emacs:emacs:emacsInstaller"
"Xresources:xterm:xtermConfigInstaller"
"i3config:i3:i3ConfigInstaller"
"i3status:i3status:i3statusConfigInstaller"
"i3blocks:i3blocks:i3blocksConfigInstaller"
"termite:termite:termiteConfigInstaller"
"zshrc:zsh:zshrcInstaller"
"rofi:rofi:rofiConfigInstaller"
)

gMagicNumber=e2718281
ABS_BASE_DIR=$(readlink -f $(dirname $0))

#{{{ bashrcInstaller

function bashrcInstaller() {
   local bashrc=~/.bashrc
   touch $bashrc
   grep $gMagicNumber $bashrc > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $bashrc
   echo -e "source $ABS_BASE_DIR/bashrc" >> $bashrc
   return 0
}

#}}}
#{{{ gdbrcInstaller

function gdbrcInstaller() {
   local gdbinit=~/.gdbinit
   local gdbdir=~/.gdb
   touch $gdbinit
   grep $gMagicNumber $gdbinit > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
	if [ ! -d $gdbdir ]; then mkdir $gdbdir; fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $gdbinit
   echo -e "source $ABS_BASE_DIR/gdbinit" >> $gdbinit
}

#}}}
#{{{ cgdbrcInstaller

function cgdbrcInstaller() {
   echo "cgdbrcInstaller called"
   return 1
}

#}}}
#{{{ conkyrcInstaller

function conkyrcInstaller() {
   echo "conkyrcInstaller called"
   return 1
}

#}}}
#{{{ gitConfigInstaller

function gitConfigInstaller() {
   local gitconf=~/.gitconfig
   touch $gitconf
   grep $gMagicNumber $gitconf > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $gitconf
   echo -e "[include]" >> $gitconf
   echo -e "   path = $ABS_BASE_DIR/gitconfig" >> $gitconf
   return 0
}

#}}}
#{{{ tmuxConfigInstaller

function tmuxConfigInstaller() {
   local tmuxconf=~/.tmux.conf
   touch $tmuxconf
   grep $gMagicNumber $tmuxconf > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $tmuxconf
   echo -e "source-file $ABS_BASE_DIR/tmux.conf" >> $tmuxconf
   return 0
}

#}}}
#{{{ vimConfigInstaller

function vimConfigInstaller() {
   local vimrc=~/.vimrc
   local vim_home=~/.vim
   touch $vimrc
   grep $gMagicNumber $vimrc > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   mkdir $vim_home > /dev/null 2>&1
   ln -s $ABS_BASE_DIR/vim/* $vim_home > /dev/null 2>&1
   echo -e "\n\" $gMagicNumber - Auto generated, do not delete or modify!" >> $vimrc
   echo -e "let \$MYVIMRC='$ABS_BASE_DIR/vimrc'" >> $vimrc
   echo -e "let \$VIMHOME='$vim_home'" >> $vimrc
   # link ctags
   type ctags >/dev/null 2>&1 && { echo -e "let \$MYCTAGS='$(which ctags)'" >> $vimrc; }
   echo -e "source $ABS_BASE_DIR/vimrc" >> $vimrc
   return 0
}

#}}}
#{{{ nvimConfigInstaller

function nvimConfigInstaller() {
   local vimrc=~/.config/nvim/init.vim
   mkdir -p ~/.config/nvim
   touch $vimrc
   grep $gMagicNumber $vimrc > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n\" $gMagicNumber - Auto generated, do not delete or modify!" >> $vimrc
   echo -e "source $ABS_BASE_DIR/nvim.init.vim" >> $vimrc
   # install plug pkg manager
   curl -fLo $HOME/.config/nvim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
   return 0
}

#}}}
#{{{ emacsInstaller

function emacsInstaller() {
   local emacsrc=~/.emacs
   touch $emacsrc
   grep $gMagicNumber $emacsrc > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n;; $gMagicNumber - Auto generated, do not delete or modify!" >> $emacsrc
   echo -e "(load \"$ABS_BASE_DIR/emacs\")" >> $emacsrc
   echo -e "(require 'johannst)" >> $emacsrc
   return 0
}

#}}}
#{{{ xtermConfigInstaller

function xtermConfigInstaller() {
   echo "xtermConfigInstaller called"
   return 1
}

#}}}
#{{{ i3ConfigInstaller

function i3ConfigInstaller() {
   local i3_home=~/.config/i3
   local i3_conf=$i3_home/config
	mkdir $i3_home 2> /dev/null
	if [ -f $i3_conf ]; then
		return 1
	fi
	cp $ABS_BASE_DIR/i3wm.conf $i3_conf
	return 0
}

#}}}
#{{{ i3statusConfigInstaller

function i3statusConfigInstaller() {
   local i3_home=~/config/.i3
   local i3_status_conf=$i3_home/i3status.conf
	mkdir $i3_home 2> /dev/null
	if [ -f $i3_status_conf ]; then
		return 1
	fi
	cp $ABS_BASE_DIR/i3status.conf $i3_status_conf

	local nif=$(ip link show | grep ^[1-9] | grep -v LOOPBACK | sed 's/ //g' | cut -d ':' -f 2)

	local num_wlanif=$(echo -e $nif | tr ' ' '\n' | grep wl | wc -l)
	local wlanif=$(echo -e $nif | tr ' ' '\n' | grep wl)
	if [[ $num_wlanif != 1 || -z $wlanif ]]; then
      echo "[Warning]: i3statusConfigInstaller couldn't find name of WIFI IF!"
		sed -i '/__DEF__WIFI_START__/,/__DEF__WIFI_END__/d' $i3_status_conf
	else
		sed -i "s/__NIF_WIFI__/$wlanif/g" $i3_status_conf
	fi

	local num_ethif=$(echo -e $nif | tr ' ' '\n' | grep en | wc -l)
	local ethif=$(echo -e $nif | tr ' ' '\n' | grep en)
	if [[ $num_ethif != 1 || -z $ethif ]]; then
      echo "[Warning]: i3statusConfigInstaller couldn't find name of ETH IF!"
		sed -i '/__DEF__ETH_START__/,/__DEF__ETH_END__/d' $i3_status_conf
	else
		sed -i "s/__NIF_ETH__/$ethif/g" $i3_status_conf
	fi

	sed -i '/__DEF__.*__/d' $i3_status_conf

	return 0
}

#}}}
#{{{ i3blocksConfigInstaller

function i3blocksConfigInstaller() {
   local i3_home=~/.config/i3
   local i3_blocks_conf=$i3_home/i3blocks.conf
	mkdir $i3_home 2> /dev/null
	if [ -f $i3_blocks_conf ]; then
		return 1
	fi
	cp $ABS_BASE_DIR/i3blocks.conf $i3_blocks_conf

	return 0
}

#}}}
#{{{ termiteConfigInstaller

function termiteConfigInstaller() {
	local tm_dir=~/.config/termite
   local tm_conf=$tm_dir/config
	[[ ! -d $tm_dir ]] && mkdir -p $tm_dir
   touch $tm_conf
   grep $gMagicNumber $tm_conf > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $tm_conf
	echo -e "$(cat termite)" >> $tm_conf

	return 0
}

#}}}
#{{{ zshrcInstaller

function zshrcInstaller() {
   local zshrc=~/.zshrc
   touch $zshrc
   grep $gMagicNumber $zshrc > /dev/null 2>&1
   if [[ $? = 0 ]]; then
      return 1
   fi
   echo -e "\n# $gMagicNumber - Auto generated, do not delete or modify!" >> $zshrc
   echo -e "source $ABS_BASE_DIR/zshrc" >> $zshrc
   return 0
}

#}}}
#{{{ i3blocksConfigInstaller

function rofiConfigInstaller() {
   local rofi_home=~/.config/rofi
   local rofi_conf=$rofi_home/config.rasi
	mkdir $rofi_home 2> /dev/null
	if [ -f $rofi_conf ]; then
		return 1
	fi
	ln -s $ABS_BASE_DIR/rofi.rasi $rofi_conf

	return 0
}

#}}}

#{{{ Installer Main Loop

function main() {
   [[ -f $gInstallConfigFile ]] || {
      generateInstallConfigFile $gInstallConfigFile
   }

   # parse gInstallConfigFile to see what should be installed
   local configToInstall=()
   while read line; do
      if [[ ! $line =~ ^[yYnN][[:blank:]]?- ]]; then 
         continue
      fi
      local flag=$(echo $line | cut -d '-' -f 1 | sed 's/ //g')
      if [[ $flag =~ ^[yY]$ ]]; then
         local config=$(echo $line | cut -d '-' -f 2 | sed 's/ //g')
         configToInstall+=("$config")
      fi
   done < $gInstallConfigFile

   echo "[Info]: List of tags found to install:"
   for conf in ${configToInstall[@]}; do
      echo -e "\t$conf"
   done

   [[ $1 != '-y' ]] && {
      echo "Install this configs? [yY/nN]"
      read go_install
      [[ $go_install =~ ^[^yY]$ ]] && exit 0
   }

   for conf in ${configToInstall[@]}; do
      installConfig $conf
   done
}

#}}}
#{{{ Installer Core

generateInstallConfigFile () {
    local config_fname=$1
    : > $config_fname

    echo "No config found, about to generate new config >> $config_fname <<"
    for conf in ${gToolsConfig[@]}; do
        local conf=$(echo $conf | cut -d':' -f1)
        echo "Install $conf? [yY/nN]"
        read install
        [[ $install =~ ^[^yY]?$ ]] && {
            echo "n - $conf" >> $config_fname
        } || {
            echo "y - $conf" >> $config_fname
        }
    done
}

installConfig() {
   local config=$1
   local binary=$(getDepenentBinaryName $config)
   isBinaryInstalled $binary
   if [[ $? -eq 1 ]]; then 
      echo "[Warning]: Skipping $config... $binary is not installed!"
      return 
   fi
   local installerFctPtr=$(getConfigInstallerFctPtr $config)
   if [[ -z $installerFctPtr ]]; then 
      echo "[Warning]: Skipping $config... installer function pointer lookup error!"
      return
   fi
   isInstallerDefined $installerFctPtr
   if [[ $? -eq 1 ]]; then 
      echo "[Warning]: Skipping $config... $installerFctPtr not defined!"
      return
   fi
   echo "[Info]: Intalling config for $binary using $installerFctPtr!"
   $installerFctPtr
   if [[ $? -eq 1 ]]; then 
      echo "[Info]: $config already installed!"
      return
   fi
   echo "[Info]: $config sucessfully installed!"
}

getValue() {
   local search_key=$1
   for k in ${gToolsConfig[@]}; do 
      local key=$(echo $k | cut -d ':' -f 1)
      if [[ "$key" == "$search_key" ]]; then 
         local entry=$(echo $k | cut -d ':' -f 2,3)
         echo ${entry}
         break
      fi
   done
}

getDepenentBinaryName() {
   local result=$(echo $(getValue $1) | cut -d ':' -f 1)
   echo $result
}

getConfigInstallerFctPtr() {
   local result=$(echo $(getValue $1) | cut -d ':' -f 2)
   echo $result
}

isBinaryInstalled() {
   local binary=$1
   type $binary >/dev/null 2>&1 || { return 1; }
   return 0
}

isInstallerDefined () {
   local func=$1
   declare -F $func >/dev/null 2>&1 || { return 1; }
   return 0
}

#}}}

main $1