#!/bin/bash

## Copyright (C) 2026 - 2026 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

export LC_ALL=C

## Compile to a temporary file first, then atomically move into place.
## This prevents leaving a corrupted binary if compilation is interrupted.
tmp_output="$(mktemp -- /usr/bin/fm-shim-backend.XXXXXX)"
trap 'rm -f -- "${tmp_output}"' EXIT

/usr/libexec/security-misc/compile-fm-shim-backend \
  /usr/src/security-misc/fm-shim-backend.c \
  "${tmp_output}" \
  || {
    printf "%s\n" 'Could not compile fm-shim-backend executable!'
    rm -f -- "${tmp_output}"
    exit 1
  }

chmod 0755 -- "${tmp_output}"
mv -f -- "${tmp_output}" /usr/bin/fm-shim-backend
trap - EXIT
