#!/bin/bash

set -e

pkg=anacron

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
  trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi

cp debian/anacrontab $AUTOPKGTEST_TMP/anacrontab.new

cd $AUTOPKGTEST_TMP

mkdir spool.new
mkdir job_output

# generate "old" spool timestamps
function set_spool
{
    date -d "yesterday" +"%Y%m%d" > spool.new/test.daily
    date -d "-7days" +"%Y%m%d" > spool.new/test.weekly
    date -d "-31days" +"%Y%m%d" > spool.new/test.monthly
    date -d "-1year" +"%Y%m%d" > spool.new/test.yearly
}

set_spool

# generate anacrontab jobs
echo -e "@daily\t1\ttest.daily\techo \"\`date\` daily\" > $AUTOPKGTEST_TMP/job_output/test.daily" >> $AUTOPKGTEST_TMP/anacrontab.new
echo -e "@weekly\t1\ttest.weekly\techo \"\`date\` weekly\" > $AUTOPKGTEST_TMP/job_output/test.weekly" >> $AUTOPKGTEST_TMP/anacrontab.new
echo -e "@monthly\t1\ttest.monthly\techo \"\`date\` monthly\" > $AUTOPKGTEST_TMP/job_output/test.monthly" >> $AUTOPKGTEST_TMP/anacrontab.new
echo -e "@yearly\t1\ttest.yearly\techo \"\`date\` yearly\" > $AUTOPKGTEST_TMP/job_output/test.yearly" >> $AUTOPKGTEST_TMP/anacrontab.new

# print version
anacron -V

# print help message
anacron -h

for anacrontab in /etc/anacrontab $AUTOPKGTEST_TMP/anacrontab.new anacrontab.new; do

    for spooldir in /var/spool/anacron $AUTOPKGTEST_TMP/spool.new spool.new; do

        echo "Testing with anacrontab=$anacrontab and spooldir=$spooldir"

        # check anacrontab is valid
        echo -e "check anacrontab is valid...\c"
        anacron -T -t $anacrontab
        if [ "$?" == 0 ] ; then echo -e " PASS"; else echo -e " FAIL"; fi

        # check obsolete jobs are displayed
        echo -e "check obsolete jobs are displayed...\c"
        result=$(anacron --list-obsolete-timestamp-files -S $spooldir -t $anacrontab)
        if [[ "$anacrontab" == "/etc/anacrontab" && "$spooldir" != "/var/spool/anacron" ]] ; then
            if [ "$result" != "" ] ; then echo -e " PASS"; else echo -e " FAIL"; fi
        else
            if [ "$result" == "" ] ; then echo -e " PASS"; else echo -e " FAIL"; fi
        fi

        # check jobs run (except default)
        if [ "$anacrontab" != "/etc/anacrontab" ] ; then
            if [ "$spooldir" != "/var/spool/anacron" ] ; then
                echo -e "check jobs running with delay...\c"
                anacron -S $spooldir -t $anacrontab
                sleep 65
                result_length=$(cat $AUTOPKGTEST_TMP/job_output/* | wc -l)
                if [ "$result_length" == 4 ] ; then echo -e " PASS"; else echo -e " FAIL"; fi

                rm $AUTOPKGTEST_TMP/job_output/*
                set_spool

                # check jobs run without delay
                echo -e "check jobs run without delay...\c"
                anacron -n -S $spooldir -t $anacrontab
                sleep 5
                result_length=$(cat $AUTOPKGTEST_TMP/job_output/* | wc -l)
                if [ "$result_length" == 4 ] ; then echo -e " PASS"; else echo -e " FAIL"; fi

                rm $AUTOPKGTEST_TMP/job_output/*
                set_spool

                # check updating timestamps
                echo -e "check updating timestamps...\c"
                anacron -u -S $spooldir -t $anacrontab
                sleep 5
                result_length=$(grep -r "`date +\"%Y%m%d\"`" $spooldir | wc -l)
                if [ "$result_length" == 8 ] ; then echo -e " PASS"; else echo -e " FAIL"; fi

                # check forcing jobs to run
                echo -e "check forcing jobs to run...\c"
                anacron -f -S $spooldir -t $anacrontab
                sleep 65
                result_length=$(cat $AUTOPKGTEST_TMP/job_output/* | wc -l)
                if [ "$result_length" == 4 ] ; then echo -e " PASS"; else echo -e " FAIL"; fi
            fi
        fi
    done
done
