diff --git a/.bin/upload b/.bin/upload index f9fc52b..2fd3f57 100755 --- a/.bin/upload +++ b/.bin/upload @@ -37,6 +37,24 @@ remote_list() { && ls -1tr" } +copy_and_print() { + echo "$_weburi/$(basename "$1")" \ + | sed 's/ /%20/g' \ + | xclip -f -r + echo +} + +# handle stdin +if [[ ! -t 0 ]] +then + fname="$1" + [ -z "$fname" ] \ + && fname="$(date +"%Y-%m-%d_%H%M%S").txt" + cat - | ssh $_sshhost "cat - > \"$_rpath/$fname\"" + copy_and_print "$fname" + exit 0 +fi + if [ -n "$_ls" ] then remote_list \ @@ -97,75 +115,92 @@ then exit 0 fi -copy_and_print() { - echo "$_weburi/$(basename "$1")" \ - | sed 's/ /%20/g' \ - | xclip -f -r - echo -} if [ $# -gt 0 ] then - for file in "$@" + for item in "$@" do - [ ! -e "$file" ] \ - && echo "File or directory not found: $file" \ + # the whole handling shall run in a subshell so all variables + # are reset when we leave the scope + # note, that this requires us to return, instead fo continue + # to leave the loop iteration + ( + + [ ! -e "$item" ] \ + && echo "File or directory not found: $item" \ && exit 1 + # target filename + file="$(basename "$item")$_ext" + # handle file (plain), no archive mode - if [ -f "$file" ] && [ -z "$_tgz" ] && [ -z "$_gz" ] + if [ -f "$item" ] && [ -z "$_tgz" ] && [ -z "$_gz" ] then # make sure the file is least owner rw and group r # the sticky bit in the destiation folder will assign user+group - chmod u+rw,g+r "$file" - echo "Uploading $file" - scp -q "$file" "$_sshhost:$_rpath/${file}${_ext}" - copy_and_print "${file}${_ext}" - continue + chmod u+rw,g+r "$item" + echo "Uploading $item as $file" + scp -q "$item" "$_sshhost:\"$_rpath/$file\"" + copy_and_print "$file" + return fi # directories can only be handled as tgz archive - [ -d "$file" ] && _tgz=1 + [ -d "$item" ] && _tgz=1 # handle file or directory (tgz mode) if [ -n "$_tgz" ] then - echo "Uploading $file as ${file}${_ext}" - _ext="$_ext.tgz" - tar czf - "$file" \ - | ssh $_sshhost "pv - > $_rpath/${file}${_ext}" - copy_and_print "${file}${_ext}" - continue + file="$file.tgz" + echo "Uploading $item as $file" + # moving closer to the item to not archive the whole path + cd $(dirname "$item") + tar czf - "$(basename "$item")" \ + | ssh $_sshhost "pv - > \"$_rpath/$file\"" + copy_and_print "$file" + return fi # handle file (gz mode) - if [ -f "$file" ] && [ -n "$_gz" ] + if [ -f "$item" ] && [ -n "$_gz" ] then - echo "Uploading $file as ${file}${_ext}" - _ext="$_ext.gz" - cat "$file" gzip -o - "$file" \ - | $_sshhost "pv - > $_rpath/${file}${_ext}" - copy_and_print "${file}${_ext}" - continue + file="$file.gz" + echo "Uploading \"$item\" as \"$file\"" + cat "$item" | gzip \ + | ssh $_sshhost "pv - > \"$_rpath/$file\"" + copy_and_print "$file" + return fi # hopefully never reached - echo "Unhandled situation for: $file" + echo "Unhandled situation for: $item" + + ) # leave scope & reset variables done fi if [ $# -eq 0 ] then echo "usage: upload [command] [ext] []" - echo " commands:" + echo " upload [filename] < file" + echo " cat file | upload [filename]" + echo + echo " - files or directories to upload" + echo + echo " Commands:" echo " rm - remove files" echo " rml - remove last upload" echo " ls - list files" echo " -0..N - list N last uploads" echo " ren - rename file" echo " renl - rename last uploaded file" - echo " extensions:" + echo + echo " Extensions:" echo " sh, ksh, txt, log - add extension to file" - echo " - upload files" + echo + echo "Special Case:" + echo " When data is piped to the script it expects only one" + echo " optional argument, which would be the target filename." + echo exit 2 fi