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