#!/bin/sh -e # manage files in a webroot # needs: sh (cp,rm), openssh (ssh,scp), gzip, tar, sed, tail, xclip # remote patch uploaded files should go _rpath="/home/www/htdocs/ptrace/paste" # web url where the uploaded files can be accessed _weburi="https://ptrace.org" # ssh user@host _sshhost="sdk@codevoid.de" for arg in "$@" do case "$arg" in sh) _ext=".sh" ;; ksh) _ext=".ksh" ;; txt) _ext=".txt" ;; log) _ext=".log" ;; gz) _gz=1 ;; tgz) _tgz=1 ;; rm) _rm=1 ;; rml) _rmlast=1 ;; ls) _ls=1 ;; l|last) _last="-1" ;; -[0-9]*) _last="$arg" ;; ren|mv) _mv=1 ;; renl|mvl) _mvlast=1 ;; *) break; ;; esac shift; done remote_list() { ssh $_sshhost \ "cd $_rpath/ \ && 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 \ | while read f do echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)" done exit 0 fi if [ -n "$_last" ] then remote_list \ | tail $_last \ | while read f do echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)" done exit 0 fi if [ -n "$_mv" ] then ssh $_sshhost \ "cd $_rpath/ \ && mv -v \"$1\" \"$2\"" exit 0 fi if [ -n "$_mvlast" ] then lastfile="$(remote_list | tail -1)" ssh $_sshhost \ "cd $_rpath/ \ && mv -v \"$lastfile\" \"$2\"" exit 0 fi if [ -n "$_rm" ] then for file in "$@" do ssh $_sshhost \ "cd $_rpath/ \ && rm -v \"$(basename "$file")\"" \ || true done exit 0 fi if [ -n "$_rmlast" ] then lastfile="$(remote_list | tail -1)" ssh $_sshhost \ "cd $_rpath/ \ && rm -v \"$lastfile\"" \ || true exit 0 fi if [ $# -gt 0 ] then for item in "$@" do # 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 "$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 "$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 "$item" ] && _tgz=1 # handle file or directory (tgz mode) if [ -n "$_tgz" ] then 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 "$item" ] && [ -n "$_gz" ] then 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: $item" ) # leave scope & reset variables done fi if [ $# -eq 0 ] then echo "usage: upload [command] [ext] []" 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 echo " Extensions:" echo " sh, ksh, txt, log - add extension to file" 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