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