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