#! /bin/bash

Print_Usage()
{
    echo
    echo "Usage: $0 [-ehiow] [-aAbB date] [-n name] [-l number]"

    echo
    echo "Options:"
    echo "    -a date:   log entries on and after the given date"
    echo "    -b date:   log entries on and before the given date"
    echo "    -A date:   log entries after the given date"
    echo "    -B date:   log entries before the given date"

    echo "    -h:        print this help page"
    echo "    -l number: limit the output to the number of entries or lines"
    echo "    -o:        display the oldest log entry first"

    echo "    -n name:   name of a resource or node"
    echo "    -i:        informative log entries"
    echo "    -w:        warning log entries"
    echo "    -e:        error log entries"

    echo
    echo "Example 1:"
    echo "    $0"
    echo "        print all log entries, the newest entry first."
    echo
    echo "Example 2:"
    echo "    $0 -oe -B'yesterday 16:44'"
    echo "        print error log entries before 16:44 yesterday, the oldest entry first."
    echo
    echo "Example 3:"
    echo "    $0 -w -e -a '10 days ago' -b '10/21/2014 14:23' -nname1_res -nname2_res -n node_name1 -o -l66"
    echo "        print error and warning log entries regarding 'name1_res',"
    echo "        'name2_res', and 'node_name1', the oldest entry first, up to"
    echo "        66 entries from 10 days ago to 14:23 on October 21st, 2014."
    echo
    echo "See also:"
    echo "    date(1) on the format of date arguments for option 'a', 'b', 'A' and 'B'."
    echo

    exit $1
}

cmd="/sbin/sbdutil -r"

rc=0
names=()
severity=0
inclusiveAfter=""
inclusiveBefore=""
exclusiveAfter=""
exclusiveBefore=""
limit=0
direction=0
verbose=0

while getopts ":a:A:b:B:ehil:n:ovw" opt; do
    case $opt in
       a)
           [ 0 -ne $verbose ] && echo "inclusiveAfter='$OPTARG'"
           inclusiveAfter=$(date +%s -d"$OPTARG" 2>/dev/null)
           [ 0 -ne $verbose ] && echo "inclusiveAfter='$inclusiveAfter'"
           if [ "" == "$inclusiveAfter" ]; then
               if [ "-" == ${OPTARG:0:1} ]; then
                   echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               else
                   echo "Invalid date: '$OPTARG' for option -$opt (@argument #$OPTIND)" >&2
               fi
               rc=2
               break;
           fi
           ;;
       b)
           [ 0 -ne $verbose ] && echo "inclusiveBefore='$OPTARG'"
           inclusiveBefore=$(date +%s -d"$OPTARG" 2>/dev/null)
           [ 0 -ne $verbose ] && echo "inclusiveBefore='$inclusiveBefore'"
           if [ "" == "$inclusiveBefore" ]; then
               if [ "-" == ${OPTARG:0:1} ]; then
                   echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               else
                   echo "Invalid date: '$OPTARG' for option -$opt (@argument #$OPTIND)" >&2
               fi
               rc=2
               break;
           fi
           ;;
       A)
           [ 0 -ne $verbose ] && echo "exclusiveAfter='$OPTARG'"
           exclusiveAfter=$(date +%s -d"$OPTARG" 2>/dev/null)
           [ 0 -ne $verbose ] && echo "exclusiveAfter='$exclusiveAfter'"
           if [ "" == "$exclusiveAfter" ]; then
               if [ "-" == ${OPTARG:0:1} ]; then
                   echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               else
                   echo "Invalid date: '$OPTARG' for option -$opt (@argument #$OPTIND)" >&2
               fi
               rc=2
               break;
           fi
           ;;
       B)
           [ 0 -ne $verbose ] && echo "exclusiveBefore='$OPTARG'"
           exclusiveBefore=$(date +%s -d"$OPTARG" 2>/dev/null)
           [ 0 -ne $verbose ] && echo "exclusiveBefore='$exclusiveBefore'"
           if [ "" == "$exclusiveBefore" ]; then
               if [ "-" == ${OPTARG:0:1} ]; then
                   echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               else
                   echo "Invalid date: '$OPTARG' for option -$opt (@argument #$OPTIND)" >&2
               fi
               rc=2
               break;
           fi
           ;;
       n)
           if [ "-" == ${OPTARG:0:1} ]; then
               echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               rc=2
               break
           fi
           names[${#names[*]}]="$OPTARG"
           ;;
       v)
           verbose=1
           ;;
       o)
           direction=1
           ;;
       i)
           ((severity=severity|4))
           ;;
       w)
           ((severity=severity|2))
           ;;
       e)
           ((severity=severity|1))
           ;;
       l)
           if [ "-" == ${OPTARG:0:1} ]; then
               echo "Missing argument for option -$opt (@argument #$OPTIND)" >&2
               rc=2
               break
           fi
           if [ "$OPTARG" -eq "$OPTARG" ] &>/dev/null; then
               limit=$OPTARG
           else
               echo "Natural number expected for option -$opt (@argument #$OPTIND)" >&2
               rc=3
               break;
           fi
           ;;
       h)
           Print_Usage 0
           ;;
       ?)
           if [ "?" == "$OPTARG" ]; then
               rc=0
           else
               echo "invalide option -$OPTARG (@argument #$OPTIND)" >&2
               rc=1
           fi
           Print_Usage $rc
           ;;
    esac
done

[ 0 -ne $verbose ] && echo "rc=$rc"

if [ 0 -eq $rc ]; then
    if [ 0 -ne $verbose ]; then
        echo "inclusiveAfter='$inclusiveAfter'"
        echo "inclusiveBefore='$inclusiveBefore'"
        echo "exclusiveAfter='$exclusiveAfter'"
        echo "exclusiveBefore='$exclusiveBefore'"

        echo "names='${names[@]}'"
        echo "severity=$severity"
        echo "limit=$limit"
        echo "direction=$direction"
    fi

    if [ "" != "$inclusiveAfter" ]; then
        cmd="$cmd -a $inclusiveAfter"
    fi
    if [ "" != "$inclusiveBefore" ]; then
        cmd="$cmd -b $inclusiveBefore"
    fi
    if [ "" != "$exclusiveAfter" ]; then
        cmd="$cmd -P $exclusiveAfter"
    fi
    if [ "" != "$exclusiveBefore" ]; then
        cmd="$cmd -Q $exclusiveBefore"
    fi

    if [ 0 -ne $severity ]; then
        cmd="$cmd -S $severity"
    fi

    if [ 0 -lt ${#names[@]} ]; then
        cmd="$cmd | grep -i \" '${names[0]}' "
        for index in $(seq 1 $((${#names[@]} - 1 ))); do
            cmd="$cmd\\| '${names[$index]}' "
        done
        cmd="$cmd\""
    fi

    if [ 0 -ne $limit ]; then
        if [ 0 -eq $direction ]; then
            cmd="$cmd | tail -$limit"
        else
            cmd="$cmd | head -$limit"
        fi
    fi

    if [ 0 -eq $direction ]; then
        cmd="$cmd | tac"
    fi

    [ 0 -ne $verbose ] && echo "cmd='$cmd'"
    /bin/bash -c "$cmd"
    rc=$?
fi

exit $rc

