#!/bin/sh -e
#
# Extracting the fields with sed is really a pain. In Perl it looks
# like this:
#
# if ($ARGV[0] !~ q{^(([^:/?\#]+)://)?([^/?\#:]*)(:([0-9]+))?(/.*)?}) {
#     die "$0: bad URL $ARGV[0]\n" ;
# }
#
# $scheme = $2 || "http" ;
# $host = $3 ;
# $port = $5 || 80 ;
# $path = $6 || "/" ;
#
# But then you wouldn't need the Socket program. ;-)


progname=`basename $0`

prot=`echo "${1?URL missing}" | sed -n 's;^\([^:]*\):.*$;\1;p'`
host=`echo "$1" | sed -n 's;^.*://\([^/]*\)/.*$;\1;p'`
file=/`echo "$1" | sed -n 's;^.*://[^/]*/\(.*\)$;\1;p'`

#echo prot=$prot
#echo host=$host
#echo file=$file

if [ -z "$prot" ] ; then
    echo $progname: Protocol name missing. Please check URL syntax. 1>&2
    exit 1
fi

if [ -z "$host" ] ; then
    echo $progname: Hostname missing. Please check URL syntax. 1>&2
    exit 1
fi

if [ $prot: != http: ] ; then
    echo $progname: Only HTTP retrieval supported. Please check URL syntax. 1>&2
    exit 1
fi

echo "GET $file HTTP/1.0
" | socket -c $host http
