#!/usr/bin/env bash
# Nautilus script for creating one or more shared links
# Requires wl-clipboard and notify-send 

ssh_server="bryanroessler.com"
ssh_files_path="/var/www/repos.bryanroessler.com/files"
www_files_path="https://repos.bryanroessler.com/files"

if [[ "$#" -lt 1 ]]; then
	echo "You must provide at least one argument"
	exit 1
fi

hash wl-copy &>/dev/null || echo "Please install wl-copy (usually in the wl-clipboard package)"

if [[ -v NAUTILUS_SCRIPT_SELECTED_URIS ]]; then
	readarray -t files <<< "$NAUTILUS_SCRIPT_SELECTED_URIS"
	files=("${files[@]#file://}")
	files=("${files[@]//\%20/ }")
else
	files=("$@")
fi

for file in "${files[@]}"; do
	[[ "$file" == "" ]] && continue
	echo here
    fname="${file##*/}"
    random64=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 64 | head -n 1)
	echo "rsync -a ${file} ${ssh_server}:${ssh_files_path}/${random64}/"
	nohup rsync -a "${file}" "${ssh_server}:${ssh_files_path}/${random64}/" &
	links_array+=("$www_files_path/${random64}/${fname// /%20}")
done

if [[ "${#links_array[@]}" == 1 ]]; then
	printf '%s' "${links_array[@]}" | wl-copy
else
	printf '%s\n' "${links_array[@]}" | wl-copy
fi

hash notify-send &>/dev/null &&
	notify-send -t 3000 -i face-smile "share-link" "File(s) uploaded and link copied to clipboard"

exit 0