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 # Find the required options to get colored ls output. GNU ls is preferred. See
68 # shell/aliases.in for details. Doing this here instead of in shell/aliases
69 # speeds up shell starts.
71 # Check if colors are available.
72 ls --color > /dev/null 2>&1
73 if test $? -eq 0; then
76 ls -G > /dev/null 2>&1
77 if test $? -eq 0; then
83 # Absolute path to `ls`.
86 if test "x$ls_color" = xgnu; then
89 # Normal (BSD) ls with colors.
90 elif test "x$ls_color" = xcli; then
91 ls_env='CLICOLOR_FORCE=1'
93 # Simple ls with no colors.
98 generate perl shell/aliases \
100 s/\bLS_ENV\b/$ARGV[0]/;
101 s/\bLS_PATH\b/$ARGV[1]/;
102 s/\bLS_COLOR\b/$ARGV[2]/;
105 "$ls_env" "$ls_path" "$ls_color"
107 # Check if grep supports --color=auto.
108 if echo test | grep --color=auto test >/dev/null 2>&1; then
111 echo 'shell/aliases: removing grep --color=auto'
112 sed '/^alias grep=/ s/^/#/' shell/aliases > shell/aliases.tmp
113 mv shell/aliases.tmp shell/aliases
116 generate cat screenrc .in
117 # As screen-256color is not widely supported use it only on machines where the
118 # matching terminfo entry is available. This also requires a terminal emulator
119 # which supports 256 colors. Also used for tmux.
121 if terminal_available screen-256color; then
122 # Called through SSH connection, assume the local system supports 256
124 if test -n "$SSH_CONNECTION"; then
126 # We have rxvt-unicode installed, check if it supports 256 colors.
127 elif installed urxvt; then
128 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
129 # CEST) for the strings/grep idea. The grep check is for "correct" 256
130 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
131 # terminal_info check for manual installations which modify
132 # rxvt-unicode's terminfo entry.
133 urxvt_path=`which urxvt`
134 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
135 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
136 || terminal_info rxvt-unicode \
137 | grep -F 'colors#256' >/dev/null; then
140 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
141 # support 256 colors).
142 elif terminal_available xterm-256color; then
146 if test -z "$use_256colors"; then
147 echo screenrc: removing 256 colors
148 sed 's/Enable 256 color/Disable 256 color/;
149 s/screen-256color/screen/' screenrc > screenrc.tmp
150 mv screenrc.tmp screenrc
152 # Some options are only necessary when running as root. They are marked as
154 if test "`id -u`" -ne 0; then
155 echo screenrc: removing root options
156 grep -v '(ROOT)' screenrc > screenrc.tmp
157 mv screenrc.tmp screenrc
159 # I use some features of GNU screen which are only in Git or very recent GNU
160 # screen versions. Drop them on machines which have older versions. They are
162 if test ! -d "$HOME/development/shell/screen"; then
163 echo screenrc: removing Git features
164 grep -v '(GIT)' screenrc > screenrc.tmp
165 mv screenrc.tmp screenrc
167 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
169 if installed rxvt; then
170 echo screenrc: removing attrcolor \"fix\"
171 sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp
172 mv screenrc.tmp screenrc
174 # Display current battery charge on computers with a battery. Necessary lines
175 # are marked as "(BATTERY)".
176 if test ! -d /sys/class/power_supply/BAT0; then
177 echo screenrc: removing battery display
178 grep -v '(BATTERY)' screenrc > screenrc.tmp
179 mv screenrc.tmp screenrc
182 if installed tmux; then
183 generate perl tmux.conf ./bin/remove-continuation.pl
185 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
187 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
188 perl ./tmux-window.pl 2 > tmux-window2.conf
189 # Set absolute path to tmux-window1.conf in tmux.conf.
190 perl < tmux.conf > tmux.conf.tmp \
191 -e 'while (<STDIN>) {
192 s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/;
195 "`pwd`/tmux-window1.conf"
196 mv tmux.conf.tmp tmux.conf
198 # 256 colors not available.
199 if test -z "$use_256colors"; then
200 echo tmux.conf: removing 256 colors
201 sed 's/Enable 256 color/Disable 256 color/;
202 s/screen-256color/screen/' tmux.conf > tmux.conf.tmp
203 mv tmux.conf.tmp tmux.conf
205 # Tmux doesn't display a warning if the shell wasn't found!
206 if test ! -x '/bin/zsh'; then
207 echo tmux.conf: removing /bin/zsh as shell
208 sed 's/zsh/sh/' tmux.conf > tmux.conf.tmp
209 mv tmux.conf.tmp tmux.conf
213 # Htop overwrites the comments in its configuration file.
214 generate cat htoprc .in
216 # Copy remindrc template if ~/.reminders doesn't exist yet.
217 if installed remind && test ! -e "$HOME/.reminders"; then
218 echo remind: copying template to .reminders
219 cp reminders.in "$HOME/.reminders"
225 # Link setup for shells.
228 link bash/rc ~/.bashrc
229 link bash/profile ~/.bash_profile
230 link bash/logout ~/.bash_logout
231 if installed csh; then
235 link zsh/env ~/.zshenv
237 link zsh/logout ~/.zlogout
239 # Link setup for additional files.
240 if installed crontab; then
241 link crontab.d ~/.crontab.d
243 link lessfilter ~/.lessfilter
244 if installed colordiff; then
245 link colordiffrc ~/.colordiffrc
247 link inputrc ~/.inputrc
248 link screenrc ~/.screenrc
249 if installed tmux; then
250 link tmux.conf ~/.tmux.conf
252 if installed htop; then
253 link htoprc ~/.htoprc
254 # New location for htoprc. Use both for compatibility.
255 mkdir -p ~/.config/htop
256 link htoprc ~/.config/htop/htoprc