]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - setup.sh
setup.sh: Remove grep_i(), moved to lib.sh.
[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 -e
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 # Check if `infocmp` is available.
34 if ! infocmp >/dev/null 2>&1; then
35     echo 'Warning: `infocmp` not available! 256color checks will fail.'
36     echo
37 fi
38
39
40 # DIRECTORY SETUP
41
42 # Create private temporary directory used by many tools (including GNU screen
43 # and tmux).
44 mkdir -p ~/.tmp
45 chmod 0700 ~/.tmp
46
47 # Create rlwrap history directory.
48 mkdir -p shell/rlwrap
49 # Create zsh cache directory.
50 mkdir -p zsh/cache
51
52
53 # FILE SETUP
54
55 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
56 # file in this directory; this requires replacing the constant HISTORY_PATH in
57 # lesskey.
58 echo 'lesskey: generating .lesskey'
59 simple_cpp <lesskey \
60     HISTORY_PATH -- "`pwd`/lesshistory" \
61     | lesskey -
62 chmod 0600 ~/.less
63
64 # Custom colors for GNU ls.
65 if installed dircolors; then
66     echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
67     dircolors -b shell/dircolors.in >>shell/dircolors
68 fi
69
70 # Find the required options to get colored ls output. GNU ls is preferred. See
71 # shell/aliases.in for details. Doing this here instead of in shell/aliases
72 # speeds up shell starts.
73 #
74 # Check if colors are available.
75 if ls --color >/dev/null 2>&1; then
76     ls_color=gnu
77 else
78     if ls -G >/dev/null 2>&1; then
79         ls_color=cli
80     else
81         ls_color=
82     fi
83 fi
84 # Absolute path to `ls`.
85 ls_path=`installed_path ls`
86 # GNU ls with colors.
87 if test "x$ls_color" = xgnu; then
88     ls_env=
89     ls_color='--color'
90 # Normal (BSD) ls with colors.
91 elif test "x$ls_color" = xcli; then
92     ls_env='CLICOLOR_FORCE=1'
93     ls_color='-G'
94 # Simple ls with no colors.
95 else
96     ls_env=
97     ls_color=
98 fi
99
100 # Also check if `column -t` is available.
101 if echo test | column -t >/dev/null 2>&1; then
102     column=' | column -t'
103 else
104     column=
105 fi
106
107 generate shell/aliases .in simple_cpp \
108     LS_ENV LS_PATH LS_COLOR COLUMN -- \
109     "$ls_env" "$ls_path" "$ls_color" "$column"
110
111 # If `tig` is not available use my simple replacement.
112 if ! installed tig; then
113     echo "alias tig='git tig'" >>shell/aliases
114 fi
115
116 # Check if grep supports --color=auto.
117 if echo test | grep --color=auto test >/dev/null 2>&1; then
118     :
119 else
120     echo 'shell/aliases: removing grep --color=auto'
121     sed_i '/^alias grep=/ s/^/#/' shell/aliases
122 fi
123
124 generate screenrc .in cat
125 # As screen-256color is not widely supported use it only on machines where the
126 # matching terminfo entry is available. This also requires a terminal emulator
127 # which supports 256 colors. Also used for tmux.
128 use_256colors=
129 if terminal_available screen-256color; then
130     # Called through SSH connection, assume the local system supports 256
131     # colors.
132     if test -n "$SSH_CONNECTION"; then
133         use_256colors=1
134     # We have rxvt-unicode installed, check if it supports 256 colors.
135     elif installed urxvt; then
136         # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
137         # CEST) for the strings/grep idea. The grep check is for "correct" 256
138         # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
139         # terminal_info check for manual installations which modify
140         # rxvt-unicode's terminfo entry.
141         urxvt_path=`installed_path urxvt`
142         urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
143         if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
144                 || terminal_info rxvt-unicode \
145                     | grep -F 'colors#256' >/dev/null; then
146             use_256colors=1
147         fi
148     # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
149     # support 256 colors).
150     elif terminal_available xterm-256color; then
151         use_256colors=1
152     fi
153 fi
154 if test -z "$use_256colors"; then
155     echo 'screenrc: removing 256 colors'
156     sed_i 's/Enable 256 color/Disable 256 color/;
157            s/screen-256color/screen/' screenrc
158 fi
159 # Some options are only necessary when running as root. They are marked as
160 # "(ROOT)".
161 if test "`id -u`" -ne 0; then
162     echo 'screenrc: removing root options'
163     grep_i -v '(ROOT)' screenrc
164 fi
165 # I use some features of GNU screen which are only in Git or very recent GNU
166 # screen versions. Drop them on machines which have older versions. They are
167 # marked as "(GIT)".
168 if test ! -d "$HOME/development/shell/screen"; then
169     echo 'screenrc: removing Git features'
170     grep_i -v '(GIT)' screenrc
171 fi
172 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
173 # it's installed.
174 if installed rxvt; then
175     echo 'screenrc: removing attrcolor "fix"'
176     sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
177 fi
178 # Display current battery charge on computers with a battery. Necessary lines
179 # are marked as "(BATTERY)".
180 if test ! -d /sys/class/power_supply/BAT0; then
181     echo 'screenrc: removing battery display'
182     grep_i -v '(BATTERY)' screenrc
183 fi
184
185 if installed tmux; then
186     generate tmux.conf .in perl ./bin/remove-continuation.pl
187
188     # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
189     # details.
190     perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
191     perl ./tmux-window.pl 2                           >tmux-window2.conf
192     # Set absolute path to tmux-window1.conf in tmux.conf.
193     generate tmux.conf '' simple_cpp \
194         TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
195
196     # 256 colors not available.
197     if test -z "$use_256colors"; then
198         echo 'tmux.conf: removing 256 colors'
199         sed_i 's/Enable 256 color/Disable 256 color/;
200                s/screen-256color/screen/' tmux.conf
201     fi
202     # Tmux doesn't display a warning if the shell wasn't found!
203     if test ! -x '/bin/zsh'; then
204         echo 'tmux.conf: removing /bin/zsh as shell'
205         sed_i 's/zsh/sh/' tmux.conf
206     fi
207 fi
208
209 # Htop overwrites the comments in its configuration file.
210 generate htoprc .in cat
211
212
213 # LINK SETUP
214
215 # Link setup for shells.
216 link shell ~/.shell
217 link bash ~/.bash
218 link bash/rc ~/.bashrc
219 link bash/profile ~/.bash_profile
220 link bash/logout ~/.bash_logout
221 if installed csh; then
222     link csh/rc ~/.cshrc
223 fi
224 link zsh ~/.zsh
225 link zsh/env ~/.zshenv
226 link zsh/rc ~/.zshrc
227 link zsh/logout ~/.zlogout
228
229 # Link setup for additional files.
230 if installed crontab; then
231     link crontab.d ~/.crontab.d
232 fi
233 link lessfilter ~/.lessfilter
234 if installed colordiff; then
235     link colordiffrc ~/.colordiffrc
236 fi
237 link inputrc ~/.inputrc
238 link screenrc ~/.screenrc
239 if installed tmux; then
240     link tmux.conf ~/.tmux.conf
241 fi
242 if installed htop; then
243     link htoprc ~/.htoprc
244     # New location for htoprc. Use both for compatibility.
245     mkdir -p ~/.config/htop
246     link htoprc ~/.config/htop/htoprc
247 fi
248 if test -d ~/.ssh && test -O ~/.ssh; then
249     link ssh_config ~/.ssh/config
250 fi