#!/bin/sh
# mx --- find MX hosts for a host name
# Author: Noah Friedman <friedman@splode.com>
# Created: 1999-07-24
# Public domain

# $Id: mx,v 1.2 2005/08/26 01:30:53 friedman Exp $

justone=
numeric=
while :; do
  case $1 in
    -1 )  shift; justone='1!d' ;;
    -ip ) shift; numeric=t     ;;
    * ) break ;;
  esac
done

dig "$1" mx \
 | sed -n -e '
       /^;; ANSWER/{
         :l
         n
         /^;;/q
         /^'"$1"'\..*MX/!b l
         s/.*MX[ 	]*//
         s/\.$//
         p
         b l
       }' \
 | sort -n \
 | sed -e 's/.*[ 	]//' \
       -e "$justone"

# mx ends here.
