#!/bin/sh
# $Header: /home/cvsadmin/pbs/Trunk_linux/modules/ip1006ga/common/tools/uClibc-0.9.28/extra/scripts/getent,v 1.1.1.1 2008-07-29 08:29:01 red_liu Exp $

search_entry() {
  if [ -e "$1" ] ; then
    /bin/egrep -v "^#" $1 | /bin/sed 's/#.*$//' | /bin/egrep "${string}" | /bin/sed -n 1p
    retval=$?
    [ "$retval" = 0 ] || retval=2
  else
    retval=2
  fi
}

file="/etc/$1"
string="dummy"

#aliases|ethers|group|hosts|netgroup|networks|passwd|protocols|rpc|services|shadow)
# dns based search is not supported for hosts|networks
# ethers|netgroup (not done, needed)?
# it returns only the first match
case $1 in
  passwd)
    string="(^\<$2\>:|^.*:.*:\<$2\>:.*:.*:.*:.*)"
    ;;
  group)
    string="(^|:)\<$2\>:"
    ;;
  shadow)
    string="^\<$2\>:"
    ;;
  aliases)
    if [ -f /etc/postfix/aliases ] ; then
      file="/etc/postfix/aliases"
    elif [ -f /etc/mail/aliases ] ; then
      file="/etc/mail/aliases"
    fi
    string="^\<$2\>:"
    ;;
  networks)
    string="^\<$2\>"
    ;;
  hosts|protocols|rpc|services)
    string="\<$2\>"
    ;;
  *)
    echo "Unknown database: $1"
    exit 1
    ;;
esac

search_entry $file $2

exit $retval
