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