#!/bin/ksh
#
# $Revision: 1.2 $ $Date: 2012-07-26 17:11:49-04 $
# $Source: /home/vogelke/bin/RCS/tinyurl,v $
# $Host: sys7.com $
# $UUID: 1d8bfe95-1d3a-3fae-991a-8319bfb8aad7 $
#
#<tinyurl: shorten a given URL using tinyurl.com API
#
# result from example http://betaworks.com/:
# http://tinyurl.com/create.php?url=http%3A%2F%2Fbetaworks.com%2F
## / DON'T SCREEN-SCRAPE, PLEASE CONTACT US TO FIND OUT HOW TO USE OUR API /

export PATH=/usr/local/bin:/usr/sfw/bin:/opt/sfw/bin:/bin:/usr/bin
tmp=/tmp/tiny$$

# Safe encoding for basic URLs.
sedscr='
  s!:!%3A!g
  s!/!%2F!g
  s! !%20!g
'

case "$#" in
    0) echo need a url; exit 1 ;;
    *) url=$(echo "$*" | sed -e "$sedscr") ;;
esac

wget -q -O $tmp 'http://tinyurl.com/api-create.php?url='$url
echo $(cat $tmp)  # no trailing newline.
rm $tmp
exit 0
