3 # Setup script for shell configuration files.
5 # Copyright (C) 2011-2012 Simon Ruderich
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.
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.
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/>.
30 terminal_available() {
31 terminal_info "$@" > /dev/null
33 # Usage: <file> <name> <grep-string> <value>
34 apply_optional_replacement() {
36 echo "$1: using $2 $4"
37 generate "$1" '' simple_cpp \
40 echo "$1: removing $2 display"
44 # Check if `infocmp` is available.
45 if ! infocmp >/dev/null 2>&1; then
46 echo 'Warning: `infocmp` not available! 256color checks will fail.'
53 # Create private temporary directory used by many tools (including GNU screen
58 # Create rlwrap history directory.
60 # Create zsh cache directory.
66 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
67 # file in this directory; this requires replacing the constant HISTORY_PATH in
69 echo 'lesskey: generating .lesskey'
71 HISTORY_PATH -- "`pwd`/lesshistory" \
75 # Custom colors for GNU ls.
76 if installed dircolors; then
77 echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
78 dircolors -b shell/dircolors.in >>shell/dircolors
81 # Find the required options to get colored ls output. GNU ls is preferred. See
82 # shell/aliases.in for details. Doing this here instead of in shell/aliases
83 # speeds up shell starts.
85 # Check if colors are available.
86 if ls --color >/dev/null 2>&1; then
89 if ls -G >/dev/null 2>&1; then
95 # Absolute path to `ls`.
96 ls_path=`installed_path ls`
98 if test "x$ls_color" = xgnu; then
101 # Normal (BSD) ls with colors.
102 elif test "x$ls_color" = xcli; then
103 ls_env='CLICOLOR_FORCE=1'
105 # Simple ls with no colors.
111 # Also check if `column -t` is available.
112 if echo test | column -t >/dev/null 2>&1; then
113 column=' | column -t'
118 generate shell/aliases .in simple_cpp \
119 LS_ENV LS_PATH LS_COLOR COLUMN -- \
120 "$ls_env" "$ls_path" "$ls_color" "$column"
122 # If `tig` is not available use my simple replacement.
123 if ! installed tig; then
124 echo "alias tig='git tig'" >>shell/aliases
127 # Check if grep supports --color=auto.
128 if echo test | grep --color=auto test >/dev/null 2>&1; then
131 echo 'shell/aliases: removing grep --color=auto'
132 sed_i '/^alias grep=/ s/^/#/' shell/aliases
135 generate screenrc .in cat
136 # As screen-256color is not widely supported use it only on machines where the
137 # matching terminfo entry is available. This also requires a terminal emulator
138 # which supports 256 colors. Also used for tmux.
140 if terminal_available screen-256color; then
141 # Called through SSH connection, assume the local system supports 256
143 if test -n "$SSH_CONNECTION"; then
145 # We have rxvt-unicode installed, check if it supports 256 colors.
146 elif installed urxvt; then
147 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
148 # CEST) for the strings/grep idea. The grep check is for "correct" 256
149 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
150 # terminal_info check for manual installations which modify
151 # rxvt-unicode's terminfo entry.
152 urxvt_path=`installed_path urxvt`
153 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
154 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
155 || terminal_info rxvt-unicode \
156 | grep -F 'colors#256' >/dev/null; then
159 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
160 # support 256 colors).
161 elif terminal_available xterm-256color; then
165 if test -z "$use_256colors"; then
166 echo 'screenrc: removing 256 colors'
167 sed_i 's/Enable 256 color/Disable 256 color/;
168 s/screen-256color/screen/' screenrc
170 # Some options are only necessary when running as root. They are marked as
172 if test "`id -u`" -ne 0; then
173 echo 'screenrc: removing root options'
174 grep_i -v '(ROOT)' screenrc
176 # I use some features of GNU screen which are only in Git or very recent GNU
177 # screen versions. Drop them on machines which have older versions. They are
179 if test ! -d "$HOME/development/shell/screen"; then
180 echo 'screenrc: removing Git features'
181 grep_i -v '(GIT)' screenrc
183 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
185 if installed rxvt; then
186 echo 'screenrc: removing attrcolor "fix"'
187 sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
189 # Display current battery charge on computers with a battery. Necessary lines
190 # are marked as "(BATTERY)". Also used for Tmux.
192 for x in /sys/class/power_supply/BAT*; do
193 test -d "$x" || continue
196 apply_optional_replacement screenrc \
197 battery BATTERY "$battery"
200 if installed tmux; then
201 generate tmux.conf .in cat
203 apply_optional_replacement tmux.conf \
204 battery BATTERY "$battery"
206 # Old Tmux versions can't handle that.
207 generate tmux.conf '' ./bin/remove-continuation.pl
209 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
211 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
212 perl ./tmux-window.pl 2 >tmux-window2.conf
213 # Set absolute path to tmux-window1.conf in tmux.conf.
214 generate tmux.conf '' simple_cpp \
215 TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
217 # 256 colors not available.
218 if test -z "$use_256colors"; then
219 echo 'tmux.conf: removing 256 colors'
220 sed_i 's/Enable 256 color/Disable 256 color/;
221 s/screen-256color/screen/' tmux.conf
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_i 's/zsh/sh/' tmux.conf
230 # Htop overwrites the comments in its configuration file.
231 generate htoprc .in cat
236 # Link setup for shells.
239 link bash/rc ~/.bashrc
240 link bash/profile ~/.bash_profile
241 link bash/logout ~/.bash_logout
242 if installed csh; then
246 link zsh/env ~/.zshenv
248 link zsh/logout ~/.zlogout
250 # Link setup for additional files.
251 if installed crontab; then
252 link crontab.d ~/.crontab.d
254 link lessfilter ~/.lessfilter
255 if installed colordiff; then
256 link colordiffrc ~/.colordiffrc
258 link inputrc ~/.inputrc
259 link screenrc ~/.screenrc
260 if installed tmux; then
261 link tmux.conf ~/.tmux.conf
263 if installed htop; then
264 link htoprc ~/.htoprc
265 # New location for htoprc. Use both for compatibility.
266 mkdir -p ~/.config/htop
267 link htoprc ~/.config/htop/htoprc
269 if test -d ~/.ssh && test -O ~/.ssh; then
270 link ssh_config ~/.ssh/config