#!/bin/sh 
#
# ident "@(#) hosts2dns 1.1 /dsk2/users/len/named/sol251/SCCS/s.hosts2dns"
# ident "@(#) Last modified 09/30/96 18:02:51"
#
# AUTHOR:	Leonard Giambrone	09/19/96
#
# NAME: 	hosts2dns
#
# USAGE:	hosts2dns
#
# DESCRIPTION:
#
#
# This script takes $HOST, a file in standard "host" format (e.g.  /etc/hosts)
# and creates two DNS zone files for the domain $DOMAIN:
#
# $ZONE		This is the Host to Address mapping file
# $REV		This is the Address to Host mapping file
#
# It does this by using awk to read in $HOSTS, formatting the output, and
# then appending the pertinent data to $ZONE_HEADER and $REV_HEADER.
# The files $HOSTS, $ZONE_HEADER, and $ZONE_REV are expected to be in the 
# current directory before this script is executed.
#
# The variables $FIRST, $SECOND, and possibly $THIRD refer to the first,
# second, and third octets of the Network being configured for.
#
#
# FILES ACCESSED:
#
# See Global Variables section for Variable reference.
# Files which have w access may be created or deleted.
#
# Filename					Access
# --------                      		-----
#
# hosts.master                                    r
# analogic.zone.header                            rw
# analogic.rev.header                             rw
# analogic.zone.new                               w
# analogic.rev.new                                w
#
#
# NON OPERATING SYSTEM PROGRAMS CALLED:
#
# EXIT CODES:
#
#		0	Successful execution
#
# HISTORY:
#
# 09/19/96 	ltg	Initial Version
#
# -----------------------------------------------------------------------------

# Global Variables

DEBUG=${DEBUG:-0}
FIRST=204
SECOND=178
THIRD=
DOMAIN=analogic.com
ZONE=analogic.zone.new
ZONE_HEADER=analogic.zone.header
REV=analogic.rev.new
REV_HEADER=analogic.rev.header
HOSTS=hosts.master


# -----------------------------------------------------------------------------
# Main Routine
# -----------------------------------------------------------------------------

if [ "$DEBUG" -eq 1 ]; then 
   set -xv
fi

mv $ZONE_HEADER $ZONE
mv $REV_HEADER $REV

echo "Making zone files $ZONE $REV for domain $DOMAIN..."

# The following line is specific to this instantiation of the script, but 
# I haven't figured out a better way...yet.

awk 'BEGIN {cur_subnet = 40 - 1}
{

# Split input to get rid of all comments

   split($0, input, "#")

# Split on whitespace to get valid fields

   numf = split(input[1], fields)
   if ((fields[1] != "") && (substr(fields[1], 1, 3) == FIRST)) {

# Split fields[1] for access to IP address octets 
# Split fields[2] to avoid making cnames identical to the hostname

      split(fields[1], ipaddr, ".")
      split(fields[2], canon, ".")

# Place a comment at the beginning of each network

      if ((ipaddr[1] == FIRST) && (ipaddr[3] != cur_subnet)) {
         cur_subnet = ipaddr[3]
         print ";\n; Network "ipaddr[1]"."ipaddr[2]"."cur_subnet".0\n;">>ZONE
         print ";\n; Network "ipaddr[1]"."ipaddr[2]"."cur_subnet".0\n;">>REV
      }

# Print the A records

      if (length(canon[1]) < 8) {
         print canon[1]"\t\tIN\tA\t"fields[1]>>ZONE
      } else {
         print canon[1]"\tIN\tA\t"fields[1]>>ZONE
      }

# Print any valid cnames

      for (i=3; i <= numf; i++) {
         split(fields[i], cname, ".")
         if (cname[1] == canon[1]) continue
         if (length(cname[1]) < 8) 
            print cname[1]"\t\tIN\tCNAME\t"canon[1]>>ZONE
         else 
            print cname[1]"\tIN\tCNAME\t"canon[1]>>ZONE
      }

# Print the PTR records

      if ((ipaddr[1] == FIRST) && (ipaddr[2] == SECOND))
         print ipaddr[4]"."ipaddr[3]"\t\tIN\tPTR\t"canon[1]"."DOMAIN".">>REV
   }      
}' FIRST="$FIRST" SECOND="$SECOND" DOMAIN="$DOMAIN" ZONE="$ZONE" REV="$REV" $HOSTS
