]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - setup.sh
shell/aliases,setup.sh: Move ls color detection to setup.sh.
[config/dotfiles.git] / setup.sh
1 #!/bin/sh
2
3 # Setup script for shell configuration files.
4
5 # Copyright (C) 2011-2012  Simon Ruderich
6 #
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.
11 #
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.
16 #
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/>.
19
20
21 . ../lib.sh
22
23 # Helper functions.
24 terminal_info() {
25     infocmp "$@" 2>&1
26 }
27 terminal_available() {
28     terminal_info "$@" > /dev/null
29 }
30 # Check if `infocmp` is available.
31 if ! infocmp > /dev/null 2>&1; then
32     echo 'Warning: `infocmp` not available! 256color checks will fail.'
33     echo
34 fi
35
36
37 # Create private temporary directory used by many tools (including GNU screen
38 # and tmux).
39 mkdir -p ~/.tmp
40 chmod 0700 ~/.tmp
41
42 # Generate ~/.less with lesskey.
43 lesskey lesskey
44 chmod 0600 ~/.less
45
46 # Find the required options to get colored ls output. GNU ls is preferred. See
47 # shell/aliases.in for details. Doing this here instead of in shell/aliases
48 # speeds up shell starts.
49 #
50 # Check if colors are available.
51 ls --color > /dev/null 2>&1
52 if test $? -eq 0; then
53     ls_color=gnu
54 else
55     ls -G > /dev/null 2>&1
56     if test $? -eq 0; then
57         ls_color=cli
58     else
59         ls_color=
60     fi
61 fi
62 # Absolute path to `ls`.
63 ls_path=`which ls`
64 # GNU ls with colors.
65 if test "x$ls_color" = xgnu; then
66     ls_env=
67     ls_color='--color'
68 # Normal (BSD) ls with colors.
69 elif test "x$ls_color" = xcli; then
70     ls_env='CLICOLOR_FORCE=1'
71     ls_color='-G'
72 # Simple ls with no colors.
73 else
74     ls_env=
75     ls_color=
76 fi
77 generate perl shell/aliases \
78     -e 'while (<STDIN>) {
79             s/\bLS_ENV\b/$ARGV[0]/;
80             s/\bLS_PATH\b/$ARGV[1]/;
81             s/\bLS_COLOR\b/$ARGV[2]/;
82             print;
83         }' \
84     "$ls_env" "$ls_path" "$ls_color"
85
86 generate cat screenrc .in
87 # As screen-256color is not widely supported use it only on machines where the
88 # matching terminfo entry is available. This also requires a terminal emulator
89 # which supports 256 colors. Also used for tmux.
90 use_256colors=
91 if terminal_available screen-256color; then
92     # Called through SSH connection, assume the local system supports 256
93     # colors.
94     if test -n "$SSH_CONNECTION"; then
95         use_256colors=1
96     # We have rxvt-unicode installed, check if it supports 256 colors.
97     elif installed urxvt; then
98         # Thanks to deryni in #rxvt-unicode on Freenode (2012-10-14 22:54
99         # CEST) for the strings/grep idea. The grep check is for "correct" 256
100         # rxvt-unicode binaries (e.g. Debian's rxvt-unicode-256color), the
101         # terminal_info check for manual installations which modify
102         # rxvt-unicode's terminfo entry.
103         urxvt_path=`which urxvt`
104         urxvt_grep=`strings "$urxvt_path" | grep '^TERM=rxvt-')`
105         if test x"$urxvt_grep" = 'xTERM=rxvt-unicode-256color' \
106                 || terminal_info rxvt-unicode \
107                     | grep -F 'colors#256' >/dev/null; then
108             use_256colors=1
109         fi
110     # Check if XTerm supports 256 colors (not a perfect check, but most XTerm
111     # support 256 colors).
112     elif terminal_available xterm-256color; then
113         use_256colors=1
114     fi
115 fi
116 if test -z "$use_256colors"; then
117     echo screenrc: removing 256 colors
118     sed 's/Enable 256 color/Disable 256 color/;
119          s/screen-256color/screen/' screenrc > screenrc.tmp
120     mv screenrc.tmp screenrc
121 fi
122 # Some options are only necessary when running as root. They are marked as
123 # "(ROOT)".
124 if test "`id -u`" -ne 0; then
125     echo screenrc: removing root options
126     grep -v '(ROOT)' screenrc > screenrc.tmp
127     mv screenrc.tmp screenrc
128 fi
129 # I use some features of GNU screen which are only in Git or very recent GNU
130 # screen versions. Drop them on machines which have older versions. They are
131 # marked as "(GIT)".
132 if test ! -d "$HOME/development/shell/screen"; then
133     echo screenrc: removing Git features
134     grep -v '(GIT)' screenrc > screenrc.tmp
135     mv screenrc.tmp screenrc
136 fi
137 # Rxvt doesn't need the attrcolor "fix". As I prefer rxvt assume I use it when
138 # it's installed.
139 if installed rxvt; then
140     echo screenrc: removing attrcolor \"fix\"
141     sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp
142     mv screenrc.tmp screenrc
143 fi
144 # Display current battery charge on computers with a battery. Necessary lines
145 # are marked as "(BATTERY)".
146 if test ! -d /sys/class/power_supply/BAT0; then
147     echo screenrc: removing battery display
148     grep -v '(BATTERY)' screenrc > screenrc.tmp
149     mv screenrc.tmp screenrc
150 fi
151
152 if installed tmux; then
153     generate perl tmux.conf ./bin/remove-continuation.pl
154
155     # 256 colors not available.
156     if test -z "$use_256colors"; then
157         echo tmux.conf: removing 256 colors
158         sed 's/Enable 256 color/Disable 256 color/;
159              s/screen-256color/screen/' tmux.conf > tmux.conf.tmp
160         mv tmux.conf.tmp tmux.conf
161     fi
162     # Tmux doesn't display a warning if the shell wasn't found!
163     if test ! -x '/bin/zsh'; then
164         echo tmux.conf: removing /bin/zsh as shell
165         sed 's/zsh/sh/' tmux.conf > tmux.conf.tmp
166         mv tmux.conf.tmp tmux.conf
167     fi
168 fi
169
170 # Htop overwrites the comments in its configuration file.
171 generate cat htoprc .in
172
173 # Link setup for shells.
174 link shell ~/.shell
175 link bash ~/.bash
176 link bash/rc ~/.bashrc
177 link bash/profile ~/.bash_profile
178 link bash/logout ~/.bash_logout
179 if installed csh; then
180     link csh/rc ~/.cshrc
181 fi
182 link zsh ~/.zsh
183 link zsh/env ~/.zshenv
184 link zsh/rc ~/.zshrc
185 link zsh/logout ~/.zlogout
186
187 # Link setup for additional files.
188 link lessfilter ~/.lessfilter
189 if installed colordiff; then
190     link colordiffrc ~/.colordiffrc
191 fi
192 link inputrc ~/.inputrc
193 link screenrc ~/.screenrc
194 if installed tmux; then
195     link tmux.conf ~/.tmux.conf
196 fi
197 if installed htop; then
198     link htoprc ~/.htoprc
199 fi
200
201 # Create rlwrap history directory.
202 mkdir -p shell/rlwrap