#!/bin/bash

#urls=$(awk 'match($0,/http:\/\/[a-zA-Z0-9]./+[a-zA-Z0-9]+/) { print substr($0, RSTART,RLENGTH) }' < $1 )
urls=$(awk 'match($0,/http:\/\/(tinyurl.com|beam.to|easyurl.net)\/[a-zA-Z0-9]+/) { print substr($0, RSTART,RLENGTH) }' < $1 )
for url in ${urls[@]}
do
wget --spider -NP -S $url -o log
target=$(grep Location log | tail -n1 | awk '{ print $2 }' | sed -e 's/\(\/\|\\\|&\)/\\&/g' )
echo "$url becomes $target"
url=$( echo $url | sed -e 's/\(\/\|\\\|&\)/\\&/g' )
#echo $url
sed -i '' -e "s/$url/$target/" $1
done