]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/setup.sh
herbstluftwm/autostart: add bindings to run programs and select windows
[config/dotfiles.git] / shell / setup.sh
1 #!/bin/sh
2
3 # Setup script for shell configuration files.
4
5 # Copyright (C) 2011-2018  Simon Ruderich
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 set -eu
22
23 . ../lib.sh
24
25
26 # Helper functions.
27 terminal_info() {
28     infocmp "$@" 2>&1
29 }
30 terminal_available() {
31     terminal_info "$@" > /dev/null
32 }
33 # Usage: <file> <name> <grep-string> <value>
34 apply_optional_replacement() {
35     if test -n "$4"; then
36         printf '%s: using %s %s\n' "$1" "$2" "$4"
37         generate "$1" '' simple_cpp \
38             "$3" -- "$4"
39     else
40         printf '%s: removing %s display\n' "$1" "$2"
41         grep_i -v "$3" "$1"
42     fi
43 }
44 # Check if `infocmp` is available.
45 if ! infocmp >/dev/null 2>&1; then
46     echo 'Warning: `infocmp` not available! 256color checks will fail.'
47     echo
48 fi
49
50
51 # DIRECTORY SETUP
52
53 # Create private temporary directory used by many tools (including GNU screen
54 # and tmux).
55 mkdir -p ~/.tmp
56 chmod 0700 ~/.tmp
57
58 # Create rlwrap history directory.
59 mkdir -p shell/rlwrap
60 # Create zsh cache directory.
61 mkdir -p zsh/cache
62
63
64 # FILE SETUP
65
66 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
67 # file in this directory; this requires replacing the constant HISTORY_PATH in
68 # lesskey.
69 echo 'lesskey: generating .lesskey'
70 cp lesskey lesskey.tmp
71 if ! less --help | grep rscroll= > /dev/null; then
72     sed_i 's/--rscroll=[^ ]*//' lesskey.tmp
73 fi
74 simple_cpp < lesskey.tmp \
75     HISTORY_PATH -- "`pwd`/lesshistory" \
76     | lesskey -
77 rm lesskey.tmp
78 chmod 0600 ~/.less
79
80 # Custom colors for GNU ls.
81 if installed dircolors; then
82     echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
83     dircolors -b shell/dircolors.in >>shell/dircolors
84 fi
85
86 # Find the required options to get colored ls output. GNU ls is preferred. See
87 # shell/aliases.in for details. Doing this here instead of in shell/aliases
88 # speeds up shell starts.
89 #
90 # GNU ls with colors available.
91 if ls --color >/dev/null 2>&1; then
92     ls_args='command ls --color'
93 # Normal (BSD) ls with colors available.
94 elif ls -G >/dev/null 2>&1; then
95     # Don't display hidden files by default when running as root (-I), I use
96     # `la` for that.
97     ls_args='CLICOLOR_FORCE=1 command ls -G -I'
98 # OpenBSD has no colored ls, use colorls instead.
99 elif colorls -G >/dev/null 2>&1; then
100     ls_args='CLICOLOR_FORCE=1 colorls -G'
101 # Simple ls with no colors.
102 else
103     ls_args='command ls'
104 fi
105
106 # Also check if `column -t` is available.
107 if echo test | column -t >/dev/null 2>&1; then
108     column=' | column -t'
109 else
110     column=
111 fi
112
113 generate shell/aliases .in simple_cpp \
114     LS_ARGS COLUMN -- \
115     "$ls_args" "$column"
116
117 # If `tig` is not available use my simple replacement.
118 if ! installed tig; then
119     echo "alias tig='git tig'" >>shell/aliases
120 fi
121
122 # Check if grep supports --color=auto.
123 if echo test | grep --color=auto test >/dev/null 2>&1; then
124     :
125 else
126     echo 'shell/aliases: removing grep --color=auto'
127     sed_i '/^alias grep=/ s/^/#/' shell/aliases
128 fi
129
130 generate screenrc .in cat
131 # As screen-256color is not widely supported use it only on machines where the
132 # matching terminfo entry is available. This also requires a terminal emulator
133 # which supports 256 colors. Also used for tmux.
134 use_256colors=
135 if terminal_available screen-256color; then
136     # GNU/Linux's virtual terminal doesn't support 256 colors. If setup.sh is
137     # run one one, assume this user is mostly used from the terminal.
138     if test x"$TERM" = xlinux || test x"$TERM" = xscreen.linux; then
139         echo 'screenrc: running on virtual terminal, disabling 256 colors'
140     # Called through SSH connection, assume the local system supports 256
141     # colors.
142     elif test -n "${SSH_CONNECTION:+set}"; then
143         use_256colors=1
144     # We have rxvt-unicode installed, check if it supports 256 colors.
145     elif installed urxvt; then
146         # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
147         # CEST) for the strings/grep idea. The grep check is for "correct" 256
148         # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
149         # terminal_info check for manual installations which modify
150         # rxvt-unicode's terminfo entry.
151         urxvt_path=`installed_path urxvt`
152         urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
153         if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
154                 || terminal_info rxvt-unicode \
155                     | grep -F 'colors#256' >/dev/null; then
156             use_256colors=1
157         fi
158     # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
159     # support 256 colors).
160     elif terminal_available xterm-256color; then
161         use_256colors=1
162     fi
163 fi
164 if test -z "$use_256colors"; then
165     echo 'screenrc: removing 256 colors'
166     sed_i 's/Enable 256 color/Disable 256 color/;
167            s/screen-256color/screen/' screenrc
168 fi
169 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
170 # it's installed.
171 if installed rxvt; then
172     echo 'screenrc: removing attrcolor "fix"'
173     sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
174 fi
175 # Display current battery charge on computers with a battery. Necessary lines
176 # are marked as "(BATTERY)". Also used for Tmux.
177 battery=
178 for x in /sys/class/power_supply/BAT*; do
179     test -d "$x" || continue
180     battery="$x"
181 done
182 apply_optional_replacement screenrc \
183     battery BATTERY "$battery"
184 # Display current temperature. Necessary lines are marked as "(TEMPERATURE)".
185 # Also used for Tmux.
186 temperature=/sys/devices/platform/coretemp.0
187 if ! test -d "$temperature"; then
188     temperature=
189 fi
190 apply_optional_replacement screenrc \
191     temperature TEMPERATURE "$temperature"
192 # Settings for local hosts.
193 if test -n "${SSH_CONNECTION:+set}"; then
194     echo 'screenrc: using dark blue for remote screens'
195     sed_i 's/sorendition = Bw/sorendition = bw/' screenrc
196 else
197     echo 'screenrc: removing remote options'
198     grep_i -v '(REMOTE)' screenrc
199 fi
200
201
202 if installed tmux; then
203     generate tmux.conf .in cat
204
205     apply_optional_replacement tmux.conf \
206         battery BATTERY "$battery"
207     apply_optional_replacement tmux.conf \
208         temperature TEMPERATURE "$temperature"
209
210     # Old Tmux versions can't handle that.
211     generate tmux.conf '' ./bin/remove-continuation.pl
212
213     # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
214     # details.
215     perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
216     perl ./tmux-window.pl 2                           >tmux-window2.conf
217     # Set absolute path to tmux-window1.conf in tmux.conf.
218     generate tmux.conf '' simple_cpp \
219         TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
220
221     # 256 colors not available.
222     if test -z "$use_256colors"; then
223         echo 'tmux.conf: removing 256 colors'
224         sed_i 's/Enable 256 color/Disable 256 color/;
225                s/screen-256color/screen/' tmux.conf
226     fi
227     # Tmux doesn't display a warning if the shell wasn't found!
228     if test ! -x '/bin/zsh'; then
229         echo 'tmux.conf: removing /bin/zsh as shell'
230         sed_i 's/zsh/sh/' tmux.conf
231     fi
232 fi
233
234 # Htop overwrites the comments in its configuration file.
235 generate htoprc .in cat
236 if installed nproc && test "$(nproc)" -gt 8; then
237     sed_i 's/LeftCPUs/LeftCPUs2/; s/RightCPUs/RightCPUs2/' htoprc
238 fi
239
240 if installed dig; then
241     # dig doesn't support any comments in digrc.
242     grep -v -E '^#' digrc.in >digrc
243 fi
244
245 _iptables='/usr/share/zsh/functions/Completion/Linux/_iptables'
246 if test -f "$_iptables"; then
247     sed '/^#compdef/ s/iptables/& ip6tables/' \
248         < "$_iptables" \
249         > zsh/functions/_iptables
250 fi
251
252 # LINK SETUP
253
254 # Link setup for shells.
255 link shell ~/.shell
256 link bash ~/.bash
257 link bash/rc ~/.bashrc
258 link bash/profile ~/.bash_profile
259 link bash/logout ~/.bash_logout
260 if installed csh; then
261     link csh/rc ~/.cshrc
262 fi
263 link zsh ~/.zsh
264 link zsh/env ~/.zshenv
265 link zsh/rc ~/.zshrc
266 link zsh/logout ~/.zlogout
267
268 if installed tmux; then
269     link terminfo ~/.terminfo
270 fi
271
272 # Link setup for additional files.
273 if installed crontab; then
274     link crontab.d ~/.crontab.d
275 fi
276 link lessfilter ~/.lessfilter
277 if installed colordiff; then
278     link colordiffrc ~/.colordiffrc
279 fi
280 link inputrc ~/.inputrc
281 if installed ghci; then
282     link haskeline ~/.haskeline
283 fi
284 link screenrc ~/.screenrc
285 if installed tmux; then
286     link tmux.conf ~/.tmux.conf
287 fi
288 if installed htop; then
289     link htoprc ~/.htoprc
290     # New location for htoprc. Use both for compatibility.
291     mkdir -p ~/.config/htop
292     link htoprc ~/.config/htop/htoprc
293 fi
294 if test -d ~/.ssh && test -O ~/.ssh; then
295     mkdir -p ~/.ssh/master
296     link ssh_config ~/.ssh/config
297 fi
298 if installed dig; then
299     link digrc ~/.digrc
300 fi