dot/profile: Fix `__mdw_addto' if variable is initially empty.
[profile] / dot / profile
CommitLineData
74a53e28
MW
1### -*-sh-*-
2###
3### Session startup things.
4
5## The shell-specific hook will run us if it thinks we haven't been run
6## before. We should therefore let it know.
7__mdw_profile=t; export __mdw_profile
8
9###--------------------------------------------------------------------------
10### Utility functions.
11
12## __mdw_addto VAR DIR PATH ...
13##
14## VAR is the name of a PATH-like environment variable (i.e., one which
15## contains a sequence of pathnames separated by colons). DIR is either `l'
16## or `r'. The PATHs are pathnames. Those PATHs which correspond to
17## existing directories but which aren't currently named in the variable are
18## added to the left or right (depending on DIR) of VAR. The relative order
19## of PATHs added in the same invokation is the same as the order they
20## appeared in PATHs: the DIR argument only affects which end of the VAR they
21## get added to.
22__mdw_addto () {
23 local var=$1 val dir=$2 new="" change=nil
24 eval val=\$$var
25 shift 2
26 for i in "$@"; do
27 case "$new:" in *:$i:*) continue;; esac
28 if ! [ -d $i ]; then continue; fi
29 case "$val" in
74a53e28
MW
30 $i) continue ;;
31 *:$i:*) val=${val%%:$i:*}:${val#*:$i:} ;;
32 $i:*) val=${val#$i:} ;;
33 *:$i) val=${val%:$i} ;;
34 esac
35 new=$new:$i change=t
36 done
37 case $dir in
4c085f9e
MW
38 l) val=${new#:}${val:+:$val} ;;
39 r) val=${val:+$val:}${new#:} ;;
74a53e28
MW
40 esac
41 case $change in t) eval $var=\$val ;; esac
42}
43
44## __mdw_programp NAME
45##
46## Does NAME exist as an executable program?
47__mdw_programp () { type >/dev/null 2>&1 "$1"; }
48
49## __mdw_setconf VAR CONF [DEFAULT]
50##
51## If CONF is defined in `~/.mdw.conf' then set VAR to its value; otherwise,
52## set VAR to DEFAULT, if given; otherwise, do nothing at all.
53__mdw_setconf () {
54 local var=$1 conf=$2 val; shift 2
55 if val=$(mdw-conf 2>/dev/null "$conf" "$@"); then
56 eval "$var=\$val; export $var"
57 fi
58}
59
60###--------------------------------------------------------------------------
61### Other preliminaries.
62
63## Work out my home directory, resolving symbolic links.
64HOME=$(cd "$HOME"; pwd -P)
65case ${SCHROOT_SESSION_ID+t} in t) ;; *) cd "$HOME" ;; esac
66
67## CDE's session structure is demented and doesn't leave us with a proper
68## logout hook, so synthesize one here.
69case ${DT+t} in t) trap "source $HOME/.shell-logout" EXIT; esac
70
71###--------------------------------------------------------------------------
72### Set some basic paths.
73
74## The main path.
75export PATH
76__mdw_addto PATH l \
77 "$HOME"/bin \
78 /usr/local/bin /usr/local/sbin /usr/local/games \
79 /usr/bin /usr/sbin /usr/games \
80 /usr/X11R6/bin /usr/local/X11R6/bin \
81 /bin /sbin \
82 /opt/nfast/bin /opt/nfast/sbin
83
84## If we have Plan 9 from User Space, then add that in.
8fd6f7da
MW
85for i in /opt/plan9 /usr/local/plan9; do
86 if [ -d $i ]; then
87 PLAN9=$i; export PLAN9
88 __mdw_addto PATH r $i/bin
89 break
90 fi
91done
74a53e28 92
b97b3f8e
MW
93## Check for some standard path hacks.
94for i in ccache; do
95 __mdw_addto PATH l "$HOME"/bin/hacks/$i
96done
97
74a53e28
MW
98###--------------------------------------------------------------------------
99### Some other preliminaries.
100
101## Establish a temporary directory.
102case ${TMPDIR+t} in
103 t) ;;
104 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi
105esac
106TMP=$TMPDIR; export TMP
107
108## Sensible umask if users have their own groups.
109umask 002
110
111###--------------------------------------------------------------------------
112### Text editor configuration.
113
114MDW_EDITOR=ed
115emacs_startup_args="--no-site-file --mdw-fast-startup -nw"
116for ed in \
117 "emacs24 $emacs_startup_args" \
118 "emacs23 $emacs_startup_args" \
119 "emacs22 $emacs_startup_args" \
120 "emacs21 $emacs_startup_args" \
121 zile mg \
122 "emacs -nw" \
123 vi pico nano ae
124do
125 name=${ed%% *}
126 if __mdw_programp "$name"; then MDW_EDITOR=$ed; break; fi
127done
128EDITOR=mdw-editor VISUAL=mdw-editor
129export EDITOR VISUAL MDW_EDITOR
130unset ed emacs_startup_args
131
132###--------------------------------------------------------------------------
133### Locale configuration.
134
2c44f386
MW
135case ${LC_MDWSSHLANG+t},${DISPLAY+t} in
136 t,*)
137 LANG=$LC_MDWSSHLANG
138 ;;
139 ,t)
74a53e28
MW
140 __mdw_setconf LANG x-ctype POSIX
141 ;;
142 *)
143 : LANG=${LC_CTYPE-${LC_ALL-$(mdw-conf console-ctype POSIX)}}
144 case "$TERM,$(tty)" in
145 linux,/dev/tty*)
146 if { vt-is-UTF8 || kbd_mode | grep UTF-8; } >/dev/null 2>&1; then
2f27026a 147 ctype=.UTF-8
74a53e28
MW
148 else
149 ctype=
150 fi
151 LANG=${LANG%.*}$ctype
152 ;;
153 esac
154 ;;
155esac
156unset LC_ALL
157export LANG
158
159LC_COLLATE=POSIX; export LC_COLLATE
160
2c44f386
MW
161case ${LANG+t} in
162 t) LC_MDWSSHLANG=$LANG; export LC_MDWSSHLANG ;;
163esac
164
74a53e28
MW
165###--------------------------------------------------------------------------
166### Pagers.
167
168## Choose a sensible pager.
169MDW_PAGER=more
170for pg in less more; do
171 if __mdw_programp "$pg"; then MDW_PAGER=$(command -v "$pg"); break; fi
172done
173PAGER=mdw-pager METAMAIL_PAGER=mdw-pager
174export MDW_PAGER PAGER METAMAIL_PAGER
175unset pg
176
177## Configure `less'.
74a53e28
MW
178case ${LC_CTYPE-$LANG} in
179 *utf8 | *utf-8 | *UTF8 | *UTF-8) LESSCHARSET=utf-8 ;;
180 *) LESSCHARSET=latin1 ;;
181esac
182export LESSCHARSET
183if __mdw_programp global; then
184 LESSGLOBALTAGS=global
185 export LESSGLOBALTAGS
186fi
187
188###--------------------------------------------------------------------------
189### Miscellaneous things.
190
191## Mail and general identification.
192__mdw_setconf MAIL mailbox /var/mail/mdw
193__mdw_setconf EMAIL email mdw@distorted.org.uk
194NAME="Mark Wooding"; export NAME
195
196## News server.
197__mdw_setconf NNTPSERVER nntp-server
198
199## Some programs want to know the hostname.
200case ${HOST+t} in t) ;; *) HOST=$(hostname); export HOST; esac
201
202## HTTP and FTP proxies.
203http=$(mdw-conf http-proxy none)
204case "${http_proxy-none},$http" in
205 *,none) ;;
206 none,*) http_proxy=http://$http/; export http_proxy ;;
207esac
442bc88b
MW
208https=$(mdw-conf https-proxy none)
209case "${https_proxy-none},$https,${http_proxy-none}" in
210 *,none,none) ;;
211 none,none,*) https_proxy=$http_proxy; export https_proxy ;;
212 none,*,*) https_proxy=http://$https/; export ftp_proxy ;;
213esac
74a53e28
MW
214ftp=$(mdw-conf ftp-proxy none)
215case "${ftp_proxy-none},$ftp,${http_proxy-none}" in
216 *,none,none) ;;
217 none,none,*) ftp_proxy=$http_proxy; export ftp_proxy ;;
218 none,*,*) ftp_proxy=http://$ftp/; export ftp_proxy ;;
219esac
442bc88b 220unset http https ftp
74a53e28
MW
221
222## Ncurses programs should use the Unicode box-drawing characters because the
223## alternative character set stuff isn't supported well.
224NCURSES_NO_UTF8_ACS=1; export NCURSES_NO_UTF8_ACS
225
226## Shut up Perl's readline machinery.
227PERL_READLINE_NOWARN=yes; export PERL_READLINE_NOWARN
228
229## If we have `distcc' then tell `ccache' to use it.
230if __mdw_programp distcc; then CCACHE_PREFIX=distcc; export CCACHE_PREFIX; fi
231
232## Choose a sensible web browser. If we have a display, try to pick a
233## graphical one.
234set -- elinks w3m lynx
235case ${DISPLAY+t} in
94526c3f 236 t) set -- mdw-iceweasel mdw-chrome iceweasel firefox "$@" ;;
74a53e28
MW
237esac
238for b in "$@"; do
239 if __mdw_programp $b; then BROWSER=$b; export BROWSER; break; fi
240done
241unset b
242
243## Acquiring root privileges. This is mainly the job of `bashrc', but we
244## cache the mechanism here.
245__mdw_setconf __MDW_ROOTLY rootly
246BECOME="--preserve-environment"; export BECOME
247
248## It's useful to see the little sigils in `ls'.
249case ${LS_OPTIONS+t} in t) ;; *) LS_OPTIONS="-F"; export LS_OPTIONS; esac
250
251## Settings for BBC BASIC listing.
252export BASCAT="-l +n"
253
254## Version control hacking.
255CVS_RSH=ssh; export CVS_RSH
256__mdw_setconf CVSROOT cvs-root
257__mdw_setconf SVNROOT svn-root
258P4CONFIG=.p4; export P4CONFIG
259
260## Help X programs find their resources.
261XUSERFILESEARCHPATH="$HOME/.Xapps/%N:/usr/lib/X11/%T/%N%S"
262export XUSERFILESEARCHPATH
263
264## Make OpenOffice.org do its thing properly.
265OOO_FORCE_DESKTOP=gnome; export OOO_FORCE_DESKTOP
266
267## Hack Qt-ish things to be unstoatly.
f1e393ff 268QT_QPA_PLATFORMTHEME=gtk2; export QT_QPA_PLATFORMTHEME
e3a7c1c2 269QT_AUTO_SCREEN_SCALE_FACTOR=0; export QT_AUTO_SCREEN_SCALE_FACTOR
74a53e28 270
eb174df8
MW
271## Use X11 input method (including compose key sequences) everywhere.
272GTK_IM_MODULE=xim; export GTK_IM_MODULE
273QT_IM_MODULE=xim; export QT_IM_MODULE
274
8308648a
MW
275## Don't hide scrollbars.
276GTK_OVERLAY_SCROLLING=0; export GTK_OVERLAY_SCROLLING
277
ab94ba6e
MW
278## Rust Cargo things.
279CARGO_HOME=$HOME/.cache/cargo; export CARGO_HOME
280
74a53e28
MW
281## Configure `ps'.
282PS_PERSONALITY=gnu; export PS_PERSONALITY
283
f9ef6491 284## Configure Debian building.
b0b75b94 285DEB_BUILD_OPTIONS="parallel=$(mdw-conf make-parallel 4)"
f9ef6491
MW
286export DEB_BUILD_OPTIONS
287
18045f11
MW
288## Turn off angry fruit salad error messages from things.
289DPKG_COLORS=never; export DPKG_COLORS
290GCC_COLORS=; export GCC_COLORS
291CCC_OVERRIDE_OPTIONS="#^-fno-color-diagnostics"; export CCC_OVERRIDE_OPTIONS
292
74a53e28
MW
293## Disable core dumps.
294ulimit -S -c 0
295
296###--------------------------------------------------------------------------
297### Authentication and SSH hacking.
298
299## Start an authentication agent. This is unnecessarily fiddly. If there's
300## a Gnome keyring server then we should use that; unfortunately, it may not
301## yet have had a chance to populate the environment with its settings, so we
302## go off and fetch them.
303if { { [ "$GNOME_KEYRING_CONTROL" ] &&
304 [ -s "$GNOME_KEYRING_CONTROL" ]; } ||
305 { [ "$DBUS_SESSION_BUS_ADDRESS" ] &&
306 __mdw_programp gnome-keyring-daemon; }; } &&
307 stuff=$(gnome-keyring-daemon -s -c gpg 2>/dev/null)
308then
309 eval "$stuff"
310 export SSH_AUTH_SOCK GPG_AGENT_INFO
311fi
312
313## If we still don't have an agent then start one with a stable name.
314eval $(start-ssh-agent -b)
315
316## Decide whether this session should be considered `secure'. A session is
317## secure if it's on a secure TTY, but there are lots of ways of finding out
318## which TTYs are secure.
319if [ -z "$__mdw_bashrc" ] && [ "$__mdw_force_secure_session" = "yes" ] ||
320 ( tty="`tty`" devtty="(/dev/)?${tty#/dev/}"
321 { { { [ -e /etc/securetty ] && sectty=/etc/securetty; } ||
322 { [ -e /etc/securettys ] && sectty=/etc/securettys; }; } &&
323 egrep "$devtty" $sectty >/dev/null; } ||
324 { [ -e /etc/default/login ] &&
325 egrep "^CONSOLE=$devtty" /etc/default/login >/dev/null; } ||
326 case "${tty#/dev/}" in
327 console|systty|tty[0-9]) true ;;
328 *) false ;;
329 esac )
330then
331 __mdw_sechost=$HOST; export __mdw_sechost
332fi
333
334## Start a passphrase pixie if there is one and it's not already running.
335if pixie --version >/dev/null 2>&1; then
336 mkdir -p "$HOME/.catacomb"
337 pixie=${CATACOMB_PIXIE-$HOME/.catacomb/pixie}
338 if [ -S "$pixie" ] && pixie -C help >/dev/null 2>&1; then
339 :
340 else
341 pixie -d 2>>"$HOME/.catacomb/pixie.log"
342 __mdw_started_pixie=yes
343 fi
344fi
345
346###--------------------------------------------------------------------------
347### Finishing touches.
348
349## For old-fashioned Bourne-ish shells, set up per-shell definitions.
350ENV=$HOME/.shrc; export ENV
351
352## If there's a local hook then run it.
353if [ -f "$HOME/.profile-local" ]; then . "$HOME/.profile-local"; fi
354
355###----- That's all, folks --------------------------------------------------