#! /bin/sh
#-----------------------------------------------------------------------------
#------------------   FUNCTIONS FOR RESOURCE LIMIT TESTS   -------------------
#-----------------------------------------------------------------------------

. common/report
. common/matchOutput

setup() {
    L=$1
    ULIMIT_DESC=$2
    
    # 'ulimit' may have different outputs depending on the platform and whether it is run with Shell or Bash.

    # get the current soft limit
    CURRENT_SOFT=`ulimit -S -$L`
    if [ "X$CURRENT_SOFT" = "X" ] ; then
        echo "Unable to get the current soft limit for $ULIMIT_DESC."
        exit 1
    fi
        
    # get the current hard limit
    CURRENT_HARD=`ulimit -H -$L`
    if [ "X$CURRENT_HARD" = "X" ] ; then
        echo "Unable to get the current hard limit for $ULIMIT_DESC."
        exit 1
    fi

    # on macosx, the hard limit may be initialized to 'unlimited' but if this value
    #  is decreased, it is not possible to restore it to 'unlimited' later on.
    #  We will resolve below if it is possible or not to set the limit to 'unlimited'.
    if [ "$CURRENT_SOFT" = "unlimited" ] ; then
        CURRENT_SOFT=1000
        ulimit -S -$L $CURRENT_SOFT
        # retrieve the value from the result of the ulimit command in case the limit was resolved to a different value
        CURRENT_SOFT=`ulimit -S -$L`
        if [ "$CURRENT_SOFT" = "unlimited" ] ; then
            # on z/OS (maybe other platforms as well?) the limits for '-d' are always unlimited
            echo "The soft and hard limits of this resource are always unlimited on this platform."
            exit 0
        fi
    fi

    if [ "$CURRENT_HARD" = "unlimited" ] ; then
        CURRENT_HARD=`expr $CURRENT_SOFT + 1000`
        ulimit -H -$L $CURRENT_HARD
        # retrieve the value from the result of the ulimit command in case the limit was resolved to a different value
        CURRENT_HARD=`ulimit -H -$L`
    fi

    # check if we have sufficient permissions to raise the hard limit
    # it is not enough to check the return code of ulimit. For example, on FreeBSD, the function will return 0 even if the limit could not be set to unlimited.
    UNLIMITED_ALLOWED=FALSE
    TEMP_HARD=`expr $CURRENT_HARD + 1`
    ulimit -H -$L $TEMP_HARD
    TEMP_HARD2=`ulimit -H -$L`
    if [ "$TEMP_HARD" = "$TEMP_HARD2" ] ; then
        IS_ROOT=TRUE
        echo "Running the script with sufficient permissions to raise the hard limit."
        if [ $UNLIMITED_ALLOWED != TRUE ] ; then
            TEMP_HARD="unlimited"
            # check if we can raise up to unlimited (depending on the system some resource can't be unlimited)
            ulimit -H -$L $TEMP_HARD
            TEMP_HARD2=`ulimit -H -$L`
            if [ "$TEMP_HARD" = "$TEMP_HARD2" ] ; then
                UNLIMITED_ALLOWED=TRUE
                echo "The hard limit can be raised to unlimited."
            else
                UNLIMITED_ALLOWED=FALSE
                echo "The hard limit cannot be raised to unlimited."
            fi
        fi
    else
        IS_ROOT=FALSE
        echo "Running the script without permissions to raise the hard limit."
    fi

    # restore the hard limit
    ulimit -H -$L $CURRENT_HARD
}

printUlimitHeader() {
    TITLE=$1
    CURRENT_LIMITS="Current soft limit=$CURRENT_SOFT | Current hard limit=$CURRENT_HARD"
    shift
    printHeader "$TITLE" "$CURRENT_LIMITS" "" "$@"
}

runUlimitTestFull() {
    printUlimitHeader "$@"
    shift
    runTest "$@"
    printResult
}
