#!/bin/bash

function usage() {
	echo "Usage : $0 [-h] [init]"
	echo "  -h    Display this message"
	echo "  init  Save current status as "
}

[ "$1" == "-h" ] && usage && exit 0

JOURNALCTL="journalctl -p warning -xb"
STATUS="/var/cache/ee/check_journalctl_change.status"

function get_status() {
	$JOURNALCTL | grep -v 'Logs begin at'
}

if [ "$1" == "init" ]
then
	get_status > $STATUS
	echo "Current status save in $STATUS."
	exit 0
fi

[ ! -f $STATUS ] && echo "UNKNOWN : Status file not initialized. Run $0 init" && exit 3

TMP=$( mktemp )
get_status > $TMP

if [ -n "$( diff $TMP $STATUS )" ]
then
	echo "WARNING : journalctl change !"
	diff $TMP $STATUS
	exit 1
fi

rm -f $TMP
echo "OK : no changes"
exit $?
