#!/bin/sh
# url: http://pank.org/scripts/
# description: bash tcp sample code work just like rough wget
# comment: bash
# platform: all

if [ -z $1 ]  ; then
    echo "`basename $0` {http URL}"
else
    if [ ${1:0:7} != http:// ] ; then
        echo http URL only
        exit 1
    fi
fi
TEMP=/tmp/$$.tmp
HOST=`echo $1 | cut -d/ -f3`
PATHFILE=/`echo $1 | cut -d/ -f4-`
{
exec 5<> /dev/tcp/$HOST/80
printf "GET $PATHFILE HTTP/1.0\nHOST: $HOST\n\n" >&5
cat <&5
exec 5>&-
} > $TEMP
L=`head $TEMP | col -b | awk '/Content-Length:/ {print $NF}'`
if [ -z $L ] ; then
    echo file not found
else
    tail -c$L $TEMP > `basename $1`
    echo `basename $1` saved
fi    
rm $TEMP