#!/bin/bash
# globals-------------------------------------------------------------------
script_name="${0##*/}"
script_dir=$(readlink -f "${0%/*}")
script_version=1
# this can be modded to be supplied from command line
named_dir='/var/named'
serialfile="ufis-common.txt"
# main----------------------------------------------------------------------
[[ "$1" ]] && {
(( ${#1} == 10 )) && newserial=$1
(( ${#1} == 8 )) && date=$1
(( ${#1} == 2 )) && revision=$1
! [[ "$1" =~ ^[0-9]+$ ]] || [[ -z "$newserial$date$revision" ]] && echo "$script_name: ERROR: can't do anything with $1" && exit 1
}
curserial="$( sed -ne 's/^.*\([0-9]\{10\}\).*/\1/g; /^[0-9]*$/p' "$named_dir/$serialfile" )"
# if we haven't been passed a serial lets build one
[[ -z "$newserial" ]] && {
# if we don't have a date get current
[[ -z "$date" ]] && date="$(date +%Y%m%d)"
# if we don't have revision number get next from the current, if current=99 or date<>today it'll be "01"
[[ -z "$contador" ]] && {
revision="01"
(( ${curserial:0:8} == $date )) && {
revision=$( printf "%02d" $(( 10#${curserial:8:2}+1 )) )
(( 10#$revision == 100 )) && echo "$script_name: warning: revision counter looped to 01" && revision="01"
}
}
newserial=$date$revision
}
# replace the serial
# some sanity checks here won't hurt
# this is a minimal one
(( ${#newserial} == 10 )) && {
echo "$script_name: new serial number $newserial"
sed -i "s/\(^.*\)$curserial\(.*$\)/\1$newserial\2/" "$named_dir/$serialfile"
}