3 # Setup script for shell configuration files.
5 # Copyright (C) 2011-2014 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 # GNU ls with colors available.
86 if ls --color >/dev/null 2>&1; then
87 ls_args='command ls --color'
88 # Normal (BSD) ls with colors available.
89 elif ls -G >/dev/null 2>&1; then
90 # Don't display hidden files by default when running as root (-I), I use
92 ls_args='CLICOLOR_FORCE=1 command ls -G -I'
93 # Simple ls with no colors.
98 # Also check if `column -t` is available.
99 if echo test | column -t >/dev/null 2>&1; then
100 column=' | column -t'
105 generate shell/aliases .in simple_cpp \
109 # If `tig` is not available use my simple replacement.
110 if ! installed tig; then
111 echo "alias tig='git tig'" >>shell/aliases
114 # Check if grep supports --color=auto.
115 if echo test | grep --color=auto test >/dev/null 2>&1; then
118 echo 'shell/aliases: removing grep --color=auto'
119 sed_i '/^alias grep=/ s/^/#/' shell/aliases
122 generate screenrc .in cat
123 # As screen-256color is not widely supported use it only on machines where the
124 # matching terminfo entry is available. This also requires a terminal emulator
125 # which supports 256 colors. Also used for tmux.
127 if terminal_available screen-256color; then
128 # GNU/Linux's virtual terminal doesn't support 256 colors. If setup.sh is
129 # run one one, assume this user is mostly used from the terminal.
130 if test x"$TERM" = xlinux || test x"$TERM" = xscreen.linux; then
131 echo 'screenrc: running on virtual terminal, disabling 256 colors'
132 # Called through SSH connection, assume the local system supports 256
134 elif test -n "${SSH_CONNECTION:+set}"; then
136 # We have rxvt-unicode installed, check if it supports 256 colors.
137 elif installed urxvt; then
138 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
139 # CEST) for the strings/grep idea. The grep check is for "correct" 256
140 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
141 # terminal_info check for manual installations which modify
142 # rxvt-unicode's terminfo entry.
143 urxvt_path=`installed_path urxvt`
144 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
145 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
146 || terminal_info rxvt-unicode \
147 | grep -F 'colors#256' >/dev/null; then
150 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
151 # support 256 colors).
152 elif terminal_available xterm-256color; then
156 if test -z "$use_256colors"; then
157 echo 'screenrc: removing 256 colors'
158 sed_i 's/Enable 256 color/Disable 256 color/;
159 s/screen-256color/screen/' screenrc
161 # Some options are only necessary when running as root. They are marked as
163 if test "`id -u`" -ne 0; then
164 echo 'screenrc: removing root options'
165 grep_i -v '(ROOT)' screenrc
167 # I use some features of GNU screen which are only in Git or very recent GNU
168 # screen versions. Drop them on machines which have older versions. They are
170 if test ! -d "$HOME/development/shell/screen"; then
171 echo 'screenrc: removing Git features'
172 grep_i -v '(GIT)' screenrc
174 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
176 if installed rxvt; then
177 echo 'screenrc: removing attrcolor "fix"'
178 sed_i 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc
180 # Display current battery charge on computers with a battery. Necessary lines
181 # are marked as "(BATTERY)". Also used for Tmux.
183 for x in /sys/class/power_supply/BAT*; do
184 test -d "$x" || continue
187 apply_optional_replacement screenrc \
188 battery BATTERY "$battery"
189 # Display current temperature. Necessary lines are marked as "(TEMPERATURE)".
190 # Also used for Tmux.
191 temperature=/sys/devices/platform/coretemp.0
192 if ! test -d "$temperature"; then
195 apply_optional_replacement screenrc \
196 temperature TEMPERATURE "$temperature"
199 if installed tmux; then
200 generate tmux.conf .in cat
202 apply_optional_replacement tmux.conf \
203 battery BATTERY "$battery"
204 apply_optional_replacement tmux.conf \
205 temperature TEMPERATURE "$temperature"
207 # Old Tmux versions can't handle that.
208 generate tmux.conf '' ./bin/remove-continuation.pl
210 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
212 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
213 perl ./tmux-window.pl 2 >tmux-window2.conf
214 # Set absolute path to tmux-window1.conf in tmux.conf.
215 generate tmux.conf '' simple_cpp \
216 TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
218 # 256 colors not available.
219 if test -z "$use_256colors"; then
220 echo 'tmux.conf: removing 256 colors'
221 sed_i 's/Enable 256 color/Disable 256 color/;
222 s/screen-256color/screen/' tmux.conf
224 # Tmux doesn't display a warning if the shell wasn't found!
225 if test ! -x '/bin/zsh'; then
226 echo 'tmux.conf: removing /bin/zsh as shell'
227 sed_i 's/zsh/sh/' tmux.conf
231 # Htop overwrites the comments in its configuration file.
232 generate htoprc .in cat
234 if installed dig; then
235 # dig doesn't support any comments in digrc.
236 grep -v -E '^#' digrc.in >digrc
242 # Link setup for shells.
245 link bash/rc ~/.bashrc
246 link bash/profile ~/.bash_profile
247 link bash/logout ~/.bash_logout
248 if installed csh; then
252 link zsh/env ~/.zshenv
254 link zsh/logout ~/.zlogout
256 if installed tmux; then
257 link terminfo ~/.terminfo
260 # Link setup for additional files.
261 if installed crontab; then
262 link crontab.d ~/.crontab.d
264 link lessfilter ~/.lessfilter
265 if installed colordiff; then
266 link colordiffrc ~/.colordiffrc
268 link inputrc ~/.inputrc
269 if installed ghci; then
270 link haskeline ~/.haskeline
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 mkdir -p ~/.ssh/master
284 link ssh_config ~/.ssh/config
286 if installed dig; then