:
##########################################################################
# Shellscript:	guesshomepage - guess home page given e-mail address
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	2000-09-02
# Requires   :	ping, timeout
# Category   :	WWW
# SCCS-Id.   :	@(#) guesshomepage	1.2 03/06/03
##########################################################################
# Description
#	Takes an e-mail address, and tries to guess the home page of the user
#	by applying some heuristics on user name and host names.
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.2'

timeout=5				# seconds (per "ping")

Usage () {
    echo >&2 "$PN - guess home page URL, $VER
usage: $PN [-n] e-mail [...]
    -n:  do not use \"ping\" to check if host names are valid"
    exit 1
}

Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

guessdomains () {
    # Process domain names always in lower case
    _d=`echo "$1" | tr '[A-Z]' '[a-z]'`

    OIFS=$IFS
    IFS=.
    set -- $_d
    IFS=$OIFS

    echo "$_d"

    while [ $# -ge 2 ]
    do
        _fullname=`echo "$@" | tr ' ' .`
	echo "$_fullname"

	for _prefix in www home people users
	do
	    case "$_fullname" in
	    	$_prefix.*)	;;
		*)	echo "$_prefix.$_fullname";;
	    esac
	done
	shift
    done
}

guessnames () {
    _gn_n=$1
    echo "$_gn_n/"
    case "$_gn_n" in
    	*.*)
	    OIFS=$IFS
	    IFS=.
	    set -- $_gn_n
	    IFS=$OIFS
	    _firstn=$1; shift
	    _secondn=$@
	    ;;
	*_*)
	    OIFS=$IFS
	    IFS=_
	    set -- $_gn_n
	    IFS=$OIFS
	    _fistn=$1; shift
	    _secondn=$@
	    ;;
    esac

    if [ -n "$_firstn" ] && [ -n "$_secondn" ]
    then
    	# "heiner.steven" -> "hsteven"
    	_initial=`expr "$_firstn" : '\(.\).*'`
	echo "${_initial}$_secondn/"
    fi
}

guesspaths () {
    _gp_n=$1
    for _gp_n in `guessnames "$_gp_n"`
    do
    	# Need trailing "/" for directory names
	for dir in / /people/ /home/ /homes/
	do
	    echo "${dir}~$_gp_n"
	    echo "${dir}$_gp_n"
	done
    done
}

set -- `getopt :hn "$@"` || Usage
[ $# -lt 1 ] && Usage			# "getopt" detected an error

Check=true
while [ $# -gt 0 ]
do
    case "$1" in
	-n)	Check=false;;
	--)	shift; break;;
	-h)	Usage;;
	-*)	Usage;;
	*)	break;;			# First file name
    esac
    shift
done

if $Check
then
    # Some systems (e.g. NetBSD, Solaris) have "ping" in an "sbin" directory
    PATH=$PATH:/usr/sbin:/sbin	export PATH

    case "`uname | tr '[A-Z]' '[a-z]'`" in
    	linux)	pingonce="ping -c 1";;
	sunos)	pingonce=ping;;
	*)	pingonce="ping -c 1";;
    esac

    : ${PINGONCE:=$pingonce}
fi

for email
do
    OIFS=$IFS; IFS=@
    set -- $email
    IFS=$OIFS
    name=$1
    domain=$2
    #echo >&2 "name=<$name>, domain=<$domain>"

    for d in `guessdomains "$domain"`
    do
    	if $Check
	then
	    timeout -t "$timeout" $PINGONCE "$d" >/dev/null 2>&1 || continue
	fi	    

    	for n in `guesspaths "$name"`
	do
	    echo "http://${d}$n"
	done
    done
done
