#! /bin/sh
# match --- emulate nis/ypmatch
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1993-02-03
# Public domain.

# $Id: match,v 1.5 1996/06/06 08:38:58 friedman Exp $

# Commentary:

# A horrible attempt to emulate some of the functionality of ypmatch
# since many machines don't use nis.

# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options} [key] [database]

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-h, --help                   You're looking at it.
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --d* )
      debug=t
      shift
     ;;
    -h | --help | --h )
      echo "$usage" 1>&2
      exit 0
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

if test $# -ne 2 ; then
   echo "$usage" 1>&2
   exit 1
fi

name="$1"
dbase="$2"

# First rewrite any shorthand database names
case "$dbase" in
  gid  ) dbase=group.bygid  ;;
  gr   ) dbase=group        ;;
  host ) dbase=hosts        ;;
  pw   ) dbase=passwd       ;;
  uid  ) dbase=passwd.byuid ;;
esac

# First try NIS; if the record is there, quit.
# We can't depend on the exit status of nismatch or ypmatch.
# On i386-*-bsdi2.0, for example, ypmatch does not exit with an error
# condition if yp is not in use.
#
# if { nismatch "$name" "$dbase" \
#      || ypmatch "$name" "$dbase"
#    } 2> /dev/null
# then
#   exit $?
# fi
str=`{ nismatch "$name" "$dbase" || ypmatch "$name" "$dbase"; } 2> /dev/null`
case "$str" in
  '' ) : ;;
  *  )
    echo "$str"
    exit 0
   ;;
esac

# First determine regexp to use
case "$dbase" in
   passwd.byuid | uid | group.bygid | gid )
                               regexp="^[^:]*:[^:]*:$name:" ;;
   passwd* |group* | aliases ) regexp="^$name:"             ;;
   *)			       regexp="$name"               ;; # e.g. hosts
esac

# Now determine filename
filename=
case "$dbase" in
   aliases )
     # Path to search for aliases file
     aliases='
        /etc/mail/lists
        /etc/mail
        /etc
        /usr/lib
        /etc/yp/src
        /etc/yp
        /var/yp
        /com/mailer
     '
     for d in $aliases ; do
       if test -f "$d/aliases"; then
         filename="$d/aliases"
         break
       fi
     done
    ;;
   passwd.gate )        filename="/usr/local/adm/$dbase" ;;
   uid | passwd.byuid ) filename="/etc/passwd" ;;
   gid | group.bygid )  filename="/etc/group"  ;;
   *)       		filename="/etc/$dbase" ;;
esac

if egrep "$regexp" "$filename" 2> /dev/null ; then
  exit $?
else
  case "$dbase" in
    aliases )
      # If alias wasn't in aliases database, try looking up alias as a
      # username and search for a .forward file.
      home=`{ grep "^$name:" /etc/passwd 2> /dev/null \
              || ypmatch "$name" passwd  2> /dev/null
            } \
            | sed -e 's/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*://' -e 's/:.*//'`

      if test -r "$home/.forward" ; then
        echo "$name:" `sed -ne 'H
                                ${
                                  s/^\n//
                                  s/\n/,/g
                                  p
                                }' "$home/.forward"`
        exit 0
      else
        exit 1
      fi
     ;;
    * ) exit $? ;;
  esac
fi

# match ends here
