#!/bin/sh

set -e

SIGNATURE_FILE={{ signature_file }}
NSIGNATURE_FILE={{ signature_file }}.tmp
SIGNATURE_STATUS={{ signature_file }}.status

touch $SIGNATURE_FILE
{{ sha256sum }} {{ path_list }} | sort -k 66 > $NSIGNATURE_FILE

if diff "$SIGNATURE_FILE" "$NSIGNATURE_FILE" > /dev/null; then
  rm -f "$NSIGNATURE_FILE"
else
  mv "$NSIGNATURE_FILE" "$SIGNATURE_FILE"
  # do not catch errors during validation
  set +e
  {{ wrapper }} -validate
  echo $? > $SIGNATURE_STATUS
  set -e
fi
exit `cat $SIGNATURE_STATUS`
