#!/bin/sh 
#
# ident "@(#) update_serial 1.1 /dsk2/users/len/named/sol251/SCCS/s.update_serial"
# ident "@(#) Last modified 09/30/96 18:02:52"
#
# AUTHOR:	Leonard Giambrone	09/19/96
#
# NAME: 	update_serial
#
# USAGE:	update_serial zone_file
#
# DESCRIPTION:
#
# 
# This script takes a DNS zone file and updates the serial number.
# It calculates the new serial number by looking at the currently configured
# version of zone_file.  If the date portion is the same, the same date is
# used, and the version is incremented and left-padded with a 0 if necessary.
# If the date portion is different, or the file does not exist, the current
# date is used with a version of "01".
#
# The file is then updated using ed, and displays the entire SOA record.
# This depends on the fact that the serial number is on the 2nd line following
# the declaration of the SOA record.
#
#
# FILES ACCESSED:
#
# See Global Variables section for Variable reference.
# Files which have w access may be created or deleted.
#
# Filename					Access
# --------                      		-----
#
# $NAMED_DIR/$prodfile				  r
# zone_file					  rw
#
#
# NON OPERATING SYSTEM PROGRAMS CALLED:
#
# EXIT CODES:
#
#		0	Successful execution
#
# HISTORY:
#
# 09/19/96 	ltg	Initial Version
#
# -----------------------------------------------------------------------------

# Global Variables

DEBUG=${DEBUG:-0}
NAMED_DIR=/etc

curr_date=`date '+%y%m%d'`
new_serial="${curr_date}01"
prodfile=`echo $1 | sed 's/\.new//'`
prodfile=`basename $prodfile`


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

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

if [ -f "$NAMED_DIR/$prodfile" ]; then

   last_serial=`grep serial "$NAMED_DIR/$prodfile" | grep -v "^#" | head -1 | awk '{print $1}'`
   last_date=`echo $last_serial | awk '{print substr($1, 1, 6)}'`
   last_vers=`echo $last_serial | awk '{print substr($1, 7, 2)}'`

# If the dates are the same, increment version

   if [ "$curr_date" = "$last_date" ]; then
      curr_vers=`expr $last_vers + 1`

# LeftPad curr_vers with "0" if necessary

      if [ `expr "$curr_vers" : '.*'` -eq 1 ]; then
         curr_vers="0$curr_vers"
      fi
      new_serial="$last_date$curr_vers"
   fi
fi

echo ""
echo "Updating serial number in $1 to be $new_serial...."
echo ""

# The serial entry MUST be 2 lines down from the SOA declaration!

ed - $1 <<EOF
/SOA/
.+2s/[0-9][0-9]*/$new_serial/
/^(/,/)/p
w
q
EOF

