dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

it2ul (1936B)


      1 #!/bin/bash
      2 
      3 trap clean_up EXIT
      4 _STTY=$(stty -g)      ## Save current terminal setup
      5 stty -echo            ## Turn off echo
      6 
      7 function clean_up() {
      8   stty "$_STTY"            ## Restore terminal settings
      9 }
     10 
     11 function show_help() {
     12   echo "Usage: $(basename $0) [destination [tar flags]]" 1>& 2
     13   echo "  If given, the destination specifies the directory to place downloaded files."
     14   echo "  Further options are passed through to tar. See your system's manpage for tar for details."
     15 }
     16 
     17 function bad_input() {
     18   echo "Bad input: %1" 1>& 2
     19   exit 1
     20 }
     21 
     22 function die() {
     23   echo "Fatal error: $1" 1>& 2
     24   exit 1
     25 }
     26 
     27 function read_base64_stanza() {
     28   value=""
     29   while read line;
     30   do 
     31     if [ "$line" == "" ]; then
     32       break
     33     fi
     34     printf "%s" "$line"
     35   done
     36 }
     37 
     38 function decode() {
     39   VERSION=$(base64 --version 2>&1)
     40   if [[ "$VERSION" =~ fourmilab ]]; then
     41     BASE64ARG=-d
     42   elif [[ "$VERSION" =~ GNU ]]; then
     43     BASE64ARG=-di
     44   else
     45     BASE64ARG=-D
     46   fi
     47 
     48   base64 "$BASE64ARG" <<< "$1"
     49 }
     50 
     51 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
     52 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
     53 # only accepts ESC backslash for ST.
     54 function print_osc() {
     55     if [[ $TERM == screen* ]] ; then
     56         printf "\033Ptmux;\033\033]"
     57     else
     58         printf "\033]"
     59     fi
     60 }
     61 
     62 # More of the tmux workaround described above.
     63 function print_st() {
     64     if [[ $TERM == screen* ]] ; then
     65         printf "\a\033\\"
     66     else
     67         printf "\a"
     68     fi
     69 }
     70 
     71 function send_request_for_upload() {
     72   print_osc
     73   printf '1337;RequestUpload=format=tgz' ""
     74   print_st
     75 }
     76 
     77 location="$PWD"
     78 if [[ $# > 0 ]]
     79 then
     80   location="$1"
     81   shift
     82 fi
     83 
     84 send_request_for_upload
     85 read status
     86 
     87 if [[ $status == ok ]]
     88 then
     89   data=$(read_base64_stanza)
     90   clean_up
     91   decode "$data" | tar -x -z -C "$location" -f - $* 1>& 2
     92 elif [[ $status == abort ]]
     93 then
     94   echo "Upload aborted" 1>& 2
     95 else
     96   die "Unknown status: $status" 1>& 2
     97 fi
     98