#!/bin/sh

DISTRIBUTOR="$1"

efi_vars_dir=/sys/firmware/efi/vars
EFI_GLOBAL_VARIABLE=8be4df61-93ca-11d2-aa0d-00e098032b8c
SB="$efi_vars_dir/SecureBoot-$EFI_GLOBAL_VARIABLE/data"

#if we have SB enabled, don't bother to install the unsigned file
if [ -e "$SB" ] && \
   [ "$(( $(printf 0x%x \'"$(cat $SB | cut -b1)") & 1 ))" = 1 ]; then
   SECURE_BOOT="1"
fi

for BINARY in $(find /usr/lib/fwupdate -name '*.efi' -printf "%f\n"); do
  ESP="/boot/efi/EFI/$DISTRIBUTOR"
  ESP_FILE="$ESP/$BINARY"
  SIGNED_FILE="/usr/lib/fwupdate-signed/$BINARY"
  UNSIGNED_FILE="/usr/lib/fwupdate/$BINARY"

  if [ -f "$ESP_FILE" ]; then
    ESP_MD5=$(md5sum $ESP_FILE | sed 's, .*,,')
  fi

  if [ -f "$SIGNED" ]; then
    COMPARE=$SIGNED_FILE
  elif [ -z "$SECURE_BOOT" ]; then
    COMPARE=$UNSIGNED_FILE
  fi

  if [ -n "$COMPARE" ]; then
  	COMPARE_MD5=$(md5sum $COMPARE | sed 's, .*,,')
  	if [ "$COMPARE_MD5" != "$ESP_MD5" ]; then
          rm -f $ESP_FILE
          mkdir -p $ESP
          echo "Installing $BINARY to EFI system partition."
          cp $COMPARE $ESP_FILE
  	fi
  else
    echo "Signed binary for $BINARY is not available, not installing."
  fi
done
