Update.
[scripts.git] / bashrc
1 # -*-Shell-script-*-
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # The site-specific and confidential settings are in another file
21
22 PRIVATE_BASHRC="${HOME}/private/bashrc.perso"
23
24 # If the MANPATH is not set, set it
25
26 [[ "${MANPATH}" ]] || MANPATH=$(manpath)
27
28 # If the private bashrc exists, execute it
29
30 [[ -f "${PRIVATE_BASHRC}" ]] && source "${PRIVATE_BASHRC}"
31
32 # !!! THIS HAS TO BE HERE EVEN IN THE NON-INTERACTIVE PART OR YOU WILL
33 # LOSE YOU PREVIOUS HISTORY !!!
34
35 export HISTFILESIZE=20000
36 export HISTSIZE=${HISTFILESIZE}
37
38 export HISTIGNORE="${HISTIGNORE}:&:[ ]*"
39
40 # I want to save the command time, but I do not want to see it in
41 # history
42
43 export HISTTIMEFORMAT=""
44
45 shopt -s histappend
46
47 # I realized that most of my settings are meaningful only in
48 # interactive mode. This should maybe be done more properly through
49 # using different .bash_profile and .bash_login
50
51 [ ${TERM} == "dumb" ] || [[ ! -t 0 ]] && return
52
53 # Remove the annoying beeps in console
54
55 # setterm -blength 0
56
57 # || setterm --blength 0 # Seriously?
58
59 ######################################################################
60 ## The interactive part
61
62 export VT_RESET=$'\e[0m'
63 export VT_BOLD=$'\e[1m'
64 export VT_UNDERLINE=$'\e[4m'
65 export VT_BLINK=$'\e[5m'
66
67 export VT_SET_TITLE=$'\e]0;'
68 export VT_END_TITLE=$'\007'
69
70 export VT_BLACK_FG=$'\e[30m'
71 export VT_RED_FG=$'\e[31m'
72 export VT_GREEN_FG=$'\e[32m'
73 export VT_YELLOW_FG=$'\e[33m'
74 export VT_BLUE_FG=$'\e[34m'
75 export VT_MAGENTA_FG=$'\e[35m'
76 export VT_CYAN_FG=$'\e[36m'
77 export VT_WHITE_FG=$'\e[37m'
78
79 export VT_BLACK_BG=$'\e[40m'
80 export VT_RED_BG=$'\e[41m'
81 export VT_GREEN_BG=$'\e[42m'
82 export VT_YELLOW_BG=$'\e[43m'
83 export VT_BLUE_BG=$'\e[44m'
84 export VT_MAGENTA_BG=$'\e[45m'
85 export VT_CYAN_BG=$'\e[46m'
86 export VT_WHITE_BG=$'\e[47m'
87
88 # Colorize man pages!
89
90 export LESS_TERMCAP_us=${VT_GREEN_FG}
91 export LESS_TERMCAP_ue=${VT_RESET}
92 export LESS_TERMCAP_md=${VT_BLUE_FG}${VT_BOLD}
93 export LESS_TERMCAP_me=${VT_RESET}
94
95 # export LESS_TERMCAP_md=$'\e[1;34;40m'
96
97 # This prevents ^S from freezing the shell
98
99 # stty -ixon
100
101 ulimit -c unlimited
102
103 alias ..='cd ..'
104 alias -- -='cd -'
105 alias rm='rm -v -i'
106 alias mv='mv -v -i'
107 alias chmod='chmod -c'
108 alias cp='cp -v -i'
109 alias rd=rmdir
110 alias md='mkdir -pv'
111 alias ps='ps uxaf'
112 alias df='df -hT'
113 # alias df='df -hT --sync'
114 alias grep='grep -i -E --color=auto'
115 alias find='ionice -c3 find'
116 alias pd=pushd
117 alias val='valgrind --leak-check=full --show-reachable=yes --db-attach=yes '
118
119 alias s='screen -d -R -U && clear'
120 alias p='feh --force-aliasing -d --full-screen --auto-zoom'
121
122 function nh () {
123     export HISTFILE=/dev/null
124     unalias cd
125 }
126
127 function select-tz () {
128     # Select a value for TZ
129     TMP=$(mktemp /tmp/select-tz.XXXXXX)
130     selector -o "${TMP}" <(grep -v ^# /usr/share/zoneinfo/zone.tab  | awk '{print $3}' | sort -u)
131     cat "${TMP}"
132     \rm -f "${TMP}"
133 }
134
135 function ding () {
136     if [[ $1 ]] && which winshepherd.sh; then
137         play -q ~/local/sounds/deskbell.wav &
138         unset E_APP_WINDOW
139         winshepherd.sh message green "$1"
140     else
141         play -q ~/local/sounds/deskbell.wav
142     fi
143 }
144
145 alias impressive="impressive -s -D 1000 -t Crossfade -T 100"
146
147 if [[ -e "${HOME}/.dircolors" ]]; then
148     eval $(dircolors "${HOME}/.dircolors")
149     alias ls='ls -p --group-directories-first --color'
150     alias lt='ls -p --color -gohtr --time-style="+%Y %b %d %H:%M"'
151     alias ll='ls -p --color -goh --time-style="+%Y %b %d %H:%M"'
152     alias lll='ls -p --color -lth'
153     alias l='ls -p --color -I "*~" -I "*.o"'
154     alias less='less -R'
155 else
156     alias ls='ls -p --group-directories-first'
157     alias lt='ls -p -gohtr --time-style="+%Y %b %d %H:%M"'
158     alias ll='ls -p -goh --time-style="+%Y %b %d %H:%M"'
159     alias lll='ls -p -lth'
160     alias l='ls -p -I "*~" -I "*.o"'
161 fi
162
163 export EDITOR=emacsclient
164 export GIT_EDITOR=${EDITOR}
165
166 ######################################################################
167 # Ignored extensions when completing
168
169 # export FIGNORE="CVS"
170
171 function latexdiff () {
172     wdiff -n \
173         -w $'\033[30;41m' -x $'\033[0m' \
174         -y $'\033[30;42m' -z $'\033[0m' \
175         "$@"
176 }
177
178 # Looks for the most recent .log and pdflatex + bibtex the
179 # corresponding tex file
180
181 function rl () {
182     # RECENT_LOG=$(ls -t $(find -maxdepth 1 -name "*.log" -type f) | head -1)
183
184     RECENT_LOG=$(ls -t *.log | head -1)
185
186     if [[ ${RECENT_LOG} ]]; then
187         FILEBASE="${RECENT_LOG/.log/}"
188
189         if [[ -f "${FILEBASE}.tex" ]]; then
190
191             pdflatex "${FILEBASE}"
192             bibtex "${FILEBASE}"
193             pdflatex "${FILEBASE}"
194             pdflatex "${FILEBASE}"
195
196             if [[ "$1" ]]; then
197                 if [[ "$1" == "-v" ]] || [[ "$1" == "--view" ]]; then
198                     xpdf "${FILEBASE}.pdf"
199                 elif [[ "$1" == "-p" ]] || [[ "$1" == "--print" ]]; then
200                     lpr "${FILEBASE}.pdf"
201                 else
202                     echo "Usage: rl [-v|--view] [-p|--print]" >&2
203                     echo "Unknown option $1" >&2
204                     return 1
205                 fi
206             fi
207
208         else
209
210             echo "Can not find a tex file corresponding to the most recent log (${RECENT_LOG/.log/})." >&2
211             return 1
212
213         fi
214
215     else
216         echo "Can not find a recent log." >&2
217         return 1
218     fi
219 }
220
221
222 ######################################################################
223 # Functions
224
225 # http://www.reddit.com/r/linux/comments/2cgu5k/a_handy_little_script_for_interacting_with_your/
226 function clip () {
227     if [[ -t 0 ]] && [[ -z "$1" ]]; then
228         # output contents of clipboard
229         xclip -out -selection clipboard || exit 1
230     elif [[ "$1" ]]; then
231         # copy file contents to clipboard
232         xclip -in -selection clipboard < "$1" || exit 1
233     else
234         # copy stdin to clipboard
235         xclip -in -selection clipboard <&0 || exit 1
236     fi
237 }
238
239 # Find a file whose name contains a substring
240
241 function fn () {
242     name=$1
243     shift
244     find "$@" -name "*${name}*";
245 }
246
247 function bak () {
248     while [[ "$1" ]]; do
249         cp "$1" "$1".bak
250         shift
251     done
252 }
253
254 # A password generator
255
256 function genpw () {
257     tr -dc A-Za-z0-9 < /dev/urandom | head -c16
258     # tr -dc [:graph:] < /dev/urandom | head -c16
259     echo
260 }
261
262 function ua () {
263
264     [[ "$1" ]] || ( echo "Universal unarchive: ua <file> [<file> ...]" >&2 && return 1)
265
266     while [[ "$1" ]]; do
267
268         case "$1" in
269             *.tgz|*.tar.gz|*.tbz|*.tar.bz2)
270                 tar xvf "$1"
271                 ;;
272
273             *.rar)
274                 unrar -kb x "$1"
275                 ;;
276
277             *.zip)
278                 unzip "$1"
279                 ;;
280
281             *)
282                 echo "Unknown file extension $1"
283                 ;;
284         esac
285
286         shift
287
288     done
289 }
290
291 # Create a dir and cd there
292
293 function mcd () {
294     mkdir -vp "$1"
295     cd "$1"
296 }
297
298 # Capture the screen in a dated png
299
300 function cap () {
301     if [[ $2 ]]; then
302         name=$2
303     else
304         name="capture-$(date +%s).png"
305     fi
306     echo "Waiting $1 s and saving to ${name}."
307     [[ "$1" ]] && sleep "$1"
308     echo "Please click on the window to capture."
309     xwd  | convert - ${name}
310     \ls -l ${name}
311 }
312
313 # Create and CD in a /tmp/tmp.XXXXXX directory. With the '-'
314 # arguments, do not create one and CD in the most recent instead
315
316 function cdt () {
317     if [[ "$1" ]]; then
318         if [[ "$1" == "-" ]]; then
319             cd $(\ls -td /tmp/tmp.?????? | head -1)
320         else
321             echo "USAGE: cdt [-]" >&2
322             return 1
323         fi
324     else
325         dir=$(mktemp -d /tmp/tmp.XXXXXX)
326         link=/tmp/tmp
327         if [[ -h ${link} ]]; then
328             \rm ${link}
329         fi
330         if [[ ! -a ${link} ]]; then
331             ln -s ${dir} ${link}
332         fi
333         cd ${dir}
334     fi
335 }
336
337 alias t='cd /tmp'
338 alias a='cd /tmp/at'
339
340 function trash () {
341     TRASH=$(date +/tmp/trash-%Y-%b-%d-%Hh)
342     LINK=/tmp/trash
343
344     mkdir -p ${TRASH}
345
346     [[ -h ${LINK} ]] && \rm ${LINK}
347
348     [[ ! -f ${LINK} ]] && ln -s ${TRASH} ${LINK}
349
350     mv "$@" ${TRASH}
351     echo "Trashed $@"
352 }
353
354 ######################################################################
355 ## A version of pho which stores the image numbers in environment
356 ## variables
357
358 function pho () {
359     TEMP=$(mktemp /tmp/pho.XXXXXXX)
360     $(which pho) "$@" | tee ${TEMP}
361     PHO_NOTE_1=$(grep ^"Note 1: " ${TEMP} | sed -e "s/^[^:]*: //")
362     PHO_NOTE_2=$(grep ^"Note 2: " ${TEMP} | sed -e "s/^[^:]*: //")
363     PHO_NOTE_3=$(grep ^"Note 3: " ${TEMP} | sed -e "s/^[^:]*: //")
364     PHO_NOTE_R90=$(grep ^"Rotate 90 \(CW\): " ${TEMP} | sed -e "s/^[^:]*: //")
365     PHO_NOTE_R180=$(grep ^"Rotate 180: " ${TEMP} | sed -e "s/^[^:]*: //")
366     PHO_NOTE_R270=$(grep ^"Rotate -90 \(CCW\): " ${TEMP} | sed -e "s/^[^:]*: //")
367     [[ "${PHO_NOTE_1}" ]] || unset PHO_NOTE_1
368     [[ "${PHO_NOTE_2}" ]] || unset PHO_NOTE_2
369     [[ "${PHO_NOTE_3}" ]] || unset PHO_NOTE_3
370     [[ "${PHO_NOTE_R90}" ]] || unset PHO_NOTE_R90
371     [[ "${PHO_NOTE_R180}" ]] || unset PHO_NOTE_R180
372     [[ "${PHO_NOTE_R270}" ]] || unset PHO_NOTE_R270
373     \rm ${TEMP}
374 }
375
376 ######################################################################
377 ## A version of date that shows the time at home if TZ is set
378
379 function dt () {
380     echo "Local - $(date)"
381     unset TZ
382     echo "Home  - $(date)"
383 }
384
385 ######################################################################
386 ## ifup / ifdown with sudo and memorization of the network
387
388 ## When invoked without an argument netup uses the same argument as
389 ## the previous time
390
391 ## When invoked without an argument netdown removes the last interface
392 ## which was netuped
393
394 [ ${NETUP_HISTORY} ] || NETUP_HISTORY="${HOME}/.netup_history"
395
396 function netup () {
397     local upped_wifi
398
399     [[ "${WIFI_INTERFACE}" ]] || WIFI_INTERFACE=wlan0
400
401     if [[ "$1" == "--scan" ]]; then
402         if ifconfig -s | grep -v -q ${WIFI_INTERFACE}; then
403             sudo ifconfig ${WIFI_INTERFACE} up
404             upped_wifi=1
405         fi
406
407         sudo iwlist ${WIFI_INTERFACE} scan | \grep -E 'ESS|Quali|Encry' | sed -e 's/^[ \t]*//'
408
409         if [[ ${upped_wifi} ]]; then
410             sudo ifconfig ${WIFI_INTERFACE} down
411             unset upped_wifi
412         fi
413
414         return 0
415     fi
416
417     if \ifconfig -s | grep -q -v ^'(Iface|lo) '; then
418         echo "There is/are already interface(s) up." >&2
419         return 1
420     fi
421
422     if \ps h -C dhclient | grep -q .; then
423         echo "There is already a dhcp client running." >&2
424         return 1
425     fi
426
427     if \ps h -C wpa_supplicant | grep -q .; then
428         echo "There is already a wpa_supplicant running." >&2
429         return 1
430     fi
431
432     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
433         echo "netup <interface>" >&2
434         return 1
435         # # If we have no argument and there is a .netup_history, use it
436         # ARGS=$(cat ${NETUP_HISTORY})
437     else
438         # Otherwise uses the given arguments, and store them
439         ARGS="$@"
440         echo ${ARGS} > ${NETUP_HISTORY}
441     fi
442
443     if [[ "${PRIVATE_INTERFACE_DEFINITION}" ]]; then
444         ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
445     fi
446
447     echo "Executing ${VT_GREEN_FG}[sudo ifup ${ARGS}]${VT_RESET}"
448     sudo ifup ${ARGS}
449
450     # Ugly hack to remove the dsl modem dns server when we add
451     # explicitely a dns in the /etc/network/interfaces
452
453     REMOVE_LOCAL_DNS=/usr/local/bin/remove-local-dns.sh
454     # REMOVE_LOCAL_DNS=${HOME}/sources/scripts/remove-local-dns.sh
455
456     if [[ -x ${REMOVE_LOCAL_DNS} ]]; then
457         echo "Executing ${VT_GREEN_FG}[sudo ${REMOVE_LOCAL_DNS} 192.168]${VT_RESET}"
458         sudo ${REMOVE_LOCAL_DNS} 192.168
459     fi
460 }
461
462 function netdown () {
463     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
464         # If there are no arguments and there is a .netup_history, get the
465         # interface from it
466         ARGS=$(tail -1 ${NETUP_HISTORY} | sed -e "s/=.*$//")
467     else
468         # Otherwise, use the standard ifdown
469         ARGS="$@"
470     fi
471
472     [[ "${PRIVATE_INTERFACE_DEFINITION}" ]] && ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
473
474     echo "Executing sudo ${VT_GREEN_FG}[ifdown ${ARGS}]${VT_RESET}"
475     sudo ifdown ${ARGS}
476
477     # if [[ $(\ps -C dhclient | tail -n +2) ]]; then
478     # echo "There is still a dhcp client running." >&2
479     # return 1
480     # fi
481
482     # if [[ $(\ps -C wpa_supplicant | tail -n +2) ]]; then
483     # echo "There is still a wpa_supplicant running." >&2
484     # return 1
485     # fi
486 }
487
488 function checkgw () {
489     GW=$(route -n | grep ^0.0.0.0 | awk '{print $2}')
490     if [[ "${GW}" ]]; then
491         ping ${GW}
492     else
493         echo "Can not find a getaway." >&2
494         return 1
495     fi
496 }
497
498 ######################################################################
499 # Show the most recent files, no scroll
500
501 function lr () {
502     HEIGHT=$(stty size | awk '{print $1}')
503     WIDTH=$(stty size | awk '{print $2}')
504     \ls -goth --time-style="+%Y %b %d %H:%M" "$@" | \
505         head -$((HEIGHT-2)) | \
506         cut -b1-${WIDTH}
507 }
508
509 ######################################################################
510 # You can change the xterm background color on the fly!
511
512 function setxtermbg () {
513     echo -n $'\e]11;'$1$'\007'
514 }
515
516 ######################################################################
517 # Shuffle the lines from the stdin
518
519 function shuffle () {
520     SEED=$1
521     [[ $SEED ]] || SEED=0
522     awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
523 }
524
525 ######################################################################
526 # Stores the last entered command into a file
527
528 KEPT_COMMANDS=${HOME}/.kept_bash_commands
529
530 function keep () {
531     if [[ ${KEPT_COMMANDS} ]]; then
532         TOKEEP=$(mktemp /tmp/keep.XXXXXX)
533         if [[ "$@" ]]; then
534             echo "$@" > ${TOKEEP}
535         else
536             selector -b -i -d -l ${HISTSIZE} -o "${TOKEEP}" <(history)
537         fi
538
539         if [[ -s "${TOKEEP}" ]]; then
540             echo $(date)": "$(cat ${TOKEEP}) >> ${KEPT_COMMANDS}
541             cat "${TOKEEP}"
542         else
543             echo "No command stored!"
544         fi
545
546         \rm ${TOKEEP}
547     else
548         echo "You have to set \$KEPT_COMMANDS"
549     fi
550 }
551
552 ######################################################################
553 # I sometime burn CDs and DVDs
554
555 function burn () {
556     [[ "${DEV_BURNER}" ]] || DEV_BURNER="/dev/sr0"
557     if [[ ! "$1" ]]; then
558         echo "burn <iso name | dirname>" >&2
559     elif [[ -f "$1" ]]; then
560         if [[ $(file "$1" | \grep -E 'ISO 9660|UDF filesystem data') ]]; then
561             wodim -eject -v dev=${DEV_BURNER} "$1"
562         else
563             echo "Do not know what to do with $1" >&2
564         fi
565     elif [[ -d "$1" ]]; then
566         [[ "${TMP_ROOT}" ]] || TMP_ROOT=/tmp/
567         echo "Using ${TMP_ROOT} as temporary directory."
568         TMP=$(mktemp ${TMP_ROOT}/cdimage.XXXXXX) && \
569             genisoimage -input-charset iso8859-1 -r -o ${TMP} "$1" && \
570             wodim -eject -v dev=${DEV_BURNER} ${TMP}
571         rm -f ${TMP}
572     else
573         echo "Can not find $1" >&2
574     fi
575 }
576
577 ######################################################################
578 # And watch DVDs too!
579
580 function dvd () {
581
582     echo
583     echo " ! @   Seek to the beginning of the previous/next chapter"
584     echo " j     Cycle through the available subtitles"
585     echo " o     Show/hide the timing"
586     echo " x z   Subtitle delay"
587     echo " / *   Volume"
588     echo
589
590     if [[ "$1" ]]; then
591         dvd_device="$1"
592         shift
593     else
594         dvd_device="/dev/dvd"
595     fi
596
597     title="1"
598
599     if [[ "$1" ]]; then
600         title=$1
601         shift
602     fi
603
604     # -vc ffmpeg12 -vf yadif
605
606     mplayer > /dev/null \
607         -stop-xscreensaver \
608         -quiet \
609         -alang en -slang en \
610         -softvol -softvol-max 1000 \
611         -dvd-device ${dvd_device} ${MPLAYER_OPTIONS} dvd://${title}
612 }
613
614 function ripdvd () {
615     if [[ -e "/dev/dvd" ]]; then
616         DVD_DEVICE="/dev/dvd"
617     elif [[ -e "/dev/dvd3" ]]; then
618         DVD_DEVICE="/dev/dvd3"
619     else
620         echo "Can not find the dvd device." >&2
621         return 1
622     fi
623
624     echo "Attemptin to rip from ${DVD_DEVICE}."
625
626     mkdir -p ${HOME}/dvds
627
628     cd ${HOME}/dvds
629     time dvdbackup -i ${DVD_DEVICE} -v -M "$@" && eject
630 }
631
632 alias ripcd=abcde
633
634 ######################################################################
635 # Create small images from images
636
637 function mksmall () {
638
639     if [[ "$1" == "--params" ]]; then
640         shift
641         PARAMS="$1"
642         shift
643     fi
644
645     [[ "${PARAMS}" ]] || PARAMS="-geometry 1280x1024"
646
647     # Auto-orient does not seem to work at all, hence the ugly hack
648     # with exif below
649
650     # PARAMS="-auto-orient -geometry 800x600"
651
652     echo "Using ${PARAMS}"
653
654     DEST_DIR=$1
655
656     [[ ${DEST_DIR} ]] || DEST_DIR=./small
657
658     mkdir -p ${DEST_DIR}
659
660     if [[ ! -d ${DEST_DIR} ]]; then
661         echo "Can not create ${DEST_DIR}" >&2
662         return
663     fi
664
665     NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
666     NB=0
667
668     for i in $(find -maxdepth 1 -type f); do
669         if [[ $(file $i | grep image) ]]; then
670             if [[ -e ${DEST_DIR}/$i ]]; then
671                 echo "The file ${DEST_DIR}/$i already exists."
672             else
673                 if [[ $(file ${i/%.*/}.* | grep -E movie) ]] ; then
674                     CAPTION_PARAMS="-font FreeSans-Bold -pointsize 32 -fill green -annotate +10+32 Video"
675                 else
676                     CAPTION_PARAMS=""
677                 fi
678
679                 convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
680             fi
681
682             \ls -lt ${DEST_DIR}/$i
683         fi
684
685         NB=$((NB+1))
686
687         echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
688     done
689 }
690
691 ######################################################################
692 # Move a file to the ~/sources/config directory and replace it where
693 # it was by a symbolic link
694
695 function mvtoconfig () {
696     CONFIGDIR=${HOME}/sources/config
697     if [[ -d ${CONFIGDIR} ]]; then
698         NEWNAME=${CONFIGDIR}/$(basename "$1" | sed -e "s/^\.//")
699         mv "$1" $NEWNAME
700         ln -s $NEWNAME $1
701     else
702         echo "Can not find ${CONFIGDIR}"
703     fi
704 }
705
706 ######################################################################
707 # The complex prompt policy
708
709 export PS1
710
711 if [[ "${CONSOLE}" == "yes" ]]; then
712     PS1=""
713 else
714
715     # If the login is a standard one (as specified in
716     # IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
717     # show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
718
719     if [[ ! ${IGNORED_PROMPT_LOGIN} ]] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]; then
720         IDENT="${USER}"
721     fi
722
723     # If the display is not the main one, make the assumption that the
724     # shell is not running on the localhost, and show the hostname
725
726     [[ "${DISPLAY}" != ":0.0" ]] && IDENT="${IDENT}@\h"
727
728     # If there is the login or the hostname, add a ":" to the prompt
729
730     [[ "${IDENT}" ]] && IDENT="${IDENT}:"
731
732     if [[ ${USER} == "root" ]]; then
733         # If we are root, show that in red
734         PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
735     else
736         if [[ $(\grep QEMU /proc/cpuinfo) ]]; then
737             # If we are in a qemu virtual machine, in yellow
738             PS1="\[${VT_YELLOW_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
739         else
740             # Otherwise in white
741             PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
742         fi
743     fi
744
745 fi
746
747 ######################################################################
748 # This implements a local history. If we are in a directory containing
749 # a writable local history file, we add the last line of the global
750 # history to it.
751
752 LOCAL_HISTORY_FILE=".local_bash_history"
753
754 function keep_local_history () {
755     if [[ -w "${LOCAL_HISTORY_FILE}" ]]; then
756         history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
757         TMP=$(mktemp /tmp/lh.XXXXXX)
758         \chmod 600 ${TMP}
759         uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
760         # mv would replace a symbolic link, while cp keeps it
761         \cp ${TMP} ${LOCAL_HISTORY_FILE}
762         \rm ${TMP}
763         LOCAL_HISTORY_HINT="* "
764     else
765         LOCAL_HISTORY_HINT=""
766     fi
767 }
768
769 PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
770
771 ######################################################################
772 # Show the history path if it is unusual
773
774 function histfile_cue () {
775     if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]; then
776         HISTORY_CUE="[${HISTFILE}]"
777     else
778         HISTORY_CUE=""
779     fi
780 }
781
782 PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
783
784 ######################################################################
785 # The dus command is available on my web site
786 #
787 # git clone http://fleuret.org/git/dus/
788
789 alias dus='dus -f -i'
790
791 ######################################################################
792 # The finddup command is available on my web site
793 #
794 # git clone http://fleuret.org/git/finddup/
795
796 alias finddup='finddup -p'
797
798 ######################################################################
799 # The selector command is available on my web site
800 #
801 # git clone http://fleuret.org/git/selector/
802
803 export SELECTOR_CD_HISTORY_SIZE=10000
804
805 source bash-selector.sh --hist --cd
806
807 ######################################################################
808 # And we avoid to put in the history the use of the selector, which we
809 # do too often
810
811 HISTIGNORE="${HISTIGNORE}:selector-history"
812
813 ######################################################################
814
815 function selector-printer () {
816     TMP=$(mktemp /tmp/selector-printer.XXXXXX)
817     selector -o ${TMP} <(lpstat -a | awk '{print $1}')
818     export PRINTER=$(cat ${TMP})
819     echo "PRINTER=${PRINTER}"
820     rm -f ${TMP}
821     lpq
822 }
823
824 ######################################################################
825 # A ls with memory to notice what files have been added/removed from
826 # the current directory
827
828 function lsn () {
829     [[ "${LSN_STATE_FILE}" ]] || LSN_STATE_FILE=".lsn-state"
830
831     if  [[ $1 == "--mem" ]] || [[ "$1" == "-m" ]]; then
832
833         \ls -pa | sort > "${LSN_STATE_FILE}"
834         echo "State updated." >&2
835
836     elif [[ $1 == "+" ]]; then
837
838         shift
839
840         TMP=$(mktemp /tmp/lsn.XXXXXX)
841         \ls -d "$@" > ${TMP}
842         cat "${LSN_STATE_FILE}" >> ${TMP}
843         sort ${TMP} | uniq -u > "${LSN_STATE_FILE}"
844         \rm ${TMP}
845
846     elif [[ "$1" ]]; then
847         cat >&2 <<EOF
848 lsn [--help|-h] [--mem|-m] [+ <file> ...]
849
850 Where
851
852   --help|-h
853      prints this help
854
855   --mem|-m
856      stores the current state of the directory
857
858   + <file> [<file> ...]
859      switches the presence/absence of the indicated files in the
860      stored state
861
862 EOF
863         if [[ ! "$1" == "-h" ]] && [[ ! "$1" == "--help" ]]; then
864             echo >&2 "Unknown option \`\`$1''."
865             return 1
866         fi
867
868     else
869
870         if [[ -f "${LSN_STATE_FILE}" ]]; then
871             TMP=$(mktemp /tmp/lsn.XXXXXX)
872             \ls -pa | sort > ${TMP}
873             if diff > /dev/null ${TMP} "${LSN_STATE_FILE}"; then
874                 echo "${VT_GREEN_FG}${VT_BOLD}No change (since $(date +"%b %d, %Y" -r "${LSN_STATE_FILE}"))${VT_RESET}"
875             else
876                 \comm -1 -3 "${LSN_STATE_FILE}" ${TMP}
877                 \comm -2 -3 "${LSN_STATE_FILE}" ${TMP} | while read line; do
878                     echo "${VT_RED_FG}${VT_BOLD}${line}${VT_RESET} (missing)"
879                 done
880             fi
881             \rm ${TMP}
882         else
883             echo "${VT_RED_FG}${VT_BOLD}No lsn state here.${VT_RESET}" >&2
884             return 1
885         fi
886
887     fi
888 }
889
890 ######################################################################
891
892 function prompt_command () {
893     # if [[ $(umask) != 0022 ]]; then
894         # echo "Umask changed to $(umask)"
895     # fi
896     # save the history after every command to avoid loosing some when
897     # multiple shells are open
898     history -a
899     # load the saved history
900     history -n
901     # and the local histories system defined above
902     keep_local_history
903     # and the history cue
904     histfile_cue
905 }
906
907 PROMPT_COMMAND="prompt_command"
908
909 ######################################################################
910
911 # Displaying the timezone if it is set
912
913 [[ ${TZ} ]] && echo "${VT_BLUE_FG}Time zone is ${TZ}.${VT_RESET}"
914
915 ######################################################################