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