b2b3e51ab0e7478293590c54f90742263634210d
[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     TERM_SIZE=($(stty size))
589     \ls -goth --time-style="+${VT_GREEN_FG}%Y %b %d %H:%M${VT_RESET}" "$@" | \
590         head -$((TERM_SIZE[0]-2)) | cut -b1-${TERM_SIZE[1]}
591 }
592
593 ######################################################################
594
595 reduce-pdf () {
596
597     # /screen selects low-resolution output
598     # /ebook selects medium-resolution output
599     # /printer selects "Print Optimized" setting.
600     # /prepress selects "Prepress Optimized" setting.
601
602     quality="printer"
603
604     while [[ "$1" ]]
605     do
606         if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]
607         then
608             echo "$0 [<file.pdf>|--quality <screen|ebook|printer|prepress>] ..."
609             return 0
610
611         elif [[ "$1" == "--quality" ]]
612         then
613             shift
614             quality="$1"
615
616         elif [[ -f "$1" ]]
617         then
618             result="$(basename "$1" .pdf)-${quality}.pdf"
619             echo -n "Generating ${result} with quality ${quality} ... "
620             gs -sDEVICE=pdfwrite \
621                -dCompatibilityLevel=1.4 \
622                -dPDFSETTINGS=/${quality} \
623                -dColorConversionStrategy=/LeaveColorUnchanged \
624                -dNOPAUSE -dQUIET -dBATCH \
625                -sOutputFile="${result}" "$1"
626             echo "done."
627             ls -hl "$1" "${result}"
628
629         else
630             echo "Cannot fine $1"
631
632         fi
633         shift
634     done
635 }
636
637 ######################################################################
638 # You can change the xterm background color on the fly!
639
640 function setxtermbg () {
641     echo -n $'\e]11;'$1$'\007'
642 }
643
644 ######################################################################
645 # Shuffle the lines from the stdin
646
647 function shuffle () {
648     SEED=$1
649     [[ $SEED ]] || SEED=0
650     awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
651 }
652
653 ######################################################################
654 # Stores the last entered command into a file
655
656 KEPT_COMMANDS=${HOME}/.kept_bash_commands
657
658 function keep () {
659     if [[ ${KEPT_COMMANDS} ]]
660     then
661         TOKEEP=$(mktemp /tmp/keep.XXXXXX)
662         if [[ "$@" ]]
663         then
664             echo "$@" > ${TOKEEP}
665         else
666             selector -b -i -d -l ${HISTSIZE} -o "${TOKEEP}" <(history)
667         fi
668
669         if [[ -s "${TOKEEP}" ]]
670         then
671             echo $(date)": "$(cat ${TOKEEP}) >> ${KEPT_COMMANDS}
672             cat "${TOKEEP}"
673         else
674             echo "No command stored!"
675         fi
676
677         \rm ${TOKEEP}
678     else
679         echo "You have to set \$KEPT_COMMANDS"
680     fi
681 }
682
683 ######################################################################
684 # I sometime burn CDs and DVDs
685
686 function burn () {
687     [[ "${DEV_BURNER}" ]] || DEV_BURNER="/dev/sr0"
688     if [[ ! "$1" ]]
689     then
690         echo "burn <iso name | dirname>" >&2
691     elif [[ -f "$1" ]]
692     then
693         if [[ $(file "$1" | \grep -E 'ISO 9660|UDF filesystem data') ]]
694         then
695             wodim -eject -v dev=${DEV_BURNER} "$1"
696         else
697             echo "Do not know what to do with $1" >&2
698         fi
699     elif [[ -d "$1" ]]
700     then
701         [[ "${TMP_ROOT}" ]] || TMP_ROOT=/tmp/
702         echo "Using ${TMP_ROOT} as temporary directory."
703         TMP=$(mktemp ${TMP_ROOT}/cdimage.XXXXXX) && \
704             genisoimage -input-charset iso8859-1 -r -o ${TMP} "$1" && \
705             wodim -eject -v dev=${DEV_BURNER} ${TMP}
706         rm -f ${TMP}
707     else
708         echo "Can not find $1" >&2
709     fi
710 }
711
712 ######################################################################
713 # And watch DVDs too!
714
715 function dvd () {
716
717     echo
718     echo " ! @   Seek to the beginning of the previous/next chapter"
719     echo " j     Cycle through the available subtitles"
720     echo " o     Show/hide the timing"
721     echo " x z   Subtitle delay"
722     echo " / *   Volume"
723     echo
724
725     if [[ "$1" ]]
726     then
727         dvd_device="$1"
728         shift
729     else
730         dvd_device="/dev/dvd"
731     fi
732
733     title="1"
734
735     if [[ "$1" ]]
736     then
737         title=$1
738         shift
739     fi
740
741     # -vc ffmpeg12 -vf yadif
742
743     mplayer > /dev/null \
744         -stop-xscreensaver \
745         -quiet \
746         -alang en -slang en \
747         -softvol -softvol-max 1000 \
748         -dvd-device ${dvd_device} ${MPLAYER_OPTIONS} dvd://${title}
749 }
750
751 function ripdvd () {
752     if [[ -e "/dev/dvd" ]]
753     then
754         DVD_DEVICE="/dev/dvd"
755     elif [[ -e "/dev/dvd3" ]]
756     then
757         DVD_DEVICE="/dev/dvd3"
758     else
759         echo "Can not find the dvd device." >&2
760         return 1
761     fi
762
763     echo "Attemptin to rip from ${DVD_DEVICE}."
764
765     mkdir -p ${HOME}/dvds
766
767     cd ${HOME}/dvds
768     time dvdbackup -i ${DVD_DEVICE} -v -M "$@" && eject
769 }
770
771 alias ripcd=abcde
772
773 ######################################################################
774 # Create small images from images
775
776 function mksmall () {
777
778     if [[ "$1" == "--params" ]]
779     then
780         shift
781         PARAMS="$1"
782         shift
783     fi
784
785     [[ "${PARAMS}" ]] || PARAMS="-geometry 1280x1024"
786
787     # Auto-orient does not seem to work at all, hence the ugly hack
788     # with exif below
789
790     # PARAMS="-auto-orient -geometry 800x600"
791
792     echo "Using ${PARAMS}"
793
794     DEST_DIR=$1
795
796     [[ ${DEST_DIR} ]] || DEST_DIR=./small
797
798     mkdir -p ${DEST_DIR}
799
800     if [[ ! -d ${DEST_DIR} ]]
801     then
802         echo "Can not create ${DEST_DIR}" >&2
803         return
804     fi
805
806     NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
807     NB=0
808
809     for i in $(find -maxdepth 1 -type f)
810     do
811         if [[ $(file $i | grep image) ]]
812         then
813             if [[ -e ${DEST_DIR}/$i ]]
814             then
815                 echo "The file ${DEST_DIR}/$i already exists."
816             else
817                 if [[ $(file ${i/%.*/}.* | grep -E movie) ]] 
818                 then
819                     CAPTION_PARAMS="-font FreeSans-Bold -pointsize 32 -fill green -annotate +10+32 Video"
820                 else
821                     CAPTION_PARAMS=""
822                 fi
823
824                 convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
825             fi
826
827             \ls -lt ${DEST_DIR}/$i
828         fi
829
830         NB=$((NB+1))
831
832         echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
833     done
834 }
835
836 ######################################################################
837 # Move a file to the ~/sources/config directory and replace it where
838 # it was by a symbolic link
839
840 function mvtoconfig () {
841     CONFIGDIR=${HOME}/sources/config
842     if [[ -d ${CONFIGDIR} ]]
843     then
844         NEWNAME=${CONFIGDIR}/$(basename "$1" | sed -e "s/^\.//")
845         mv "$1" $NEWNAME
846         ln -s $NEWNAME $1
847     else
848         echo "Can not find ${CONFIGDIR}"
849     fi
850 }
851
852 ######################################################################
853 # The complex prompt policy
854
855 export PS1
856
857 if [[ "${CONSOLE}" == "yes" ]]
858 then
859
860     PS1=""
861
862 else
863
864     # If the login is a standard one (as specified in
865     # IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
866     # show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
867
868     if [[ ! ${IGNORED_PROMPT_LOGIN} ]] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]
869     then
870         IDENT="${USER}"
871     fi
872
873     # If the display is not the main one, make the assumption that the
874     # shell is not running on the localhost, and show the hostname
875
876     [[ "${DISPLAY}" != ":0.0" ]] && IDENT="${IDENT}@\h"
877
878     # If there is the login or the hostname, add a ":" to the prompt
879
880     [[ "${IDENT}" ]] && IDENT="${IDENT}:"
881
882     if [[ ${USER} == "root" ]]
883     then
884         # If we are root, show that in red
885         PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
886     else
887         if [[ $(\grep QEMU /proc/cpuinfo) ]]
888         then
889             # If we are in a qemu virtual machine, in yellow
890             PS1="\[${VT_YELLOW_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
891         else
892             # Otherwise in white
893             PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
894         fi
895     fi
896
897 fi
898
899 PS1="${PSCUE}${PS1}"
900
901 ######################################################################
902 # This implements a local history. If we are in a directory containing
903 # a writable local history file, we add the last line of the global
904 # history to it.
905
906 LOCAL_HISTORY_FILE=".local_bash_history"
907
908 function keep_local_history () {
909     if [[ -w "${LOCAL_HISTORY_FILE}" ]]
910     then
911         history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
912         TMP=$(mktemp /tmp/lh.XXXXXX)
913         \chmod 600 ${TMP}
914         uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
915         # mv would replace a symbolic link, while cp keeps it
916         \cp ${TMP} ${LOCAL_HISTORY_FILE}
917         \rm ${TMP}
918         LOCAL_HISTORY_HINT="* "
919     else
920         LOCAL_HISTORY_HINT=""
921     fi
922 }
923
924 PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
925
926 ######################################################################
927 # Show the history path if it is unusual
928
929 function histfile_cue () {
930     if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]
931     then
932         HISTORY_CUE="[${HISTFILE}]"
933     else
934         HISTORY_CUE=""
935     fi
936 }
937
938 PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
939
940 ######################################################################
941 # The dus command is available on my web site
942 #
943 # git clone http://fleuret.org/git/dus/
944
945 alias dus='dus -f -i'
946
947 ######################################################################
948 # The finddup command is available on my web site
949 #
950 # git clone http://fleuret.org/git/finddup/
951
952 alias finddup='finddup -p'
953
954 ######################################################################
955 # The selector command is available on my web site
956 #
957 # git clone http://fleuret.org/git/selector/
958
959 export SELECTOR_CD_HISTORY_SIZE=10000
960
961 source bash-selector.sh --hist --cd
962
963 ######################################################################
964 # And we avoid to put in the history the use of the selector, which we
965 # do too often
966
967 HISTIGNORE="${HISTIGNORE}:selector-history"
968
969 ######################################################################
970
971 function selector-printer () {
972     TMP=$(mktemp /tmp/selector-printer.XXXXXX)
973     selector -o ${TMP} <(lpstat -a | awk '{print $1}')
974     export PRINTER=$(cat ${TMP})
975     echo "PRINTER=${PRINTER}"
976     rm -f ${TMP}
977     lpq
978 }
979
980 ######################################################################
981 # A ls with memory to notice what files have been added/removed from
982 # the current directory
983
984 function lsn () {
985     [[ "${LSN_STATE_FILE}" ]] || LSN_STATE_FILE=".lsn-state"
986
987     if  [[ $1 == "--mem" ]] || [[ "$1" == "-m" ]]
988     then
989
990         \ls -pa | sort > "${LSN_STATE_FILE}"
991         echo "State updated." >&2
992
993     elif [[ $1 == "+" ]]
994     then
995
996         shift
997
998         TMP=$(mktemp /tmp/lsn.XXXXXX)
999         \ls -d "$@" > ${TMP}
1000         cat "${LSN_STATE_FILE}" >> ${TMP}
1001         sort ${TMP} | uniq -u > "${LSN_STATE_FILE}"
1002         \rm ${TMP}
1003
1004     elif [[ "$1" ]]
1005     then
1006         cat >&2 <<EOF
1007 lsn [--help|-h] [--mem|-m] [+ <file> ...]
1008
1009 Where
1010
1011   --help|-h
1012      prints this help
1013
1014   --mem|-m
1015      stores the current state of the directory
1016
1017   + <file> [<file> ...]
1018      switches the presence/absence of the indicated files in the
1019      stored state
1020
1021 EOF
1022         if [[ ! "$1" == "-h" ]] && [[ ! "$1" == "--help" ]]
1023         then
1024             echo >&2 "Unknown option \`\`$1''."
1025             return 1
1026         fi
1027
1028     else
1029
1030         if [[ -f "${LSN_STATE_FILE}" ]]
1031         then
1032             TMP=$(mktemp /tmp/lsn.XXXXXX)
1033             \ls -pa | sort > ${TMP}
1034             if diff > /dev/null ${TMP} "${LSN_STATE_FILE}"
1035             then
1036                 echo "${VT_GREEN_FG}${VT_BOLD}No change (since $(date +"%b %d, %Y" -r "${LSN_STATE_FILE}"))${VT_RESET}"
1037             else
1038                 \comm -1 -3 "${LSN_STATE_FILE}" ${TMP}
1039                 \comm -2 -3 "${LSN_STATE_FILE}" ${TMP} | while read line
1040                                                          do
1041                     echo "${VT_RED_FG}${VT_BOLD}${line}${VT_RESET} (missing)"
1042                 done
1043             fi
1044             \rm ${TMP}
1045         else
1046             echo "${VT_RED_FG}${VT_BOLD}No lsn state here.${VT_RESET}" >&2
1047             return 1
1048         fi
1049
1050     fi
1051 }
1052
1053 ######################################################################
1054
1055 function prompt_command () {
1056     [[ "${CORE_CHECK_PWD}" == ${PWD} ]] || find . -maxdepth 1 -name "core__*" | awk '{print "'${VT_RED_FG}'"$0"'${VT_RESET}'"}'
1057     CORE_CHECK_PWD=${PWD}
1058
1059     # if [[ $(umask) != 0022 ]]
1060     # then
1061         # echo "Umask changed to $(umask)"
1062     # fi
1063     # save the history after every command to avoid loosing some when
1064     # multiple shells are open
1065     history -a
1066     # load the saved history
1067     history -n
1068     # and the local histories system defined above
1069     keep_local_history
1070     # and the history cue
1071     histfile_cue
1072 }
1073
1074 PROMPT_COMMAND="prompt_command"
1075
1076 ######################################################################
1077
1078 # Displaying the timezone if it is set
1079
1080 [[ ${TZ} ]] && echo "${VT_BLUE_FG}Time zone is ${TZ}.${VT_RESET}"
1081
1082 ######################################################################