]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - setup.sh
setup.sh,shell/aliases: Only use `column -t` if it's available.
[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 # Helper functions.
26 terminal_info() {
27     infocmp "$@" 2>&1
28 }
29 terminal_available() {
30     terminal_info "$@" > /dev/null
31 }
32 # Check if `infocmp` is available.
33 if ! infocmp > /dev/null 2>&1; then
34     echo 'Warning: `infocmp` not available! 256color checks will fail.'
35     echo
36 fi
37
38
39 # DIRECTORY SETUP
40
41 # Create private temporary directory used by many tools (including GNU screen
42 # and tmux).
43 mkdir -p ~/.tmp
44 chmod 0700 ~/.tmp
45
46 # Create rlwrap history directory.
47 mkdir -p shell/rlwrap
48 # Create zsh cache directory.
49 mkdir -p zsh/cache
50
51
52 # FILE SETUP
53
54 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
55 # file in this directory; this requires replacing the constant HISTORY_PATH in
56 # lesskey.
57 echo lesskey: generating .lesskey
58 perl < lesskey \
59     -e 'while (<STDIN>) {
60             s/\bHISTORY_PATH\b/$ARGV[0]/;
61             print;
62         }' \
63     "`pwd`/lesshistory" \
64     | lesskey -
65 chmod 0600 ~/.less
66
67 # Find the required options to get colored ls output. GNU ls is preferred. See
68 # shell/aliases.in for details. Doing this here instead of in shell/aliases
69 # speeds up shell starts.
70 #
71 # Check if colors are available.
72 ls --color > /dev/null 2>&1
73 if test $? -eq 0; then
74     ls_color=gnu
75 else
76     ls -G > /dev/null 2>&1
77     if test $? -eq 0; then
78         ls_color=cli
79     else
80         ls_color=
81     fi
82 fi
83 # Absolute path to `ls`.
84 ls_path=`installed_path ls`
85 # GNU ls with colors.
86 if test "x$ls_color" = xgnu; then
87     ls_env=
88     ls_color='--color'
89 # Normal (BSD) ls with colors.
90 elif test "x$ls_color" = xcli; 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 perl shell/aliases \
107     -e 'while (<STDIN>) {
108             s/\bLS_ENV\b/$ARGV[0]/;
109             s/\bLS_PATH\b/$ARGV[1]/;
110             s/\bLS_COLOR\b/$ARGV[2]/;
111             s/\bCOLUMN\b/$ARGV[3]/;
112             print;
113         }' \
114     "$ls_env" "$ls_path" "$ls_color" "$column"
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 '/^alias grep=/ s/^/#/' shell/aliases > shell/aliases.tmp
122     mv shell/aliases.tmp shell/aliases
123 fi
124
125 generate cat screenrc .in
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     # Called through SSH connection, assume the local system supports 256
132     # colors.
133     if test -n "$SSH_CONNECTION"; then
134         use_256colors=1
135     # We have rxvt-unicode installed, check if it supports 256 colors.
136     elif installed urxvt; then
137         # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
138         # CEST) for the strings/grep idea. The grep check is for "correct" 256
139         # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
140         # terminal_info check for manual installations which modify
141         # rxvt-unicode's terminfo entry.
142         urxvt_path=`installed_path urxvt`
143         urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
144         if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
145                 || terminal_info rxvt-unicode \
146                     | grep -F 'colors#256' >/dev/null; then
147             use_256colors=1
148         fi
149     # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
150     # support 256 colors).
151     elif terminal_available xterm-256color; then
152         use_256colors=1
153     fi
154 fi
155 if test -z "$use_256colors"; then
156     echo screenrc: removing 256 colors
157     sed 's/Enable 256 color/Disable 256 color/;
158          s/screen-256color/screen/' screenrc > screenrc.tmp
159     mv screenrc.tmp screenrc
160 fi
161 # Some options are only necessary when running as root. They are marked as
162 # "(ROOT)".
163 if test "`id -u`" -ne 0; then
164     echo screenrc: removing root options
165     grep -v '(ROOT)' screenrc > screenrc.tmp
166     mv screenrc.tmp 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 -v '(GIT)' screenrc > screenrc.tmp
174     mv screenrc.tmp screenrc
175 fi
176 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
177 # it's installed.
178 if installed rxvt; then
179     echo screenrc: removing attrcolor \"fix\"
180     sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp
181     mv screenrc.tmp screenrc
182 fi
183 # Display current battery charge on computers with a battery. Necessary lines
184 # are marked as "(BATTERY)".
185 if test ! -d /sys/class/power_supply/BAT0; then
186     echo screenrc: removing battery display
187     grep -v '(BATTERY)' screenrc > screenrc.tmp
188     mv screenrc.tmp screenrc
189 fi
190
191 if installed tmux; then
192     generate perl tmux.conf ./bin/remove-continuation.pl
193
194     # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
195     # details.
196     perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
197     perl ./tmux-window.pl 2                           > tmux-window2.conf
198     # Set absolute path to tmux-window1.conf in tmux.conf.
199     perl < tmux.conf > tmux.conf.tmp \
200         -e 'while (<STDIN>) {
201                 s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/;
202                 print;
203             }' \
204         "`pwd`/tmux-window1.conf"
205     mv tmux.conf.tmp tmux.conf
206
207     # 256 colors not available.
208     if test -z "$use_256colors"; then
209         echo tmux.conf: removing 256 colors
210         sed 's/Enable 256 color/Disable 256 color/;
211              s/screen-256color/screen/' tmux.conf > tmux.conf.tmp
212         mv tmux.conf.tmp tmux.conf
213     fi
214     # Tmux doesn't display a warning if the shell wasn't found!
215     if test ! -x '/bin/zsh'; then
216         echo tmux.conf: removing /bin/zsh as shell
217         sed 's/zsh/sh/' tmux.conf > tmux.conf.tmp
218         mv tmux.conf.tmp tmux.conf
219     fi
220 fi
221
222 # Htop overwrites the comments in its configuration file.
223 generate cat htoprc .in
224
225
226 # LINK SETUP
227
228 # Link setup for shells.
229 link shell ~/.shell
230 link bash ~/.bash
231 link bash/rc ~/.bashrc
232 link bash/profile ~/.bash_profile
233 link bash/logout ~/.bash_logout
234 if installed csh; then
235     link csh/rc ~/.cshrc
236 fi
237 link zsh ~/.zsh
238 link zsh/env ~/.zshenv
239 link zsh/rc ~/.zshrc
240 link zsh/logout ~/.zlogout
241
242 # Link setup for additional files.
243 if installed crontab; then
244     link crontab.d ~/.crontab.d
245 fi
246 link lessfilter ~/.lessfilter
247 if installed colordiff; then
248     link colordiffrc ~/.colordiffrc
249 fi
250 link inputrc ~/.inputrc
251 link screenrc ~/.screenrc
252 if installed tmux; then
253     link tmux.conf ~/.tmux.conf
254 fi
255 if installed htop; then
256     link htoprc ~/.htoprc
257     # New location for htoprc. Use both for compatibility.
258     mkdir -p ~/.config/htop
259     link htoprc ~/.config/htop/htoprc
260 fi
261