download (1704B)
1 #!/bin/rc 2 3 fn uri2file { 4 ret = () 5 6 for (uri in $*) { 7 filetype = `{echo $uri | grep -o '\.[^/]*$' | sed 's/^.//'} 8 hier = ``('/'){echo $uri | sed 's~^.*://~~'} 9 for (ent in $hier) { 10 if (~ $reverse ()) { 11 reverse = $ent 12 } else { 13 reverse = $ent ^ '+' ^ $reverse 14 } 15 } 16 filename = `{echo $reverse | tr -d '\n'} 17 if (! ~ $filetype ()) 18 filename = $^filename ^ '.' ^ $filetype 19 reverse = () 20 ret = ($ret $filename) 21 } 22 23 echo $^ret | tr -d ' ' 24 } 25 26 fn mktemp { 27 urifile = ``($nl){uri2file $2} 28 ret = $1 ^ '/' ^ $^urifile 29 while (test -f $ret) { 30 ret = $1 ^ '/' ^ `{base64 </dev/urandom | tr -d '/' | head -c10} 31 } 32 echo $^ret | tr -s '/' 33 } 34 35 rcname = `{basename $0} 36 37 if (~ $* ()) 38 * = `{cat /dev/stdin} 39 40 for (arg in $*) { 41 switch ($arg) { 42 case -h 43 cat >[1=2] <<eof 44 usage: $rcname [-hpdt] <file|URI> ... 45 46 -h help 47 -p plumb URIs 48 -d download URIs to default location 49 . 50 eof 51 exit 2 52 case -p 53 default = 'plumb' 54 case -d 55 default = 'download' 56 case * 57 if (test -f $arg) { 58 lines = ``($nl){cat $arg} 59 for (line in $lines) { 60 column = ``($tab $nl){echo $line} 61 if (!~ $column(1) () && !~ $column(2) ()) { 62 mkdir -p $column(1) 63 filename = $column(1) ^ '/' ^ `{uri2file $column(2)} 64 if (!test -f $^filename) { 65 curl '-o'^$filename $column(2) 66 } 67 } 68 } 69 } else { 70 if (! ~ $default ()) { 71 input = $default 72 } else { 73 input = `{printf 'plumb\ndownload\n' | dmenu -i -p $arg} 74 } 75 76 switch ($input) { 77 case plumb 78 plumb $arg 79 case download 80 filename = ``(''){mktemp $HOME/general/downloads $arg | tr -d '\n'} 81 uri = `{echo $arg | sed -E 's/]|\[|}|\{/\\&/g'} 82 curl $^uri > $^filename 83 herbe 'Saved to: ' ^ $^filename 84 } 85 } 86 } 87 }