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