#!/bin/sh
#@(#)HP X Station Admin Scripts
#
# isxtnameinh	Check if hostname of X station already exists in hosts database
#


if [ $1 ]
then
	XTNAME=$1
        export XTNAME
else
	echo "X station name not supplied in call to $0"
	exit 3
fi


if [ "X$DOMN" != "X" ]
then
  nslookup $XTNAME 2>&1 | egrep -i '(can.t find)|(no address)' > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    IPADDR=`nslookup $XTNAME | sed -n '/^Name:/,/^Address:/p' | tail -1 \
	| awk '$1 == "Address:" {print $2}' | egrep '^[0-9]'`
    if [ $? -ne 0 ]
    then
        echo ""
        echo "Your computer uses a domain nameserver. Please add $XTNAME"
        echo "to the nameserver database and re-run this selection."
        echo ""
        exit 1
    else
      echo $IPADDR > $TMP/IPADDR
      exit 6
    fi
  else
        echo ""
        echo "Your computer uses a domain nameserver. Please add $XTNAME"
        echo "to the nameserver database and re-run this selection."
        echo ""
        exit 1
  fi
fi


if [ $NISPLUSH = "true" ]
then
  IPADDR=`nismatch cname=$XTNAME hosts.org_dir | awk '{print $3}' \
			| egrep '^[0-9]'`
  if [ $? -ne 0 ]
  then
    exit 8
  else
    echo $IPADDR > $TMP/IPADDR
    exit 7
  fi
fi


if [ $NISH = "true" ]
then
  IPADDR=`ypcat hosts | awk '$2 == '\"$XTNAME\"' {print $1}' | head -1 | egrep '^[0-9]'`
  if [ $? -ne 0 ]
  then
    IPADDR=`ypcat hosts | awk '$3 == '\"$XTNAME\"' {print $1}' |  head -1 | egrep '^[0-9]'`
    if [ $? -ne 0 ]
    then
      if [ $AMHMASTER = "false" ]
      then
        echo ""
        echo "You are running NIS for the hosts database. Please add $XTNAME"
        echo "to the hosts map on $NISHMASTER and re-run this selection."
        echo ""
        exit 1
      fi
    else
      echo $IPADDR > $TMP/IPADDR
      exit 5
    fi 
  else
    echo $IPADDR > $TMP/IPADDR
    exit 5
  fi
fi
    

if [ ! -f $HOSTTABLE ]
then
	echo "Warning: $HOSTTABLE doesn't exist."
	exit 4
elif
	[ ! -r $HOSTTABLE ]
   then
		echo "No read permission on file $HOSTTABLE."
		exit 2
fi

awk '$2 == '\"$XTNAME\"' { print $1}' $HOSTTABLE | head -1 > $TMP/IPADDR 
cat $TMP/IPADDR | egrep '^[0-9].+[.]'  > /dev/null

if [ $? -eq 0 ]
then
	exit 0
else
	SHORTNAME=`echo $XTNAME | grep "\." | awk -F. '{print $1}'`
	if [ $SHORTNAME ]
	then
	  awk '$2 == '\"$SHORTNAME\"' { print $1}' $HOSTTABLE > $TMP/IPADDR
	  cat $TMP/IPADDR | egrep '^[0-9].+[.]'  > /dev/null
	  if [ $? -eq 0 ]
	  then
	    exit 0
	  else
	    exit 4
	  fi
	else
	  exit 4
	fi
fi

