#!/bin/bash

. $(dirname $0)/functions

if [ -n "$MSYSTEM" ]; then
    BUILD=graph5
else
    BUILD=$BUILDDIR/graph5
fi

# Exercise --right-axis-range, --right-axis-label-angle and
# --vertical-label-angle.
#
# The right axis borrows its grid line positions from the left axis, so
# when the two ranges do not line up it gets labelled with values outside
# its own domain. --right-axis-range suppresses those labels.

$RRDTOOL create ${BUILD}.rrd \
    --start 1748087640 --step 60 \
    DS:value:GAUGE:120:U:U \
    RRA:AVERAGE:0.5:1:60
report "create"

is_cached && exit 0

# Populate with 0 .. 100
for i in $(seq 1 20); do
    t=$((1748087640 + i*60))
    v=$((i * 5))
    $RRDTOOL update ${BUILD}.rrd ${t}:${v}
done
report "update"

# The right axis is shifted by 1000, so its labels (1000..1100) are easy to
# tell apart from the left axis labels (0..100). An explicit format keeps
# SI scaling out of the way.
graph_right () {
    $RRDTOOL graph "$1" --imgformat PDF \
        --start 1748087700 --end 1748088840 --height 400 \
        --right-axis 1:1000 --right-axis-format "%.0lf" \
        "${@:2}" \
        DEF:v=${BUILD}.rrd:value:AVERAGE \
        LINE1:v#000000 > /dev/null
}

graph_right ${BUILD}-full.pdf
report "graph without --right-axis-range"

graph_right ${BUILD}-clipped.pdf --right-axis-range :1050
report "graph with --right-axis-range :1050"

# Both label angles are accepted and do not change the geometry
$RRDTOOL graph ${BUILD}-angle.pdf --imgformat PDF \
    --start 1748087700 --end 1748088840 --height 400 \
    --right-axis 1:0 --right-axis-label "right" -v "left" \
    --vertical-label-angle 90 --right-axis-label-angle 270 \
    DEF:v=${BUILD}.rrd:value:AVERAGE \
    LINE1:v#000000 > /dev/null
report "graph with label angles"

# Invalid input must be rejected
$RRDTOOL graph ${BUILD}-bad.pdf --imgformat PDF \
    --right-axis 1:0 --right-axis-range 1050 \
    DEF:v=${BUILD}.rrd:value:AVERAGE LINE1:v#000000 > /dev/null 2>&1
if [ $? = 0 ]; then
    fail 1 "--right-axis-range without a colon should be an error"
else
    ok "--right-axis-range without a colon is rejected"
fi

$RRDTOOL graph ${BUILD}-bad.pdf --imgformat PDF \
    --right-axis 1:0 --right-axis-range 100:10 \
    DEF:v=${BUILD}.rrd:value:AVERAGE LINE1:v#000000 > /dev/null 2>&1
if [ $? = 0 ]; then
    fail 1 "--right-axis-range with min > max should be an error"
else
    ok "--right-axis-range with min > max is rejected"
fi

$RRDTOOL graph ${BUILD}-bad.pdf --imgformat PDF \
    --right-axis 1:0 --right-axis-label-angle sideways \
    DEF:v=${BUILD}.rrd:value:AVERAGE LINE1:v#000000 > /dev/null 2>&1
if [ $? = 0 ]; then
    fail 1 "--right-axis-label-angle with a non number should be an error"
else
    ok "--right-axis-label-angle with a non number is rejected"
fi

# If pdftotext is available, check which right axis labels got drawn
if command -v pdftotext > /dev/null; then
    # right axis labels are the four digit ones, 1000 and up
    FULL=$(pdftotext ${BUILD}-full.pdf - | grep -cE '^1[0-9]{3}$')
    ABOVE=$(pdftotext ${BUILD}-clipped.pdf - | \
            grep -E '^1[0-9]{3}$' | awk '$1 > 1050' | wc -l)
    BELOW=$(pdftotext ${BUILD}-clipped.pdf - | \
            grep -E '^1[0-9]{3}$' | awk '$1 <= 1050' | wc -l)

    if [ "$FULL" -lt 4 ]; then
        fail 1 "expected the unclipped graph to carry right axis labels (got $FULL)"
    elif [ "$ABOVE" != 0 ]; then
        fail 1 "--right-axis-range left $ABOVE label(s) above the range"
    elif [ "$BELOW" -lt 2 ]; then
        fail 1 "--right-axis-range dropped labels inside the range (kept $BELOW)"
    else
        ok "--right-axis-range kept $BELOW label(s) and dropped the $((FULL - BELOW)) outside"
    fi

    # The left axis must be unaffected by --right-axis-range
    LEFT_FULL=$(pdftotext ${BUILD}-full.pdf - | grep -cE '^[0-9]{1,3}$')
    LEFT_CLIP=$(pdftotext ${BUILD}-clipped.pdf - | grep -cE '^[0-9]{1,3}$')
    if [ "$LEFT_FULL" = "$LEFT_CLIP" ]; then
        ok "left axis labels are unchanged ($LEFT_FULL)"
    else
        fail 1 "left axis changed from $LEFT_FULL to $LEFT_CLIP labels"
    fi
else
    ok "pdftotext not available, skipping label checks"
fi
