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
36 # Check if `infocmp` is available.
37 if ! infocmp >/dev/null 2>&1; then
38 echo 'Warning: `infocmp` not available! 256color checks will fail.'
45 # Create private temporary directory used by many tools (including GNU screen
50 # Create rlwrap history directory.
52 # Create zsh cache directory.
58 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
59 # file in this directory; this requires replacing the constant HISTORY_PATH in
61 echo 'lesskey: generating .lesskey'
64 s/\bHISTORY_PATH\b/$ARGV[0]/;
71 # Custom colors for GNU ls.
72 if installed dircolors; then
73 echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
74 dircolors -b shell/dircolors.in >>shell/dircolors
77 # Find the required options to get colored ls output. GNU ls is preferred. See
78 # shell/aliases.in for details. Doing this here instead of in shell/aliases
79 # speeds up shell starts.
81 # Check if colors are available.
82 if ls --color >/dev/null 2>&1; then
85 if ls -G >/dev/null 2>&1; then
91 # Absolute path to `ls`.
92 ls_path=`installed_path ls`
94 if test "x$ls_color" = xgnu; then
97 # Normal (BSD) ls with colors.
98 elif test "x$ls_color" = xcli; then
99 ls_env='CLICOLOR_FORCE=1'
101 # Simple ls with no colors.
107 # Also check if `column -t` is available.
108 if echo test | column -t >/dev/null 2>&1; then
109 column=' | column -t'
114 generate perl shell/aliases \
115 -e 'while (<STDIN>) {
116 s/\bLS_ENV\b/$ARGV[0]/;
117 s/\bLS_PATH\b/$ARGV[1]/;
118 s/\bLS_COLOR\b/$ARGV[2]/;
119 s/\bCOLUMN\b/$ARGV[3]/;
122 "$ls_env" "$ls_path" "$ls_color" "$column"
124 # If `tig` is not available use my simple replacement.
125 if ! installed tig; then
126 echo "alias tig='git tig'" >>shell/aliases
129 # Check if grep supports --color=auto.
130 if echo test | grep --color=auto test >/dev/null 2>&1; then
133 echo 'shell/aliases: removing grep --color=auto'
134 sed_i '/^alias grep=/ s/^/#/' shell/aliases
137 generate cat screenrc .in
138 # As screen-256color is not widely supported use it only on machines where the
139 # matching terminfo entry is available. This also requires a terminal emulator
140 # which supports 256 colors. Also used for tmux.
142 if terminal_available screen-256color; then
143 # Called through SSH connection, assume the local system supports 256
145 if test -n "$SSH_CONNECTION"; then
147 # We have rxvt-unicode installed, check if it supports 256 colors.
148 elif installed urxvt; then
149 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
150 # CEST) for the strings/grep idea. The grep check is for "correct" 256
151 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
152 # terminal_info check for manual installations which modify
153 # rxvt-unicode's terminfo entry.
154 urxvt_path=`installed_path urxvt`
155 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
156 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
157 || terminal_info rxvt-unicode \
158 | grep -F 'colors#256' >/dev/null; then
161 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
162 # support 256 colors).
163 elif terminal_available xterm-256color; then
167 if test -z "$use_256colors"; then
168 echo 'screenrc: removing 256 colors'
169 sed_i 's/Enable 256 color/Disable 256 color/;
170 s/screen-256color/screen/' screenrc
172 # Some options are only necessary when running as root. They are marked as
174 if test "`id -u`" -ne 0; then
175 echo 'screenrc: removing root options'
176 grep_i -v '(ROOT)' screenrc
178 # I use some features of GNU screen which are only in Git or very recent GNU
179 # screen versions. Drop them on machines which have older versions. They are
181 if test ! -d "$HOME/development/shell/screen"; then
182 echo 'screenrc: removing Git features'
183 grep_i -v '(GIT)' screenrc
185 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
187 if installed rxvt; then
188 echo 'screenrc: removing attrcolor "fix"'
189 sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
191 # Display current battery charge on computers with a battery. Necessary lines
192 # are marked as "(BATTERY)".
193 if test ! -d /sys/class/power_supply/BAT0; then
194 echo 'screenrc: removing battery display'
195 grep_i -v '(BATTERY)' screenrc
198 if installed tmux; then
199 generate perl tmux.conf ./bin/remove-continuation.pl
201 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
203 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
204 perl ./tmux-window.pl 2 >tmux-window2.conf
205 # Set absolute path to tmux-window1.conf in tmux.conf.
206 perl <tmux.conf >tmux.conf.tmp \
207 -e 'while (<STDIN>) {
208 s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/;
211 "`pwd`/tmux-window1.conf"
212 mv tmux.conf.tmp tmux.conf
214 # 256 colors not available.
215 if test -z "$use_256colors"; then
216 echo 'tmux.conf: removing 256 colors'
217 sed_i 's/Enable 256 color/Disable 256 color/;
218 s/screen-256color/screen/' tmux.conf
220 # Tmux doesn't display a warning if the shell wasn't found!
221 if test ! -x '/bin/zsh'; then
222 echo 'tmux.conf: removing /bin/zsh as shell'
223 sed_i 's/zsh/sh/' tmux.conf
227 # Htop overwrites the comments in its configuration file.
228 generate cat htoprc .in
233 # Link setup for shells.
236 link bash/rc ~/.bashrc
237 link bash/profile ~/.bash_profile
238 link bash/logout ~/.bash_logout
239 if installed csh; then
243 link zsh/env ~/.zshenv
245 link zsh/logout ~/.zlogout
247 # Link setup for additional files.
248 if installed crontab; then
249 link crontab.d ~/.crontab.d
251 link lessfilter ~/.lessfilter
252 if installed colordiff; then
253 link colordiffrc ~/.colordiffrc
255 link inputrc ~/.inputrc
256 link screenrc ~/.screenrc
257 if installed tmux; then
258 link tmux.conf ~/.tmux.conf
260 if installed htop; then
261 link htoprc ~/.htoprc
262 # New location for htoprc. Use both for compatibility.
263 mkdir -p ~/.config/htop
264 link htoprc ~/.config/htop/htoprc
266 if test -d ~/.ssh && test -O ~/.ssh; then
267 link ssh_config ~/.ssh/config