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