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