#!/bin/sh
#
# Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


# This script is used for testing the ProcessSpawn utility class. This
# class is used to fork and execute a new process. It also allows for
# checking the exit code returned when the process terminates.
# The unit tests execute this script via ProcessSpawn class with
# different command line parameters to test the class functionality.
#
# In particular, they check if the class correctly records the exit code
# returned. The exit code returned is controlled by the caller. It is
# possible to explicitly specify the exit code to be returned using
# the command line options. It is also possible to specify that the
# exit code is "unique" for the process, so as the test can check
# that two distinct processes spawned by the same ProcessSpawn
# object may return different status code. The command line of this
# script also allows for forcing the process to sleep so as the
# test has much enough time to verify that the convenience methods
# checking the state of the process, i.e. process running or not.

exit_code=

while [ ! -z "${1}" ]
do
    option=${1}
    case ${option} in
        -p)
            exit_code=$$
            ;;
        -e)
            shift
            exit_code=${1}
            ;;
        -s)
            shift
            sleep ${1}
            ;;
        *)
            exit 123
            ;;
    esac
    shift
done

# The exit code of 32 is returned when no args specified or
# when only the -s arg has been specified.
if [ -z "${exit_code}" ]; then
    exit 32;
fi

exit ${exit_code}
