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 ######################################################################
58 ## The interactive part
59
60 export VT_RESET=$'\e[0m'
61 export VT_BOLD=$'\e[1m'
62 export VT_UNDERLINE=$'\e[4m'
63 export VT_BLINK=$'\e[5m'
64
65 export VT_SET_TITLE=$'\e]0;'
66 export VT_END_TITLE=$'\007'
67
68 export VT_BLACK_FG=$'\e[30m'
69 export VT_RED_FG=$'\e[31m'
70 export VT_GREEN_FG=$'\e[32m'
71 export VT_YELLOW_FG=$'\e[33m'
72 export VT_BLUE_FG=$'\e[34m'
73 export VT_MAGENTA_FG=$'\e[35m'
74 export VT_CYAN_FG=$'\e[36m'
75 export VT_WHITE_FG=$'\e[37m'
76
77 export VT_BLACK_BG=$'\e[40m'
78 export VT_RED_BG=$'\e[41m'
79 export VT_GREEN_BG=$'\e[42m'
80 export VT_YELLOW_BG=$'\e[43m'
81 export VT_BLUE_BG=$'\e[44m'
82 export VT_MAGENTA_BG=$'\e[45m'
83 export VT_CYAN_BG=$'\e[46m'
84 export VT_WHITE_BG=$'\e[47m'
85
86 # Colorize man pages!
87
88 export LESS_TERMCAP_us=${VT_GREEN_FG}
89 export LESS_TERMCAP_ue=${VT_RESET}
90 export LESS_TERMCAP_md=${VT_BLUE_FG}${VT_BOLD}
91 export LESS_TERMCAP_me=${VT_RESET}
92
93 # export LESS_TERMCAP_md=$'\e[1;34;40m'
94
95 # This prevents ^S from freezing the shell
96
97 # stty -ixon
98
99 ulimit -c unlimited
100
101 alias ..='cd ..'
102 alias -- -='cd -'
103 alias rm='rm -i'
104 alias mv='mv -i'
105 # alias chmod='chmod -v'
106 alias chmod='chmod -c'
107 alias cp='cp -i'
108 alias rd=rmdir
109 alias md='mkdir -v'
110 alias ps='ps uxaf'
111 alias df='df -hT --sync'
112 alias grep='grep -i -E --mmap --color=auto'
113 alias find='ionice -c3 find'
114 alias pd=pushd
115 alias val='valgrind --leak-check=full --show-reachable=yes --db-attach=yes '
116
117 alias s='screen -d -R -U && clear'
118
119 function nh () {
120     export HISTFILE=/dev/null
121     unalias cd
122 }
123
124 function ding () {
125     if [[ $1 && $(which winshepherd.sh) ]]; then
126         play -q ~/local/sounds/deskbell.wav &
127         unset E_APP_WINDOW
128         winshepherd.sh message green "$1"
129     else
130         play -q ~/local/sounds/deskbell.wav
131     fi
132 }
133
134 # alias kj="keyjnote -s -D 1000 -t Crossfade -T 100"
135 alias impressive="impressive -s -D 1000 -t Crossfade -T 100"
136
137 # alias fdupes='fdupes -r .'
138
139 # ls colors
140
141 if [ -e "${HOME}/.dircolors" ]; then
142     eval $(dircolors "${HOME}/.dircolors")
143     alias ls='ls --group-directories-first --color'
144     alias lt='ls --color -gohtr --time-style="+%Y %b %d %H:%M"'
145     alias ll='ls --color -goh --time-style="+%Y %b %d %H:%M"'
146     alias lll='ls --color -lth'
147     alias l='ls --color -I "*~" -I "*.o"'
148     alias less='less -R'
149 else
150     alias ls='ls --group-directories-first'
151     alias lt='ls -gohtr --time-style="+%Y %b %d %H:%M"'
152     alias ll='ls -goh --time-style="+%Y %b %d %H:%M"'
153     alias lll='ls -lth'
154     alias l='ls -I "*~" -I "*.o"'
155 fi
156
157 export EDITOR=emacsclient
158 export GIT_EDITOR=${EDITOR}
159
160 ######################################################################
161 # Ignored extensions when completing
162
163 # export FIGNORE="CVS"
164
165 function latexdiff () {
166     # wdiff -n \
167         # -w $'\033[30;43m'"[$1]"$'\033[30;41m ' -x $'\033[0m' \
168         # -y $'\033[30;43m'"[$2]"$'\033[30;42m ' -z $'\033[0m' \
169         # $*
170
171     wdiff -n \
172         -w $'\033[30;41m' -x $'\033[0m' \
173         -y $'\033[30;42m' -z $'\033[0m' \
174         $*
175
176  # | \grep $'\033'"\|section{"
177 }
178
179 ######################################################################
180 # Functions
181
182 # Find a file whose name contains a substring
183
184 function fn () {
185     name=$1
186     shift
187     find "$@" -name "*${name}*";
188 }
189
190 function bak () {
191     while [[ "$1" ]]; do
192         cp "$1" "$1".bak
193         shift
194     done
195 }
196
197 function ua () {
198     while [[ "$1" ]]; do
199
200         case "$1" in
201
202             *.tgz|*.tar.gz|*.tbz|*.tar.bz2)
203                 tar xvf "$1"
204                 ;;
205
206             *.rar)
207                 unrar x "$1"
208                 ;;
209
210             *.zip)
211                 unzip "$1"
212                 ;;
213
214             *)
215                 echo "Unknown file extension $1"
216                 ;;
217         esac
218
219         shift
220
221     done
222 }
223
224 # Create a dir and cd there
225
226 function mcd () {
227     mkdir -vp "$1"
228     cd "$1"
229 }
230
231 # Capture the screen in a dated png
232
233 function cap () {
234     if [[ $2 ]]; then
235         name=$2
236     else
237         name="capture-$(date +%s).png"
238     fi
239     echo "Waiting $1 s and saving to ${name}."
240     [[ "$1" ]] && sleep "$1"
241     echo "Please click on the window to capture."
242     xwd  | convert - ${name}
243     \ls -l ${name}
244 }
245
246 # Create and CD in a /tmp/tmp.XXXXXX directory. With the '-'
247 # arguments, do not create one and CD in the most recent instead
248
249 function cdt () {
250     if [[ "$1" ]]; then
251         if [[ "$1" == "-" ]]; then
252             cd $(\ls -td /tmp/tmp.?????? | head -1)
253         else
254             echo "USAGE: cdt [-]" >&2
255             return 1
256         fi
257     else
258         cd $(mktemp -d /tmp/tmp.XXXXXX)
259     fi
260 }
261
262 alias t='cd /tmp'
263
264 function trash () {
265     TRASH=$(date +/tmp/trash-%Y-%b-%d-%Hh)
266
267     if [[ -d ${TRASH} ]]; then
268         echo "Re-use ${TRASH}"
269     else
270         mkdir ${TRASH}
271         echo "Created ${TRASH}"
272     fi
273
274     mv "$@" ${TRASH}
275     echo "Trashed $@"
276 }
277
278 # alias trash=trash.sh
279
280 function mmsget () {
281     mplayer "$1" -dumpstream -dumpfile $(basename "$1")
282 }
283
284 function quicktex () {
285     if [[ $1 ]]; then
286         MAIN=$1
287     else
288         MAIN=$(\ls -t *.tex | head -1 | sed -r -e 's/\.tex//')
289     fi
290     pdflatex ${MAIN}
291     bibtex ${MAIN}
292     pdflatex ${MAIN}
293     pdflatex ${MAIN}
294     xpdf ${MAIN}.pdf
295 }
296
297 ######################################################################
298 # http://www.reddit.com/r/linux/comments/akt3j/a_functional_programming_style_map_function_for/
299
300 function map () {
301     local command i rep
302     if [ $# -lt 2 ] || [[ ! "$@" =~ :[[:space:]] ]];then
303         echo "Invalid syntax." >&2; return 1
304     fi
305     until [[ "$1" =~ : ]]; do
306         command="$command $1"; shift
307     done
308     command="$command ${1%:}"; shift
309     for i in "$@"; do
310         if [[ $command =~ \{\} ]];then
311             rep="${command//\{\}/\"$i\"}"
312             eval "${rep//\\/\\\\}"
313         else
314             eval "${command//\\/\\\\} \"${i//\\/\\\\}\""
315         fi
316     done
317 }
318
319 ######################################################################
320 ## A version of pho which stores the image numbers in environment
321 ## variables
322
323 function pho () {
324     TEMP=$(mktemp /tmp/pho.XXXXXXX)
325     $(which pho) "$@" | tee ${TEMP}
326     PHO_NOTE_1=$(grep ^"Note 1: " ${TEMP} | sed -e "s/^[^:]*: //")
327     PHO_NOTE_2=$(grep ^"Note 2: " ${TEMP} | sed -e "s/^[^:]*: //")
328     PHO_NOTE_3=$(grep ^"Note 3: " ${TEMP} | sed -e "s/^[^:]*: //")
329     PHO_NOTE_R90=$(grep ^"Rotate 90 \(CW\): " ${TEMP} | sed -e "s/^[^:]*: //")
330     PHO_NOTE_R180=$(grep ^"Rotate 180: " ${TEMP} | sed -e "s/^[^:]*: //")
331     PHO_NOTE_R270=$(grep ^"Rotate -90 \(CCW\): " ${TEMP} | sed -e "s/^[^:]*: //")
332     [[ "${PHO_NOTE_1}" ]] || unset PHO_NOTE_1
333     [[ "${PHO_NOTE_2}" ]] || unset PHO_NOTE_2
334     [[ "${PHO_NOTE_3}" ]] || unset PHO_NOTE_3
335     [[ "${PHO_NOTE_R90}" ]] || unset PHO_NOTE_R90
336     [[ "${PHO_NOTE_R180}" ]] || unset PHO_NOTE_R180
337     [[ "${PHO_NOTE_R270}" ]] || unset PHO_NOTE_R270
338     \rm ${TEMP}
339 }
340
341 # function rotjpeg () {
342     # if [ "$1" == "90" ] || [ "$1" == "180" ] || [ "$1" == "270" ]; then
343         # TEMP=$(mktemp /tmp/rotjpeg.XXXXXX)
344         # echo jpegtran -rotate "$1" -copy all $2 > ${TEMP}
345         # echo cp $2 ${2/jpg/}original.jpg
346         # echo cp ${TEMP} $2
347         # rm ${TEMP}
348     # else
349         # echo "Can not rotate with an angle of "$1" degrees."
350     # fi
351 # }
352
353 ######################################################################
354 ## A version of date that shows the time at home if TZ is set
355
356 function dt () {
357     echo "Local: $(date)"
358     if [[ ${TZ} ]]; then
359         unset TZ
360         echo "Home:  $(date)"
361     fi
362 }
363
364 ######################################################################
365 ## ifup / ifdown with sudo and memorization of the network
366
367 ## When invoked without an argument netup uses the same argument as
368 ## the previous time
369
370 ## When invoked without an argument netdown removes the last interface
371 ## which was netuped
372
373 [[ ${NETUP_HISTORY} ]] || NETUP_HISTORY="${HOME}/.netup_history"
374
375 function netup () {
376     if [[ $(\ifconfig -s | grep -v ^"(Iface|lo) ") ]]; then
377         echo "There is already interface(s) up." >&2
378         return 1
379     fi
380
381     if [[ $(\ps -C dhclient | tail -n +2) ]]; then
382         echo "There is already a dhcp client running." >&2
383         return 1
384     fi
385
386     if [[ $(\ps -C wpa_supplicant | tail -n +2) ]]; then
387         echo "There is already a wpa_supplicant running." >&2
388         return 1
389     fi
390
391     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
392         echo "netup <interface>" >&2
393         return 1
394         # # If we have no argument and there is a .netup_history, use it
395         # ARGS=$(cat ${NETUP_HISTORY})
396     else
397         # Otherwise uses the given arguments, and store them
398         ARGS="$@"
399         echo ${ARGS} > ${NETUP_HISTORY}
400     fi
401
402     if [[ -n "${PRIVATE_INTERFACE_DEFINITION}" ]]; then
403         ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
404     fi
405
406     echo "Executing ${VT_GREEN_FG}[sudo ifup ${ARGS}]${VT_RESET}"
407     sudo ifup ${ARGS}
408
409     # Ugly hack to remove the dsl modem dns server when we add
410     # explicitely a dns in the /etc/network/interfaces
411
412     REMOVE_LOCAL_DNS=/usr/local/bin/remove-local-dns.sh
413
414     if [[ -x ${REMOVE_LOCAL_DNS} ]]; then
415         echo "Executing ${VT_GREEN_FG}[sudo ${REMOVE_LOCAL_DNS} 192.168]${VT_RESET}"
416         sudo ${REMOVE_LOCAL_DNS} 192.168
417     fi
418 }
419
420 function netdown () {
421     if [[ ! "$@" ]] && [[ -s ${NETUP_HISTORY} ]]; then
422         # If there are no arguments and there is a .netup_history, get the
423         # interface from it
424         ARGS=$(tail -1 ${NETUP_HISTORY} | sed -e "s/=.*$//")
425     else
426         # Otherwise, use the standard ifdown
427         ARGS="$@"
428     fi
429
430     if [[ -n "${PRIVATE_INTERFACE_DEFINITION}" ]]; then
431         ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
432     fi
433
434     echo "Executing sudo ${VT_GREEN_FG}[ifdown ${ARGS}]${VT_RESET}"
435     sudo ifdown ${ARGS}
436 }
437
438 function checkgw () {
439     GW=$(route -n | grep ^0.0.0.0 | awk '{print $2}')
440     if [[ -n "${GW}" ]]; then
441         ping ${GW}
442     else
443         echo "Can not find a getaway." >&2
444         return 1
445     fi
446 }
447
448 ######################################################################
449 # Show the most recent files, no scroll
450
451 function lr () {
452     HEIGHT=$(stty size | awk '{print $1}')
453     WIDTH=$(stty size | awk '{print $2}')
454     \ls -goth --time-style="+%Y %b %d %H:%M" "$@" | \
455         head -$((HEIGHT-2)) | \
456         cut -b1-${WIDTH}
457 }
458
459 ######################################################################
460 # cd and ls into a directory
461 # [from http://www.oreillynet.com/onlamp/blog/2007/01/whats_in_your_bash_history.html]
462
463 # function c () { cd "$@" && lr; }
464
465 ######################################################################
466 # You can change the xterm background color on the fly!
467
468 function setxtermbg () {
469     echo -n $'\e]11;'$1$'\007'
470 }
471
472 ######################################################################
473 # Shuffle the lines from the stdin
474
475 function shuffle () {
476     SEED=$1
477     [[ $SEED ]] || SEED=0
478     awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
479 }
480
481 ######################################################################
482 # Stores the last entered command into a file
483
484 KEPT_COMMANDS=${HOME}/.kept_bash_commands
485
486 function keep () {
487     if [[ ${KEPT_COMMANDS} ]]; then
488         LINE="$*"
489         [[ "${LINE}" ]] || LINE=$(history | tail -2 | head -1 | sed -e "s/^[0-9 ]*//")
490         echo $LINE
491         echo $(date)": "${LINE} >> ${KEPT_COMMANDS}
492     else
493         echo "You have to set \$KEPT_COMMANDS"
494     fi
495 }
496
497 ######################################################################
498 # I sometime burn CDs and DVDs
499
500 function burn () {
501     DEVICE="/dev/cdrw"
502     if [[ ! "$1" ]]; then
503         echo "burn <iso name | dirname>" >&2
504     elif [[ -f "$1" ]]; then
505         if [[ $(file "$1" | grep "ISO 9660") ]]; then
506             wodim -eject -v dev=${DEVICE} "$1"
507         else
508             echo "Unknown type of $1" >&2
509         fi
510     elif [[ -d "$1" ]]; then
511         [[ "${TMP_ROOT}" ]] || TMP_ROOT=/tmp/
512         TMP=$(mktemp ${TMP_ROOT}/cdimage.XXXXXX) && \
513             genisoimage -input-charset iso8859-1 -r -o ${TMP} "$1" && \
514             wodim -eject -v dev=${DEVICE} ${TMP}
515         rm -f ${TMP}
516     else
517         echo "Can not find $1" >&2
518     fi
519 }
520
521 ######################################################################
522 # And watch DVDs too!
523
524 function dvd () {
525
526     echo
527     echo " ! @   Seek to the beginning of the previous/next chapter"
528     echo " j     Cycle through the available subtitles"
529     echo " o     Show/hide the timing"
530     echo " x z   Subtitle delay"
531     echo " / *   Volume"
532     echo
533
534     if [[ "$1" ]]; then
535         dvd_device="$1"
536         shift
537     else
538         dvd_device="/dev/dvd"
539     fi
540
541     title="1"
542
543     if [[ "$1" ]]; then
544         title=$1
545         shift
546     fi
547
548     # -vc ffmpeg12 -vf yadif
549
550     mplayer > /dev/null \
551         -stop-xscreensaver \
552         -quiet \
553         -alang en -slang en \
554         -softvol -softvol-max 1000 \
555         -dvd-device ${dvd_device} $* dvd://${title}
556 }
557
558 function ripdvd () {
559     if [[ -e "/dev/dvd" ]]; then
560         DVD_DEVICE="/dev/dvd"
561     elif [[ -e "/dev/dvd3" ]]; then
562         DVD_DEVICE="/dev/dvd3"
563     else
564         echo "Can not find the dvd device." >&2
565         return 1
566     fi
567
568     echo "Attemptin to rip from ${DVD_DEVICE}."
569
570     mkdir -p ${HOME}/dvds
571
572     cd ${HOME}/dvds
573     time dvdbackup -i ${DVD_DEVICE} -v -M $* && eject
574 }
575
576 alias ripcd=abcde
577
578 ######################################################################
579 # Upload the sources from the current directory to work
580
581 function ulsrc () {
582     if [[ ! "${MY_WORK_MACHINE}" ]]; then
583         echo "\$MY_WORK_MACHINE undefined" 1>&2
584         return 1
585     fi
586
587     DIR=${PWD/$HOME\//}
588
589     scp {Makefile,*.{cc,h,sh}} ${MY_WORK_MACHINE}:${DIR}
590
591     echo "Uploaded to ${MY_WORK_MACHINE}:${DIR}/"
592 }
593
594 ######################################################################
595 # Create small images from images
596
597 function mksmall () {
598
599     PARAMS="-geometry 800x600"
600
601     # Auto-orient does not seem to work at all, hence the ugly hack
602     # with exif below
603
604     # PARAMS="-auto-orient -geometry 800x600"
605
606     echo "Using ${PARAMS}"
607
608     DEST_DIR=$1
609
610     [[ ${DEST_DIR} ]] || DEST_DIR=./small
611
612     mkdir -p ${DEST_DIR}
613
614     if [[ ! -d ${DEST_DIR} ]]; then
615         echo "Can not create ${DEST_DIR}" >&2
616         return
617     fi
618
619     NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
620     NB=0
621
622     for i in $(find -maxdepth 1 -type f); do
623         if [[ $(file $i | grep image) ]]; then
624             if [[ -e ${DEST_DIR}/$i ]]; then
625                 echo "The file ${DEST_DIR}/$i already exists."
626             else
627
628                 # orientation=$(exif $i \
629                     # | grep ^Orientation \
630                     # | head -1 \
631                     # | sed -e "s/^[^|]*|//" \
632                     # | sed -e "s/ *$//")
633
634                 # case ${orientation} in
635                     # ""|"top - left")
636                         # rotation_cmd=""
637                         # ;;
638
639                     # "right - top")
640                         # rotation_cmd="-rotate 90"
641                         # ;;
642
643                     # "left - bottom")
644                         # rotation_cmd="-rotate 270"
645                         # ;;
646
647                     # *)
648                         # rotation_cmd=""
649                         # echo "Unknown orientation \"${orientation}\" !"
650                         # ;;
651                 # esac
652
653                 if [[ $(file ${i/%.*/}.* | grep -E movie) ]] ; then
654                     CAPTION_PARAMS="-font FreeSans-Bold -pointsize 32 -fill green -annotate +10+32 Video"
655                 else
656                     CAPTION_PARAMS=""
657                 fi
658
659                 convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
660             fi
661
662             \ls -lt ${DEST_DIR}/$i
663         fi
664
665         NB=$((NB+1))
666
667         echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
668     done
669 }
670
671 ######################################################################
672 # Move a file to the ~/sources/config directory and replace it where
673 # it was by a symbolic link
674
675 function mvtoconfig () {
676     CONFIGDIR=${HOME}/sources/config
677     if [[ -d ${CONFIGDIR} ]]; then
678         NEWNAME=${CONFIGDIR}/$(basename "$1" | sed -e "s/^\.//")
679         mv "$1" $NEWNAME
680         ln -s $NEWNAME $1
681     else
682         echo "Can not find ${CONFIGDIR}"
683     fi
684 }
685
686 ######################################################################
687 # Track uncommited files (I presume this is very ugly from a real git
688 # user perspective)
689
690 function git-fm () {
691     CURRENT_DIR=$(pwd)
692     NB_SUBDIR=0
693
694     for i in $(find -name ".git"); do
695         NB_SUBDIR=$((NB_SUBDIR+1))
696         cd ${CURRENT_DIR}/$(dirname $i)
697         NB_MODIFIED=$(git status | grep modified | wc -l)
698         if [[ ${NB_MODIFIED} -gt 0 ]]; then
699             echo "$(dirname $i) (${NB_MODIFIED})"
700             git status | grep modified \
701                 | sed -e "s/^#\t/    /" | sed -e "s/modified: *//"
702         fi
703     done
704
705     cd ${CURRENT_DIR}
706
707     echo "Visited ${NB_SUBDIR} directories."
708 }
709
710 ######################################################################
711 # Commits all directories under git
712
713 alias git-ca="echo Are you sure?"
714
715 function git-ca () {
716     ORIGINAL_PWD=${PWD}
717     UNCOMMITTED=""
718     for d in $(find ${PWD} -name ".git"  | sed -e "s/\.git$//"); do
719         cd $d
720         NB_MODIFIED=$(git status | grep modified | wc -l)
721         if [[ ${NB_MODIFIED} -gt 0 ]]; then
722             if [[ $(pwd) =~ ${NO_AUTOMATIC_GIT_COMMIT} ]]; then
723                 UNCOMMITTED="${UNCOMMITTED} $(pwd)"
724             else
725                 echo $(pwd)" (${NB_MODIFIED} modified file(s))"
726                 git commit -a -m "Automatic commit" | grep -v ^#
727             fi
728             # git gc
729         fi
730     done
731
732     cd ${ORIGINAL_PWD}
733
734     if [[ ${UNCOMMITTED} ]]; then
735         echo "** WARNING: Did not automatically commit${UNCOMMITTED}"
736     fi
737 }
738
739 ######################################################################
740 # Backups all git directories into an encrypted backup file located
741 # either on the usb key or the SD card (in that order) if they can be
742 # mounted.
743
744 function git-backup () {
745
746     BACKUPDIR=/mnt/key
747
748     mount ${BACKUPDIR} 2> /dev/null
749
750     if [[ ! $(mount | grep ${BACKUPDIR}) ]]; then
751         BACKUPDIR=/mnt/sd
752         mount ${BACKUPDIR}
753     fi
754
755     if [[ $(mount | grep ${BACKUPDIR}) ]]; then
756         echo "Mounted ${BACKUPDIR}"
757     else
758         echo "Could not mount the backup directory"
759         return 1
760     fi
761
762     RESULT=${BACKUPDIR}/gitbackup-$(date +%F-%H%M%S).tgz.mc
763
764     tar zcvf - $(find ${HOME}/ -name .git) \
765         | mcrypt -f ${HOME}/private/mcrypt.key > ${RESULT}
766
767     if [[ -f ${RESULT} ]]; then
768         ls -lh ${RESULT}
769     else
770         echo "Could not create the backup!"
771         return 1
772     fi
773
774     sync
775
776     umount ${BACKUPDIR} && echo "Umounted ${BACKUPDIR}"
777 }
778
779 ######################################################################
780 # Downloads torrents located in ${BT_DIR}/torrents/ and puts the
781 # result in the ${BT_DIR}
782
783 function bt () {
784     if [[ ${BT_DIR} ]]; then
785         if [[ -d "${BT_DIR}/torrents" ]]; then
786             if [[ "$1" ]]; then
787                 mv "$1" ${BT_DIR}/torrents
788             fi
789             if [[ "$(ps auxwww | grep btlaunchmanycurses | grep -v grep)" ]]; then
790                 echo "A client is already running."
791             else
792                 cd ${BT_DIR} && screen btlaunchmanycurses torrents --max_upload_rate 32
793             fi
794         else
795             echo "Directory ${BT_DIR}/torrents does not exist."
796         fi
797     else
798         echo "You have to set \$BT_DIR."
799     fi
800 }
801
802 ######################################################################
803 # The complex prompt policy
804
805 export PS1
806
807 if [ "${CONSOLE}" == "yes" ]; then
808     PS1=""
809 else
810
811 # If the login is a standard one (as specified in
812 # IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
813 # show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
814
815     if [ ! ${IGNORED_PROMPT_LOGIN} ] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]; then
816         IDENT="${USER}"
817     fi
818
819 # If the display is not the main one, make the assumption that the
820 # shell is not running on the localhost, and show the hostname
821
822     [ "${DISPLAY}" != ":0.0" ] && IDENT="${IDENT}@\h"
823
824 # If there is the login or the hostname, add a ":" to the prompt
825
826     [ "${IDENT}" ] && IDENT="${IDENT}:"
827
828 # If we are root, show that in red
829
830     if [[ ${USER} == "root" ]]; then
831         PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
832     else
833         PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
834     fi
835
836 # In an xterm, show the hostname and path in the title bar, highlight
837 # the prompt
838
839     # [ "${TERMS_WITH_BAR}" ] || TERMS_WITH_BAR="^xterm|screen$"
840
841     # if [[ "${TERM}" =~ "${TERMS_WITH_BAR}" ]]; then
842         # PS1="\[${VT_SET_TITLE}shell@\h (\w)${VT_END_TITLE}${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
843     # else
844         # PS1="\[${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
845     # fi
846
847 fi
848
849 ######################################################################
850 # This implements a local history. If we are in a directory containing
851 # a writable local history file, we add the last line of the global
852 # history to it.
853
854 LOCAL_HISTORY_FILE=".local_bash_history"
855
856 function keep_local_history () {
857     if [[ -w "${LOCAL_HISTORY_FILE}" ]]; then
858         history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
859         TMP=$(mktemp /tmp/lh.XXXXXX)
860         \chmod 600 ${TMP}
861         uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
862         # mv would replace a symbolic link, while cp keeps it
863         \cp ${TMP} ${LOCAL_HISTORY_FILE}
864         \rm ${TMP}
865         LOCAL_HISTORY_HINT="* "
866     else
867         LOCAL_HISTORY_HINT=""
868     fi
869 }
870
871 PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
872
873 ######################################################################
874 # Switch off the history
875
876 function histfile_cue () {
877     if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]; then
878         HISTORY_CUE="[${HISTFILE}]"
879     else
880         HISTORY_CUE=""
881     fi
882 }
883
884 PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
885
886 ######################################################################
887 # The dus command is available on my web site
888 #
889 # git clone http://fleuret.org/git/dus/
890
891 alias dus='dus -f -i'
892
893 ######################################################################
894 # The finddup command is available on my web site
895 #
896 # git clone http://fleuret.org/git/finddup/
897
898 # alias finddup='finddup -p0d'
899 alias finddup='finddup -p'
900
901 ######################################################################
902 # The selector command is available on my web site
903 #
904 # git clone http://fleuret.org/git/selector/
905
906 source bash-selector.sh --hist --cd
907
908 ######################################################################
909 # And we avoid to put in the history the use of the selector, which we
910 # do too often
911
912 HISTIGNORE="${HISTIGNORE}:selector-history"
913
914 ######################################################################
915
916 function selector-printer () {
917     TMP=$(mktemp /tmp/selector-printer.XXXXXX)
918     selector -o ${TMP} <(lpstat -a | awk '{print $1}')
919     export PRINTER=$(cat ${TMP})
920     echo "PRINTER=${PRINTER}"
921     rm -f ${TMP}
922     lpq
923 }
924
925 ######################################################################
926
927 function prompt_command () {
928 # save the history after every command to avoid loosing some when
929 # multiple shells are open
930     history -a
931 # load the saved history
932     history -n
933 # and the local histories system defined above
934     keep_local_history
935 # and the history cue
936     histfile_cue
937 }
938
939 PROMPT_COMMAND="prompt_command"
940
941 ######################################################################
942
943 # Displaying the timezone if it is set
944
945 if [[ ${TZ} ]]; then
946     echo "${VT_BOLD}${VT_GREEN_FG}Time zone is ${TZ}.${VT_RESET}"
947 fi
948
949 ######################################################################