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/>.
29 terminal_available() {
30 terminal_info "$@" > /dev/null
32 # Check if `infocmp` is available.
33 if ! infocmp > /dev/null 2>&1; then
34 echo 'Warning: `infocmp` not available! 256color checks will fail.'
41 # Create private temporary directory used by many tools (including GNU screen
46 # Create rlwrap history directory.
48 # Create zsh cache directory.
54 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
55 # file in this directory; this requires replacing the constant HISTORY_PATH in
57 echo lesskey: generating .lesskey
60 s/\bHISTORY_PATH\b/$ARGV[0]/;
67 # Custom colors for GNU ls.
68 if installed dircolors; then
69 dircolors -b shell/dircolors.in > shell/dircolors
72 # Find the required options to get colored ls output. GNU ls is preferred. See
73 # shell/aliases.in for details. Doing this here instead of in shell/aliases
74 # speeds up shell starts.
76 # Check if colors are available.
77 if ls --color > /dev/null 2>&1; then
80 if ls -G > /dev/null 2>&1; then
86 # Absolute path to `ls`.
87 ls_path=`installed_path ls`
89 if test "x$ls_color" = xgnu; then
92 # Normal (BSD) ls with colors.
93 elif test "x$ls_color" = xcli; then
94 ls_env='CLICOLOR_FORCE=1'
96 # Simple ls with no colors.
102 # Also check if `column -t` is available.
103 if echo test | column -t >/dev/null 2>&1; then
104 column=' | column -t'
109 generate perl shell/aliases \
110 -e 'while (<STDIN>) {
111 s/\bLS_ENV\b/$ARGV[0]/;
112 s/\bLS_PATH\b/$ARGV[1]/;
113 s/\bLS_COLOR\b/$ARGV[2]/;
114 s/\bCOLUMN\b/$ARGV[3]/;
117 "$ls_env" "$ls_path" "$ls_color" "$column"
119 # If `tig` is not available use my simple replacement.
120 if ! installed tig; then
121 echo "alias tig='git tig'" >>shell/aliases
124 # Check if grep supports --color=auto.
125 if echo test | grep --color=auto test >/dev/null 2>&1; then
128 echo 'shell/aliases: removing grep --color=auto'
129 sed '/^alias grep=/ s/^/#/' shell/aliases > shell/aliases.tmp
130 mv shell/aliases.tmp shell/aliases
133 generate cat screenrc .in
134 # As screen-256color is not widely supported use it only on machines where the
135 # matching terminfo entry is available. This also requires a terminal emulator
136 # which supports 256 colors. Also used for tmux.
138 if terminal_available screen-256color; then
139 # Called through SSH connection, assume the local system supports 256
141 if test -n "$SSH_CONNECTION"; then
143 # We have rxvt-unicode installed, check if it supports 256 colors.
144 elif installed urxvt; then
145 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
146 # CEST) for the strings/grep idea. The grep check is for "correct" 256
147 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
148 # terminal_info check for manual installations which modify
149 # rxvt-unicode's terminfo entry.
150 urxvt_path=`installed_path urxvt`
151 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
152 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
153 || terminal_info rxvt-unicode \
154 | grep -F 'colors#256' >/dev/null; then
157 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
158 # support 256 colors).
159 elif terminal_available xterm-256color; then
163 if test -z "$use_256colors"; then
164 echo screenrc: removing 256 colors
165 sed 's/Enable 256 color/Disable 256 color/;
166 s/screen-256color/screen/' screenrc > screenrc.tmp
167 mv screenrc.tmp screenrc
169 # Some options are only necessary when running as root. They are marked as
171 if test "`id -u`" -ne 0; then
172 echo screenrc: removing root options
173 grep -v '(ROOT)' screenrc > screenrc.tmp
174 mv screenrc.tmp 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 -v '(GIT)' screenrc > screenrc.tmp
182 mv screenrc.tmp screenrc
184 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
186 if installed rxvt; then
187 echo screenrc: removing attrcolor \"fix\"
188 sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp
189 mv screenrc.tmp 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 -v '(BATTERY)' screenrc > screenrc.tmp
196 mv screenrc.tmp screenrc
199 if installed tmux; then
200 generate perl tmux.conf ./bin/remove-continuation.pl
202 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
204 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
205 perl ./tmux-window.pl 2 > tmux-window2.conf
206 # Set absolute path to tmux-window1.conf in tmux.conf.
207 perl < tmux.conf > tmux.conf.tmp \
208 -e 'while (<STDIN>) {
209 s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/;
212 "`pwd`/tmux-window1.conf"
213 mv tmux.conf.tmp tmux.conf
215 # 256 colors not available.
216 if test -z "$use_256colors"; then
217 echo tmux.conf: removing 256 colors
218 sed 's/Enable 256 color/Disable 256 color/;
219 s/screen-256color/screen/' tmux.conf > tmux.conf.tmp
220 mv tmux.conf.tmp tmux.conf
222 # Tmux doesn't display a warning if the shell wasn't found!
223 if test ! -x '/bin/zsh'; then
224 echo tmux.conf: removing /bin/zsh as shell
225 sed 's/zsh/sh/' tmux.conf > tmux.conf.tmp
226 mv tmux.conf.tmp tmux.conf
230 # Htop overwrites the comments in its configuration file.
231 generate cat htoprc .in
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