Created
June 15, 2014 12:33
-
-
Save anonymous/50e9e9448c2366a09686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# Usage: push.sh [task] <args> | |
# -- Options -- | |
local_temp_directory=/tmp | |
ssh_host=some-server.asdf | |
screenshot_dir="~/screenshots" | |
screenshot_url_base="http://some-server.asdf/s" | |
screenshot_tool=gnome-screenshot | |
file_dir="~/files/" | |
file_url_base="http://some-server.asdf/f" | |
file_picker="zenity --file-selection" | |
paste_dir="~/pastes" | |
paste_url_base="http://some-server.asdf/p" | |
# Used as the app's name when displaying notifications | |
app_name=push.sh | |
# -- End options -- | |
task=$1 # More descriptive name | |
args=${*:2} | |
notify() { | |
notify-send "$1" "$2" \ | |
--app-name=$app_name \ | |
--hint=int:transient:1 \ | |
${*:3} # Pass additional arguments to notify-send | |
} | |
upload() { | |
scp_output=$(scp $1 $ssh_host:$2/ 2>&1) | |
if [[ $? -ne 0 ]]; then | |
notify "Upload failed ($?)" "$scp_output" --icon=error | |
delete_temp_file | |
exit 1 | |
fi | |
} | |
copy_url() { | |
echo $1/$(basename $2) | |
echo -n $1/$(basename $2) | xclip -selection clipboard -i | |
notify "Uploaded" "$1/$(basename $2)" | |
} | |
temp_file_name() { | |
printf %x $(date +%s%N) | |
} | |
delete_temp_file() { | |
[[ -f $tempname ]] && rm $tempname | |
} | |
tempname=$local_temp_directory/$(temp_file_name) | |
case $task in | |
screenshot) | |
tempname=$tempname.png | |
$screenshot_tool -f $tempname $args | |
upload $tempname $screenshot_dir | |
copy_url $screenshot_url_base $tempname | |
;; | |
file) | |
# If a file is specified and it exists, use that instead of asking. | |
file= | |
if [[ -e $2 ]]; then | |
file=$2 | |
else | |
file=$($file_picker $args) | |
# If we cancel the file picker, exit. | |
[[ $? -ne 0 ]]; exit 1 | |
fi | |
upload "$file" $file_dir | |
copy_url $file_url_base "$file" | |
;; | |
paste) | |
clipboard=$(xclip -selection clipboard -o) | |
if [[ -f $clipboard ]]; then # Are we copying a file? | |
# Drop it in the same place as file uploads | |
upload "$clipboard" $file_dir | |
copy_url $file_url_base "$clipboard" | |
else | |
xclip -selection clipboard -o > $tempname | |
upload $tempname $paste_dir | |
copy_url $paste_url_base $tempname | |
fi | |
;; | |
*) | |
echo -e Unknown task: $task. Available tasks: screenshot, file,\ | |
paste | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment