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