dpaste () {
(
local histfile histdir token expire file resfile mimetype lang pasteid pasteurl
((DEBUGTHIS)) && VERBOSE='-v' || VERBOSE=
histfile=~/.dpaste_history
histdir=~/.dpaste_history.d
mkdir -p "$histdir"
token="X-Paste-Token: $(cat ~/.dotfiles/.dpaste)"
expire=7776000
file=$(mktemp -t dpaste.XXXX)
resfile=$(mktemp -t dpaste.XXXX)
cp "${1:-/dev/stdin}" "$file"
mimetype="$(xdg-mime query filetype "$file")"
lang="${mimetype/text\/x-/}"
[ "$lang" = "$mimetype" ] && lang=bash
data="$(jq --arg poster "$USER" \
--arg lang "$lang" \
--arg expire "$expire" \
--slurp --raw-input \
'{$poster,$lang,$expire,code:.}' "$file")"
((DEBUGTHIS)) && printf 'DEBUG:\n%s\n\n' "$data"
res="$(curl --show-error --fail --silent $VERBOSE \
--output "$resfile" \
--write-out "%{http_code}" \
-X POST "https://paste.debian.net/api/v1/paste" \
-H "accept: application/json" \
-H "X-Paste-Token: 3e8bfe429b957d2c2a7ace88b9a84d347b8eb172" \
-H "Content-Type: application/json" \
-d "$data")"
if [ "$res" != 200 ]
then
echo 'FORMORER!!!!!'
else
pasteid=$(jq -r .id < "$resfile")
url=$(jq -r .url < "$resfile")
expires=$(jq -r .expires < "$resfile")
expires_date=$(date --date "+${expires} seconds" +%s)
jq --arg expiresdate "$expires_date" '[{$expiresdate},.]|add' "$resfile" | tee -a "$histfile"
cp "$file" "$histdir"/"$pasteid"
echo "${url/hidden/plainh}" | tr -d '\n' | xsel -i -b
fi
rm "$file" "$resfile"
)
}