#!/usr/bin/bash
ACTION_SWITCH=${1}

case ${ACTION_SWITCH} in
"check")
    sha256hmac -c /etc/libvirt/journals.hmac &> /var/log/lvj.log
    res=$?
    if [[ ${res} -gt 0 ]]; then
        [[ -f /etc/libvirt/journals.hmac ]] && ACTION="WRONG" || ACTION="NO_CHECKSUM"
        /usr/libexec/libvirt-log-helper journal-checksum --action="${ACTION}"
    elif [[ ${res} -eq 0 ]]; then
        /usr/libexec/libvirt-log-helper journal-checksum --action=OK
    fi
;;

"write")
    SYSTEM_STATUS="$(systemctl is-system-running)"
    if [[ "${SYSTEM_STATUS}" == "stopping" ]]; then
        [[ -f "/tmp/.shutdown" ]] && CAUSE=$(cat /tmp/.shutdown) || CAUSE="stopped by user"
        [[ ${#CAUSE} -gt 64 ]] && CAUSE=${CAUSE:0:64}
        /usr/libexec/libvirt-log-helper shutdown --cause "${CAUSE}"
        rm -f /tmp/.shutdown
    fi
    sha256hmac /var/log/pam.ext.log /var/log/libvirt/libvirt.log > /etc/libvirt/journals.hmac
;;

*)
;;
esac