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