#!/bin/sh

AWK="/usr/bin/awk"

if [[ "$1" == "" ]]; then
  base=`basename $0`
  echo "Usage: $base user_or_domain"
  echo "Examples: "
  echo "$base example.com "
  echo "$base bill@example.com "
  echo
  exit 1
fi

$AWK -v x="$1" '
  BEGIN {
    if( x ~ /^local\./ || x ~ /^remote\./ ) {
      filter = x
    } else {
      if( x !~ /@/ ) {
        filter = "@" x "$"
      } else {
        filter = "^((local\\.)|(remote\\.))" x "$"
      }
    }
  }
  /^d/ {
    if ($8 ~ filter) print
  }
'
