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.
52 # Generate ~/.less with lesskey. Prevent cluttering ~/ by storing the history
53 # file in this directory; this requires replacing the constant HISTORY_PATH in
55 echo lesskey: generating .lesskey
58 s/\bHISTORY_PATH\b/$ARGV[0]/;
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.
69 # Check if colors are available.
70 ls --color > /dev/null 2>&1
71 if test $? -eq 0; then
74 ls -G > /dev/null 2>&1
75 if test $? -eq 0; then
81 # Absolute path to `ls`.
84 if test "x$ls_color" = xgnu; then
87 # Normal (BSD) ls with colors.
88 elif test "x$ls_color" = xcli; then
89 ls_env='CLICOLOR_FORCE=1'
91 # Simple ls with no colors.
96 generate perl shell/aliases \
98 s/\bLS_ENV\b/$ARGV[0]/;
99 s/\bLS_PATH\b/$ARGV[1]/;
100 s/\bLS_COLOR\b/$ARGV[2]/;
103 "$ls_env" "$ls_path" "$ls_color"
105 # Check if grep supports --color=auto.
106 if echo test | grep --color=auto test >/dev/null 2>&1; then
109 echo 'shell/aliases: removing grep --color=auto'
110 sed '/^alias grep=/ s/^/#/' shell/aliases > shell/aliases.tmp
111 mv shell/aliases.tmp shell/aliases
114 generate cat screenrc .in
115 # As screen-256color is not widely supported use it only on machines where the
116 # matching terminfo entry is available. This also requires a terminal emulator
117 # which supports 256 colors. Also used for tmux.
119 if terminal_available screen-256color; then
120 # Called through SSH connection, assume the local system supports 256
122 if test -n "$SSH_CONNECTION"; then
124 # We have rxvt-unicode installed, check if it supports 256 colors.
125 elif installed urxvt; then
126 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
127 # CEST) for the strings/grep idea. The grep check is for "correct" 256
128 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
129 # terminal_info check for manual installations which modify
130 # rxvt-unicode's terminfo entry.
131 urxvt_path=`which urxvt`
132 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
133 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
134 || terminal_info rxvt-unicode \
135 | grep -F 'colors#256' >/dev/null; then
138 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
139 # support 256 colors).
140 elif terminal_available xterm-256color; then
144 if test -z "$use_256colors"; then
145 echo screenrc: removing 256 colors
146 sed 's/Enable 256 color/Disable 256 color/;
147 s/screen-256color/screen/' screenrc > screenrc.tmp
148 mv screenrc.tmp screenrc
150 # Some options are only necessary when running as root. They are marked as
152 if test "`id -u`" -ne 0; then
153 echo screenrc: removing root options
154 grep -v '(ROOT)' screenrc > screenrc.tmp
155 mv screenrc.tmp screenrc
157 # I use some features of GNU screen which are only in Git or very recent GNU
158 # screen versions. Drop them on machines which have older versions. They are
160 if test ! -d "$HOME/development/shell/screen"; then
161 echo screenrc: removing Git features
162 grep -v '(GIT)' screenrc > screenrc.tmp
163 mv screenrc.tmp screenrc
165 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
167 if installed rxvt; then
168 echo screenrc: removing attrcolor \"fix\"
169 sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp
170 mv screenrc.tmp screenrc
172 # Display current battery charge on computers with a battery. Necessary lines
173 # are marked as "(BATTERY)".
174 if test ! -d /sys/class/power_supply/BAT0; then
175 echo screenrc: removing battery display
176 grep -v '(BATTERY)' screenrc > screenrc.tmp
177 mv screenrc.tmp screenrc
180 if installed tmux; then
181 generate perl tmux.conf ./bin/remove-continuation.pl
183 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
185 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
186 perl ./tmux-window.pl 2 > tmux-window2.conf
187 # Set absolute path to tmux-window1.conf in tmux.conf.
188 perl < tmux.conf > tmux.conf.tmp \
189 -e 'while (<STDIN>) {
190 s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/;
193 "`pwd`/tmux-window1.conf"
194 mv tmux.conf.tmp tmux.conf
196 # 256 colors not available.
197 if test -z "$use_256colors"; then
198 echo tmux.conf: removing 256 colors
199 sed 's/Enable 256 color/Disable 256 color/;
200 s/screen-256color/screen/' tmux.conf > tmux.conf.tmp
201 mv tmux.conf.tmp tmux.conf
203 # Tmux doesn't display a warning if the shell wasn't found!
204 if test ! -x '/bin/zsh'; then
205 echo tmux.conf: removing /bin/zsh as shell
206 sed 's/zsh/sh/' tmux.conf > tmux.conf.tmp
207 mv tmux.conf.tmp tmux.conf
211 # Htop overwrites the comments in its configuration file.
212 generate cat htoprc .in
214 # Copy remindrc template if ~/.reminders doesn't exist yet.
215 if installed remind && test ! -e "$HOME/.reminders"; then
216 echo remind: copying template to .reminders
217 cp reminders.in "$HOME/.reminders"
223 # Link setup for shells.
226 link bash/rc ~/.bashrc
227 link bash/profile ~/.bash_profile
228 link bash/logout ~/.bash_logout
229 if installed csh; then
233 link zsh/env ~/.zshenv
235 link zsh/logout ~/.zlogout
237 # Link setup for additional files.
238 if installed crontab; then
239 link crontab.d ~/.crontab.d
241 link lessfilter ~/.lessfilter
242 if installed colordiff; then
243 link colordiffrc ~/.colordiffrc
245 link inputrc ~/.inputrc
246 link screenrc ~/.screenrc
247 if installed tmux; then
248 link tmux.conf ~/.tmux.conf
250 if installed htop; then
251 link htoprc ~/.htoprc
252 # New location for htoprc. Use both for compatibility.
253 mkdir -p ~/.config/htop
254 link htoprc ~/.config/htop/htoprc