playing with pasticcio

ID length

2 pastes
xhav raw 2022-08-17T18:58:54Z
: 1660762733:0;curl https://cvcv.cz/p/v/4ta2.txt | sh -s -- -pP - <<< `tail -n1 $HISTFILE` && echo 'cool hehe'
4ta2 raw 2022-08-17T18:30:38Z
#!/bin/sh -eu

#
# Utility to interact with the Pasticcio pastebin.
#

PASTHOST=${PASTHOST:="https://cvcv.cz"}

usage() {
	printf "%s\n" "Usage: ${0##*/} [options...] [FILE]"
	printf "\t%s\n" "-h: print this help."
	printf "\t%s\n" "-p: create a paste."
	printf "\t%s\n\n" "-P: make it public."
	printf "\t%s\n" "FILE: file to submit. If empty or '-', read stdin."
	printf "\n"
	printf "\t%s\n" "env:"
	printf "\t%s\n" "PASTHOST: remote host. Default: cvcv.cz."
	printf "\n"
	printf "\t%s\n" "example 1:"
	printf "\t%s\n" "\$ ${0##*/} -p \$(which ${0##*/})"
	printf "\t%s\n" "example 2:"
	printf "\t%s\n" "\$ echo \"Maynard J. Keenan\"|${0##*/} -p -"
}

submit_paste() {
	curl \
		-X POST \
		-s \
		-D - \
		-o /dev/null \
		--data-urlencode rand_id_length=4 \
		--data-urlencode public="$make_it_public" \
		--data-urlencode paste_blob@"$1" \
		--no-buffer \
		"$PASTHOST/p/new" \
		2>/dev/null
}

create_paste=0
file_name="-"
make_it_public="n"

while getopts hpP flag
do
	case $flag in
		h) usage && exit 0 ;;
		p) create_paste=1 ;;
		P) make_it_public="y" ;;
		*) usage && exit 1 ;;
	esac
done
shift $(($OPTIND-1))

if [ $create_paste -ne 1 ]; then
   # since we only have one option for now, we can fail.
   usage
   exit 1
fi

if [ $# -eq 1 ]; then
	file_name="$1"
fi

if [ $create_paste -eq 1 ];then
	resp=$(submit_paste "$file_name")
	first_line=$(printf "%s" "$resp" | grep '^HTTP')
	if printf "%s" "$first_line" | grep ' 302 ' >/dev/null; then
		path="$( \
			printf "%s" "$resp" | \
			grep -i '^Location:' | \
			sed -n 's/^Location: \(\S*\).*$/\1/pi' \
			)"
		printf "%s%s.txt\n" "$PASTHOST" "$path"
	else
		printf "%s\n" "$resp" >&2
		exit 1
	fi
fi