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 printf '%s: using %s %s\n' "$1" "$2" "$4"
37 generate "$1" '' simple_cpp \
40 printf '%s: removing %s display\n' "$1" "$2"
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 # Absolute path to `ls`.
86 ls_path=`installed_path ls`
87 # GNU ls with colors available.
88 if ls --color >/dev/null 2>&1; then
91 # Normal (BSD) ls with colors available.
92 elif ls -G >/dev/null 2>&1; then
93 ls_env='CLICOLOR_FORCE=1'
95 # Simple ls with no colors.
101 # Also check if `column -t` is available.
102 if echo test | column -t >/dev/null 2>&1; then
103 column=' | column -t'
108 generate shell/aliases .in simple_cpp \
109 LS_ENV LS_PATH LS_COLOR COLUMN -- \
110 "$ls_env" "$ls_path" "$ls_color" "$column"
112 # If `tig` is not available use my simple replacement.
113 if ! installed tig; then
114 echo "alias tig='git tig'" >>shell/aliases
117 # Check if grep supports --color=auto.
118 if echo test | grep --color=auto test >/dev/null 2>&1; then
121 echo 'shell/aliases: removing grep --color=auto'
122 sed_i '/^alias grep=/ s/^/#/' shell/aliases
125 generate screenrc .in cat
126 # As screen-256color is not widely supported use it only on machines where the
127 # matching terminfo entry is available. This also requires a terminal emulator
128 # which supports 256 colors. Also used for tmux.
130 if terminal_available screen-256color; then
131 # GNU/Linux's virtual terminal doesn't support 256 colors. If setup.sh is
132 # run one one, assume this user is mostly used from the terminal.
133 if test x"$TERM" = xlinux || test x"$TERM" = xscreen.linux; then
134 echo 'screenrc: running on virtual terminal, disabling 256 colors'
135 # Called through SSH connection, assume the local system supports 256
137 elif test -n "$SSH_CONNECTION"; then
139 # We have rxvt-unicode installed, check if it supports 256 colors.
140 elif installed urxvt; then
141 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
142 # CEST) for the strings/grep idea. The grep check is for "correct" 256
143 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
144 # terminal_info check for manual installations which modify
145 # rxvt-unicode's terminfo entry.
146 urxvt_path=`installed_path urxvt`
147 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
148 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
149 || terminal_info rxvt-unicode \
150 | grep -F 'colors#256' >/dev/null; then
153 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
154 # support 256 colors).
155 elif terminal_available xterm-256color; then
159 if test -z "$use_256colors"; then
160 echo 'screenrc: removing 256 colors'
161 sed_i 's/Enable 256 color/Disable 256 color/;
162 s/screen-256color/screen/' screenrc
164 # Some options are only necessary when running as root. They are marked as
166 if test "`id -u`" -ne 0; then
167 echo 'screenrc: removing root options'
168 grep_i -v '(ROOT)' screenrc
170 # I use some features of GNU screen which are only in Git or very recent GNU
171 # screen versions. Drop them on machines which have older versions. They are
173 if test ! -d "$HOME/development/shell/screen"; then
174 echo 'screenrc: removing Git features'
175 grep_i -v '(GIT)' screenrc
177 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
179 if installed rxvt; then
180 echo 'screenrc: removing attrcolor "fix"'
181 sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
183 # Display current battery charge on computers with a battery. Necessary lines
184 # are marked as "(BATTERY)". Also used for Tmux.
186 for x in /sys/class/power_supply/BAT*; do
187 test -d "$x" || continue
190 apply_optional_replacement screenrc \
191 battery BATTERY "$battery"
192 # Display current temperature. Necessary lines are marked as "(TEMPERATURE)".
193 # Also used for Tmux.
194 temperature=/sys/devices/platform/coretemp.0
195 if ! test -d "$temperature"; then
198 apply_optional_replacement screenrc \
199 temperature TEMPERATURE "$temperature"
202 if installed tmux; then
203 generate tmux.conf .in cat
205 apply_optional_replacement tmux.conf \
206 battery BATTERY "$battery"
207 apply_optional_replacement tmux.conf \
208 temperature TEMPERATURE "$temperature"
210 # Old Tmux versions can't handle that.
211 generate tmux.conf '' ./bin/remove-continuation.pl
213 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
215 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
216 perl ./tmux-window.pl 2 >tmux-window2.conf
217 # Set absolute path to tmux-window1.conf in tmux.conf.
218 generate tmux.conf '' simple_cpp \
219 TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
221 # 256 colors not available.
222 if test -z "$use_256colors"; then
223 echo 'tmux.conf: removing 256 colors'
224 sed_i 's/Enable 256 color/Disable 256 color/;
225 s/screen-256color/screen/' tmux.conf
227 # Tmux doesn't display a warning if the shell wasn't found!
228 if test ! -x '/bin/zsh'; then
229 echo 'tmux.conf: removing /bin/zsh as shell'
230 sed_i 's/zsh/sh/' tmux.conf
234 # Htop overwrites the comments in its configuration file.
235 generate htoprc .in cat
237 if installed dig; then
238 # dig doesn't support any comments in digrc.
239 grep -v -E '^#' digrc.in >digrc
245 # Link setup for shells.
248 link bash/rc ~/.bashrc
249 link bash/profile ~/.bash_profile
250 link bash/logout ~/.bash_logout
251 if installed csh; then
255 link zsh/env ~/.zshenv
257 link zsh/logout ~/.zlogout
259 if installed tmux; then
260 link terminfo ~/.terminfo
263 # Link setup for additional files.
264 if installed crontab; then
265 link crontab.d ~/.crontab.d
267 link lessfilter ~/.lessfilter
268 if installed colordiff; then
269 link colordiffrc ~/.colordiffrc
271 link inputrc ~/.inputrc
272 link screenrc ~/.screenrc
273 if installed tmux; then
274 link tmux.conf ~/.tmux.conf
276 if installed htop; then
277 link htoprc ~/.htoprc
278 # New location for htoprc. Use both for compatibility.
279 mkdir -p ~/.config/htop
280 link htoprc ~/.config/htop/htoprc
282 if test -d ~/.ssh && test -O ~/.ssh; then
283 link ssh_config ~/.ssh/config
285 if installed dig; then