#!/bin/sh
# Makes TinyURLs from URLs given as argument or coming through stdin.

# Check if an URL has been given as argument
if [[ "$1" == "" ]]; then
	# That's not the case, so let's get it from stdin.
	read LONGURL
else
	# Yes. So we'll use that one.
	LONGURL="$1"
fi

# Send the URL to tinyurl.com and parse the tiny URL from the resulting
# website.
SHORTURL="$(curl -s "http://tinyurl.com/create.php?url=${LONGURL}" |\
          grep "\[.*\]" |\
          sed -e 's|^.*\[<a href=\"http\://tinyurl\.com/|http\://tinyurl.com/|' \
              -e 's|\".*$||')"

# Output the URLs
echo "${LONGURL} --> ${SHORTURL}"

# Copy tiny URL to KDE clipboard (klipper)
dcop klipper klipper setClipboardContents "${SHORTURL}"
