#!/bin/bash
#
# Note: this script uses bash's ulimit builtin to get the limit values.
# In the ideal world, we'd create our own limit-printing binary which uses
# getrlimit directly and outputs in more readable format.

# @PREAMBLE_START@

# This section is copied from preamble.sh (automatically, during build) and
# should not be edited in place.

# turn on echo for debuging.
if [ ! -z "${VERT_DEBUG_PREAMBLE:-}" ]; then set -x; else set +x; fi
: '# Preamble begin'
: '# # Standardizing shell'
unset IFS # default word splitting on whitespace
set +o history # disable history. Should be default, but some reports otherwise
set -u # disallow unset variables

# note: these two vars are reset by vars.bash, but necessary to get there.
VERT_D=${VERT_VERT_D:-'/opt/vertica'}
BINLIB_D=${VERT_BINLIB_D:-"$VERT_D/share/binlib"}
source "$BINLIB_D/include/vars.bash"
source "$BINLIB_D/include/common_func.bash"
source "$BINLIB_D/include/self_vars.bash"

: '# Preamble end'
set +x # turn echo off
# @PREAMBLE_END@

function usage() {
    v_here_echo << HEREDOC
$SELF_SHORTNAME: Print the given user's limits (soft and hard)

Usage: ./$SELF_SHORTNAME USERNAME

Each line is a limit (by bash \`ulimit\` character), whether it is soft or hard,
and the value - all delimited by commas.
HEREDOC
}

if [ "$EUID" != "0" ]; then
    echo "Must be root. EUID = $EUID" >&2
    usage >&2
    exit 1
fi

shift $(( $OPTIND - 1 ))

if [ "$#" -gt "1"  ]; then
    echo "Too many arguments." >&2
    usage >&2
    exit 1
elif [ "$#" -lt "1" ]; then
    echo "Too few arguments. Need a username" >&2
    usage >&2
    exit 1
fi

TARGETUSER="$1"

# We're assuming that the user has already satisfied the requirement to have
# pam_limits.so included in /etc/pam.d/su.  Therefore, by invoking su with the
# --login flag, the results of the command will provide the user's actual
# limits configuration.
printf -v COMMAND "%q -Sa" "$BIN_ULIMIT"
SOFTLIMITS=`"$BIN_SU" "$TARGETUSER" --login --shell /bin/bash \
    --command "$COMMAND" < /dev/null`

RCODE="$?"
if [ "$RCODE" != "0" ]; then
    echo "Unable to read soft limits (rcode=$RCODE)" >&2
    exit 1
fi

printf -v COMMAND "%q -Ha" "$BIN_ULIMIT"
HARDLIMITS=`"$BIN_SU" "$TARGETUSER" --login --shell /bin/bash \
    --command "$COMMAND" < /dev/null`

RCODE="$?"
if [ "$RCODE" != "0" ]; then
    echo "Unable to read hard limits (rcode=$RCODE)" >&2
    exit 1
fi

function v_print_limits() {
    local LIMIT_TYPE="$1"
    local LIMITS="$2"

    local NAME=""
    local VAL=""
    local REALVAL=""
    local EXTRA=""
    local LINE=""

    # Look up all block devices
    while IFS= read -r LINE; do
        if [ -z "$LINE" ]; then
            # empty line
            continue;
        fi

        # split on close paren
        IFS=')' read -r NAME VAL EXTRA <<< "$LINE"

        if [ ! -z "$EXTRA" ]; then
            echo "Could not interpret line: $LINE" >&2
            # Move onwards, with hopes that it's just one line that's weird.
            continue;
        fi

        # extract the single letter identifier
        case "$NAME" in
            *-? )
                NAME="${NAME: -1}"
                ;;
            * )
                echo "Could not interpret line: $LINE" >&2
                continue
                ;;
        esac

        # get the single word value (i.e. strip)
        read -r REALVAL EXTRA <<< "$VAL"

        if [ ! -z "$EXTRA" -o -z "$REALVAL" ]; then
            echo "Could not interpret line: $LINE" >&2
            continue
        fi

        echo "$NAME,$LIMIT_TYPE,$REALVAL"
    done <<< "$LIMITS"
}

v_print_limits "hard" "$HARDLIMITS"
v_print_limits "soft" "$SOFTLIMITS"

exit 0

# TODO: getopt
