dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit 38d372b05a79b3857695851b6a8b5559b56f34c0
parent 091f41aff825be6774e063f63796949fb89b56df
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Wed,  2 Jan 2019 10:19:49 +0100

Improve structure and multi-file handling of upload script

Diffstat:
Mlinks/bin/upload | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 114 insertions(+), 32 deletions(-)

diff --git a/links/bin/upload b/links/bin/upload @@ -1,41 +1,123 @@ #!/usr/bin/env bash -# host="idkfa.ucsd.edu" -# user="ad" +set -e + +version=1.0 + +# Upload target host="adamsgaard.dk" user="andersdc" +remotedir="/var/www/html/files_nonpub" -if [ "$1" = "--border" ] || [ "$1" = "-b" ]; then - uploadfile="$(mktemp)" - #basename="border_$(basename "$2")" - basename="$(basename "$2")" - convert "$2" \ - -bordercolor White -border 10%x10% \ +function show_help { + echo "usage: ${0##*/} [OPTIONS] FILE1 [FILE2...[FILE N]]" + echo "will upload each FILE to $user@$host:$remotedir" + echo "OPTIONS are one or more of the following:" + echo " -h, --help show this message" + echo " -v, --version show version and license information" + echo " -b, --border if an image, add a 10% white border to FILES" + echo " -r, --resize if an image, resize FILES to fit 800x800 pixels" + echo " without changing its width-to-height ratio" + echo " -s, --sharpen if an image, sharpen all FILES" + echo "the optional operations are performed in the order listed above." + echo "${0##*/} requires imagemagick and rsync to be installed on the system." +} + +function show_version { + echo "${0##*/} version $version" + echo "Licensed under the GNU Public License, v3+" + echo "written by Anders Damsgaard, anders@adamsgaard.dk" + echo "https://gitlab.com/admesg/dotfiles" +} + +function add_sharpening { + convert "$1" \ -unsharp 2x0.5+0.7+0 \ -quality 95 \ - "$uploadfile" -elif [ "$1" = "--resize-border" ] || [ "$1" = "-rb" ]; then - uploadfile="$(mktemp)" - #basename="border_$(basename "$2")" - basename="$(basename "$2")" - convert "$2" \ - -resize 800x800\> \ + "$1" +} + +function add_border { + convert "$1" \ -bordercolor White -border 10%x10% \ -unsharp 2x0.5+0.7+0 \ -quality 95 \ - "$uploadfile" -else - uploadfile="$1" - basename="$(basename "$1")" -fi - -basename="${basename// /_}" -rsync -rvz --progress --chmod=Fu=rw,Fog=r -e "ssh" "$uploadfile" \ - "${user}@${host}:/var/www/html/files_nonpub/$basename" -url="https://$host/files_nonpub/${basename}" -echo -e "\\n$(tput setaf 2)Uploaded to $url (copied to clipboard)$(tput sgr0)" - -if [ "$(uname)" = "Darwin" ]; then - echo -n "$url" | pbcopy -else - echo -n "$url" | xclip -fi + "$1" +} + +function add_resize { + convert "$1" \ + -resize 800x800\> \ + -quality 95 \ + "$1" +} + +function upload_file { + infile="$1" + original_basename="$2" + formatted_basename="${$original_basename// /_}" + rsync -rvz --progress --chmod=Fu=rw,Fog=r -e "ssh" "$infile" \ + "${user}@${host}:${remotedir}/$formatted_basename" + url="https://$host/files_nonpub/${formatted_basename}" + echo + echo -e "$(tput setaf 2)Uploaded to $url (copied to clipboard)$(tput sgr0)" + + if [ "$(uname)" = "Darwin" ]; then + echo -n "$url" | pbcopy + else + echo -n "$url" | xclip + fi +} + +function die { + printf '%s\n' "$1" >&2 + exit 1 +} + +# parse input arguments (http://mywiki.wooledge.org/BashFAQ/035) +border=0 +resize=0 +sharpen=0 +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + -b|--border) + border=1 + ;; + -r|--resize) + resize=1 + ;; + -s|--sharpen) + sharpen=1 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'Error: Unknown option specified' + ;; + *) # No more options + break + esac + shift +done + +[ $# -lt 1 ] && (die 'Error: No input FILE[S] specified') +# loop over FILE[S] +for f in "$@"; do + tempfile="$(mktemp)" + cp "$f" "$uploadfile" + + [ "$border" = 1 ] && add_border "$tempfile" + [ "$resize" = 1 ] && add_resize "$tempfile" + [ "$sharpen" = 1 ] && add_sharpen "$tempfile" + + upload_file "$tempfile" "$(basename "$f")" +done