#! /bin/sh

set -e

dir=$(pwd)/debian/tests
. $dir/textutils.sh

frame "Here we are testing the bounds allowed" \
      "for day of month and month fields, as stated in crontab(5)"

echo "================= adding user 'dummy' ====================="
getent passwd dummy || adduser --disabled-password --gecos "" dummy

frame "make user 'dummy' fill his crontab with dom = 0" \
      "and check that the error contains 'bad day-of-month'"

export expected="bad day-of-month"
if bad=$(su dummy -c "(export LINES=' * * 0 * * echo hello';
    echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 \
	     | grep "$expected")
then
    if echo "$bad" | grep -q "$expected"; then
	echo OK
    else
	echo "The response '$bad' was not expected"
    fi
else
    echo "a crontab with dom = 0 has raised the error $?"
fi

frame "make user 'dummy' fill his crontab with dom = 31" \
      "and check that the error contains 'bad day-of-month'"

export expected="bad day-of-month"
if bad=$(su dummy -c "(export LINES=' * * 0 * * echo hello';
    echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 \
	     | grep "$expected")
then
    if echo "$bad" | grep -q "$expected"; then
	echo OK
    else
	echo "The response '$bad' was not expected"
    fi
else
    echo "a crontab with dom = 31 has raised the error $?"
fi

frame "make user 'dummy' fill his crontab with month = 0" \
      "and check that the error contains 'bad month'"

expected="bad month"
if bad=$(su dummy -c "(export LINES=' * * * 0 * echo hello';
    echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 \
	     | grep "$expected")
then
    if echo "$bad" | grep -q "$expected"; then
	echo OK
    else
	echo "The response '$bad' was not expected"
    fi
else
    echo "a crontab with month = 0 has raised the error $?"
fi

       
frame "make user 'dummy' fill his crontab with month = 13" \
      "and check that the error contains 'bad month'"

expected="bad month"
if bad=$(su dummy -c "(export LINES=' * * * 13 * echo hello';
    echo 'n' | EDITOR=$(pwd)/debian/tests/cronfiller.py crontab -n -e)" 2>&1 \
	     | grep "$expected")
then
    if echo "$bad" | grep -q "$expected"; then
	echo OK
    else
	echo "The response '$bad' was not expected"
    fi
else
    echo "a crontab with month = 0 has raised the error $?"
fi
