3 # Setup script for shell configuration files.
5 # Copyright (C) 2011-2018 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'
70 cp lesskey lesskey.tmp
71 if ! less --help | grep rscroll= > /dev/null; then
72 sed_i 's/--rscroll=[^ ]*//' lesskey.tmp
74 simple_cpp < lesskey.tmp \
75 HISTORY_PATH -- "`pwd`/lesshistory" \
80 # Custom colors for GNU ls.
81 if installed dircolors; then
82 echo '# WARNING! DO NOT EDIT THIS FILE!' >shell/dircolors
83 dircolors -b shell/dircolors.in >>shell/dircolors
86 # Find the required options to get colored ls output. GNU ls is preferred. See
87 # shell/aliases.in for details. Doing this here instead of in shell/aliases
88 # speeds up shell starts.
90 # GNU ls with colors available.
91 if ls --color >/dev/null 2>&1; then
92 ls_args='command ls --color'
93 # Normal (BSD) ls with colors available.
94 elif ls -G >/dev/null 2>&1; then
95 # Don't display hidden files by default when running as root (-I), I use
97 ls_args='CLICOLOR_FORCE=1 command ls -G -I'
98 # OpenBSD has no colored ls, use colorls instead.
99 elif colorls -G >/dev/null 2>&1; then
100 ls_args='CLICOLOR_FORCE=1 colorls -G'
101 # Simple ls with no colors.
106 # Also check if `column -t` is available.
107 if echo test | column -t >/dev/null 2>&1; then
108 column=' | column -t'
113 generate shell/aliases .in simple_cpp \
117 # If `tig` is not available use my simple replacement.
118 if ! installed tig; then
119 echo "alias tig='git tig'" >>shell/aliases
122 # Check if grep supports --color=auto.
123 if echo test | grep --color=auto test >/dev/null 2>&1; then
126 echo 'shell/aliases: removing grep --color=auto'
127 sed_i '/^alias grep=/ s/^/#/' shell/aliases
130 if ! lsblk --merge >/dev/null 2>&1; then
131 echo 'shell/aliases: removing lsblk --merge'
132 sed_i 's/lsblk --merge/lsblk/' shell/aliases
135 generate screenrc .in cat
136 # Use absolute path for "shell" option for proper $SHELL variable.
137 apply_optional_replacement screenrc \
138 zsh ZSH "`installed_path zsh`"
139 # As screen-256color is not widely supported use it only on machines where the
140 # matching terminfo entry is available. This also requires a terminal emulator
141 # which supports 256 colors. Also used for tmux.
143 if terminal_available screen-256color; then
144 # GNU/Linux's virtual terminal doesn't support 256 colors. If setup.sh is
145 # run one one, assume this user is mostly used from the terminal.
146 if test x"$TERM" = xlinux || test x"$TERM" = xscreen.linux; then
147 echo 'screenrc: running on virtual terminal, disabling 256 colors'
148 # Called through SSH connection, assume the local system supports 256
150 elif test -n "${SSH_CONNECTION:+set}"; then
152 # We have rxvt-unicode installed, check if it supports 256 colors.
153 elif installed urxvt; then
154 # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
155 # CEST) for the strings/grep idea. The grep check is for "correct" 256
156 # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
157 # terminal_info check for manual installations which modify
158 # rxvt-unicode's terminfo entry.
159 urxvt_path=`installed_path urxvt`
160 urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-'`
161 if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
162 || terminal_info rxvt-unicode \
163 | grep -F 'colors#256' >/dev/null; then
166 # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
167 # support 256 colors).
168 elif terminal_available xterm-256color; then
172 if test -z "$use_256colors"; then
173 echo 'screenrc: removing 256 colors'
174 sed_i 's/Enable 256 color/Disable 256 color/;
175 s/screen-256color/screen/' 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"
200 # Settings for local hosts.
201 if test -n "${SSH_CONNECTION:+set}"; then
202 echo 'screenrc: using dark blue for remote screens'
203 sed_i 's/sorendition = Bw/sorendition = bw/' screenrc
205 echo 'screenrc: removing remote options'
206 grep_i -v '(REMOTE)' screenrc
210 if installed tmux; then
211 generate tmux.conf .in cat
213 apply_optional_replacement tmux.conf \
214 battery BATTERY "$battery"
215 apply_optional_replacement tmux.conf \
216 temperature TEMPERATURE "$temperature"
218 # Old Tmux versions can't handle that.
219 generate tmux.conf '' ./bin/remove-continuation.pl
221 # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for
223 perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" >tmux-window1.conf
224 perl ./tmux-window.pl 2 >tmux-window2.conf
225 # Set absolute path to tmux-window1.conf in tmux.conf.
226 generate tmux.conf '' simple_cpp \
227 TMUX_WINDOW_PATH -- "`pwd`/tmux-window1.conf"
229 # 256 colors not available.
230 if test -z "$use_256colors"; then
231 echo 'tmux.conf: removing 256 colors'
232 sed_i 's/Enable 256 color/Disable 256 color/;
233 s/screen-256color/screen/' tmux.conf
235 # Tmux doesn't display a warning if the shell wasn't found!
236 if test ! -x '/bin/zsh'; then
237 echo 'tmux.conf: removing /bin/zsh as shell'
238 sed_i 's/zsh/sh/' tmux.conf
242 # Htop overwrites the comments in its configuration file.
243 generate htoprc .in cat
244 if installed nproc && test "$(nproc)" -gt 8; then
245 sed_i 's/LeftCPUs/LeftCPUs2/; s/RightCPUs/RightCPUs2/' htoprc
248 if installed dig; then
249 # dig doesn't support any comments in digrc.
250 grep -v -E '^#' digrc.in >digrc
253 _iptables='/usr/share/zsh/functions/Completion/Linux/_iptables'
254 if test -f "$_iptables"; then
255 sed '/^#compdef/ s/iptables/& ip6tables/' \
257 > zsh/functions/_iptables
262 # Link setup for shells.
265 link bash/rc ~/.bashrc
266 link bash/profile ~/.bash_profile
267 link bash/logout ~/.bash_logout
268 if installed csh; then
272 link zsh/env ~/.zshenv
274 link zsh/logout ~/.zlogout
276 if installed tmux; then
277 link terminfo ~/.terminfo
280 # Link setup for additional files.
281 if installed crontab; then
282 link crontab.d ~/.crontab.d
284 link lessfilter ~/.lessfilter
285 if installed colordiff; then
286 link colordiffrc ~/.colordiffrc
288 link inputrc ~/.inputrc
289 if installed ghci; then
290 link haskeline ~/.haskeline
292 link screenrc ~/.screenrc
293 if installed tmux; then
294 link tmux.conf ~/.tmux.conf
296 if installed htop; then
297 link htoprc ~/.htoprc
298 # New location for htoprc. Use both for compatibility.
299 mkdir -p ~/.config/htop
300 link htoprc ~/.config/htop/htoprc
302 if test -d ~/.ssh && test -O ~/.ssh; then
303 mkdir -p ~/.ssh/master
304 link ssh_config ~/.ssh/config
306 if installed dig; then