]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - setup.sh
ssh_config: store ControlMaster socket in ~/.ssh/master/
[config/dotfiles.git] / setup.sh
1 #!/bin/sh
2
3 # Setup script for shell configuration files.
4
5 # Copyright (C) 2011-2014  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 simple_cpp <lesskey \
71     HISTORY_PATH -- "`pwd`/lesshistory" \
72     | lesskey -
73 chmod 0600 ~/.less
74
75 # Custom colors for GNU ls.
76 if installed dircolors; then
77     echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
78     dircolors -b shell/dircolors.in >>shell/dircolors
79 fi
80
81 # Find the required options to get colored ls output. GNU ls is preferred. See
82 # shell/aliases.in for details. Doing this here instead of in shell/aliases
83 # speeds up shell starts.
84 #
85 # GNU ls with colors available.
86 if ls --color >/dev/null 2>&1; then
87     ls_env=
88     ls_color='--color'
89     ls_options=
90 # Normal (BSD) ls with colors available.
91 elif ls -G >/dev/null 2>&1; then
92     ls_env='CLICOLOR_FORCE=1'
93     ls_color='-G'
94     # Don't display hidden files by default when running as root. I use `la`
95     # for that.
96     ls_options='-I'
97 # Simple ls with no colors.
98 else
99     ls_env=
100     ls_color=
101     ls_options=
102 fi
103
104 # Also check if `column -t` is available.
105 if echo test | column -t >/dev/null 2>&1; then
106     column=' | column -t'
107 else
108     column=
109 fi
110
111 generate shell/aliases .in simple_cpp \
112     LS_ENV LS_COLOR LS_OPTIONS COLUMN -- \
113     "$ls_env" "$ls_color" "$ls_options" "$column"
114
115 # If `tig` is not available use my simple replacement.
116 if ! installed tig; then
117     echo "alias tig='git tig'" >>shell/aliases
118 fi
119
120 # Check if grep supports --color=auto.
121 if echo test | grep --color=auto test >/dev/null 2>&1; then
122     :
123 else
124     echo 'shell/aliases: removing grep --color=auto'
125     sed_i '/^alias grep=/ s/^/#/' shell/aliases
126 fi
127
128 generate screenrc .in cat
129 # As screen-256color is not widely supported use it only on machines where the
130 # matching terminfo entry is available. This also requires a terminal emulator
131 # which supports 256 colors. Also used for tmux.
132 use_256colors=
133 if terminal_available screen-256color; then
134     # GNU/Linux's virtual terminal doesn't support 256 colors. If setup.sh is
135     # run one one, assume this user is mostly used from the terminal.
136     if test x"$TERM" = xlinux || test x"$TERM" = xscreen.linux; then
137         echo 'screenrc: running on virtual terminal, disabling 256 colors'
138     # Called through SSH connection, assume the local system supports 256
139     # colors.
140     elif test -n "${SSH_CONNECTION:+set}"; then
141         use_256colors=1
142     # We have rxvt-unicode installed, check if it supports 256 colors.
143     elif installed urxvt; then
144         # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
145         # CEST) for the strings/grep idea. The grep check is for "correct" 256
146         # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
147         # terminal_info check for manual installations which modify
148         # rxvt-unicode's terminfo entry.
149         urxvt_path=`installed_path urxvt`
150         urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
151         if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
152                 || terminal_info rxvt-unicode \
153                     | grep -F 'colors#256' >/dev/null; then
154             use_256colors=1
155         fi
156     # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
157     # support 256 colors).
158     elif terminal_available xterm-256color; then
159         use_256colors=1
160     fi
161 fi
162 if test -z "$use_256colors"; then
163     echo 'screenrc: removing 256 colors'
164     sed_i 's/Enable 256 color/Disable 256 color/;
165            s/screen-256color/screen/' screenrc
166 fi
167 # Some options are only necessary when running as root. They are marked as
168 # "(ROOT)".
169 if test "`id -u`" -ne 0; then
170     echo 'screenrc: removing root options'
171     grep_i -v '(ROOT)' screenrc
172 fi
173 # I use some features of GNU screen which are only in Git or very recent GNU
174 # screen versions. Drop them on machines which have older versions. They are
175 # marked as "(GIT)".
176 if test ! -d "$HOME/development/shell/screen"; then
177     echo 'screenrc: removing Git features'
178     grep_i -v '(GIT)' screenrc
179 fi
180 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
181 # it's installed.
182 if installed rxvt; then
183     echo 'screenrc: removing attrcolor "fix"'
184     sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
185 fi
186 # Display current battery charge on computers with a battery. Necessary lines
187 # are marked as "(BATTERY)". Also used for Tmux.
188 battery=
189 for x in /sys/class/power_supply/BAT*; do
190     test -d "$x" || continue
191     battery="$x"
192 done
193 apply_optional_replacement screenrc \
194     battery BATTERY "$battery"
195 # Display current temperature. Necessary lines are marked as "(TEMPERATURE)".
196 # Also used for Tmux.
197 temperature=/sys/devices/platform/coretemp.0
198 if ! test -d "$temperature"; then
199     temperature=
200 fi
201 apply_optional_replacement screenrc \
202     temperature TEMPERATURE "$temperature"
203
204
205 if installed tmux; then
206     generate tmux.conf .in cat
207
208     apply_optional_replacement tmux.conf \
209         battery BATTERY "$battery"
210     apply_optional_replacement tmux.conf \
211         temperature TEMPERATURE "$temperature"
212
213     # Old Tmux versions can't handle that.
214     generate tmux.conf '' ./bin/remove-continuation.pl
215
216     # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
217     # details.
218     perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
219     perl ./tmux-window.pl 2                           >tmux-window2.conf
220     # Set absolute path to tmux-window1.conf in tmux.conf.
221     generate tmux.conf '' simple_cpp \
222         TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
223
224     # 256 colors not available.
225     if test -z "$use_256colors"; then
226         echo 'tmux.conf: removing 256 colors'
227         sed_i 's/Enable 256 color/Disable 256 color/;
228                s/screen-256color/screen/' tmux.conf
229     fi
230     # Tmux doesn't display a warning if the shell wasn't found!
231     if test ! -x '/bin/zsh'; then
232         echo 'tmux.conf: removing /bin/zsh as shell'
233         sed_i 's/zsh/sh/' tmux.conf
234     fi
235 fi
236
237 # Htop overwrites the comments in its configuration file.
238 generate htoprc .in cat
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
246 # LINK SETUP
247
248 # Link setup for shells.
249 link shell ~/.shell
250 link bash ~/.bash
251 link bash/rc ~/.bashrc
252 link bash/profile ~/.bash_profile
253 link bash/logout ~/.bash_logout
254 if installed csh; then
255     link csh/rc ~/.cshrc
256 fi
257 link zsh ~/.zsh
258 link zsh/env ~/.zshenv
259 link zsh/rc ~/.zshrc
260 link zsh/logout ~/.zlogout
261
262 if installed tmux; then
263     link terminfo ~/.terminfo
264 fi
265
266 # Link setup for additional files.
267 if installed crontab; then
268     link crontab.d ~/.crontab.d
269 fi
270 link lessfilter ~/.lessfilter
271 if installed colordiff; then
272     link colordiffrc ~/.colordiffrc
273 fi
274 link inputrc ~/.inputrc
275 if installed ghci; then
276     link haskeline ~/.haskeline
277 fi
278 link screenrc ~/.screenrc
279 if installed tmux; then
280     link tmux.conf ~/.tmux.conf
281 fi
282 if installed htop; then
283     link htoprc ~/.htoprc
284     # New location for htoprc. Use both for compatibility.
285     mkdir -p ~/.config/htop
286     link htoprc ~/.config/htop/htoprc
287 fi
288 if test -d ~/.ssh && test -O ~/.ssh; then
289     mkdir -p ~/.ssh/master
290     link ssh_config ~/.ssh/config
291 fi
292 if installed dig; then
293     link digrc ~/.digrc
294 fi