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