#!/bin/sh

set -e
SIGNATURE_FILE={{ signature_file }}
NSIGNATURE_FILE={{ signature_file }}.tmp

touch $SIGNATURE_FILE
rm -f $NSIGNATURE_FILE $NSIGNATURE_FILE.wrk
set +e
{%- for path in path_list.split() %}
{%-   set directory = os_module.path.dirname(path) %}
{%-   set name = os_module.path.basename(path) %}
{{ find_executable }} {{ directory }} -type f -name '{{ name }}' -exec {{ sha256sum }} {} \; >> $NSIGNATURE_FILE.wrk
{%- endfor %}
set -e
sort -k 2,2 $NSIGNATURE_FILE.wrk > $NSIGNATURE_FILE
rm -f $NSIGNATURE_FILE.wrk

if diff "$SIGNATURE_FILE" "$NSIGNATURE_FILE" > /dev/null ; then
  # No changes since last run just propagate information
  rm -f ${NSIGNATURE_FILE}
  exit 1
else
  # Changes since last run, so store new value and propagate information
  mv "$NSIGNATURE_FILE" "$SIGNATURE_FILE"
  exit 0
fi
