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