#! /bin/sh
##
## %CopyrightBegin%
##
## Copyright Ericsson AB 2009-2020. All Rights Reserved.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## %CopyrightEnd%
##
#

#
## run-ns
##
## $0 IPAddress PortNum SubDir
##
## Helper script to run a very local name server for this test suite,
## on a given port number, with a configuration
## customized for the test suite.
##
## * Create a work directory ./SubDir, create a $CONF_FILE there
##   that includes $INC_FILE that refers to the zone files.
## * Locate name server and check its version.
## * Zopy zone files and $INC_FILE from `dirname $0`/SubDir to ./SubDir.
## * Start name server in ./SubDir with logging to $LOG_FILE there.
## * Wait for "quit" on stdin.
## * Terminate named and wait for it.
##
## Prints status lines starting with tag and colon (think mail header):
##   Error: have given up, no name server started
##   Running: name server is running, waiting for "quit"
##   Other tags: diagnostics info
#

unset LDPATH CDPATH ENV BASH_ENV
IFS=' 	'
PATH=/usr/sbin:/sbin:/usr/bin:/bin
SHELL=/bin/sh
export PATH SHELL

LOG_FILE=ns.log

CONF_FILE=nsd.conf
INC_FILE=nsd_inc.conf

error () {
    r=$?
    echo "Error: $*"
    exit $r
}

# Check argument: IP address
test :"$1" != : || \
    error "Empty argument 1: IP address !"

# Check argument: Port number
expr "0$2" + 0 '>' 0  '&'  "0$2" + 0 '<' 65536 >/dev/null 2>&1 || \
    error "Invalid argument 2: port number !"

# Check argument: Work/Zone subdir
test :"$3" != : || \
    error "Empty argument 3: Work/Zone subdir!"
SRCDIR="`dirname "$0"`/$3"
test -d "$SRCDIR" || \
    error "Missing zone directory $SRCDIR !"
test -f "$SRCDIR/$INC_FILE" || \
    error "Missing file: $SRCDIR/$INC_FILE !"

# Locate named and check version.
# The bind-named name is used for tricking Apparmor and such
# by copying/hardlinking the real named to that name.
NS=named
for n in /usr/sbin/nsd /usr/bin/nsd /usr/local/sbin/nsd /usr/local/bin/nsd
do
    test -x "$n" && NS="$n" && break
done
NS_VER="`"$NS" -v 2>&1`" || \
    error "Name server not found!"
NS_VER=$(echo "$NS_VER" |
             ( read V1 V2 V3 IGNORED &&
                   echo "$V1 $V3" ) )
NS_FG="-d"

# Create working directory and cd to it
mkdir "$3" >/dev/null 2>&1
cd "$3" >/dev/null 2>&1 || \
    error "Can not cd: $3 !"
DIR="$(pwd)"

# Create $CONF_FILE
cat >"$CONF_FILE" <<CONF_FILE
#
# $CONF_FILE for $NS_VER
# Generated by $0.
#
# Copyright: see $0.
#
server:
    zonesdir:     "$DIR"
    port:          $2
    database:     ""
    zonelistfile: "zone.list"
    pidfile:      "nsd.pid"
    xfrdfile:     ""
    xfrdir:       "$DIR"
    username:     "$LOGNAME"
    verbosity:     2

remote-control:
    control-enable: no

include: "$DIR/$INC_FILE"
CONF_FILE

# Copy all subdir files
( cd "$SRCDIR" && ls -1 ) | while read f; do
    cp -fp "$SRCDIR/$f" .
done

# Start nameserver
echo "Cwd: `pwd`"
echo "Nameserver: $NS_VER"
echo "Port: $2"
echo "ZoneDir: $3"
echo "Command: $NS $NS_FG -c $CONF_FILE" -t "$3"
$NS $NS_FG -c "$CONF_FILE" >"$LOG_FILE" 2>&1 </dev/null &
NS_PID=$!
echo "Pid: $NS_PID"
trap "kill $NS_PID >/dev/null 2>&1; wait $NS_PID >/dev/null 2>&1" \
    0 1 2 3 15

sleep 5 # Give name server time to load its zone files

if ps -p $NS_PID >/dev/null 2>&1 || ps p $NS_PID >/dev/null 2>&1; then
    echo "Running: Enter \`\`quit'' to terminate nameserver[$NS_PID]..."
    while read LINE; do
	test :"$LINE" = :'quit' && break
    done
    echo "Closing: Terminating nameserver..."
else
    error "$NS failed to start"
fi
