#!/bin/bash

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

#set -x
set -e

## 'adduser is a debianism' is required for 'anondate' which uses
## 'journalctl'. Group 'systemd-journal' might not exist yet during the build
## process, but 'Depends: systemd' would be a too heavy dependency and might
## cause dependency issues during the build process or distro morphing because
## package 'systemd' as well as package 'sdwdate' 'Provides: time-daemon'.
## 'adduser' is a debianism.
adduser sdwdate systemd-journal 2>/dev/null || true

gcc_hardening_options=(
  "-O2" "-Wall" "-Wextra" "-Wformat" "-Wformat=2" "-Wconversion"
  "-Wimplicit-fallthrough" "-Werror=format-security" "-Werror=implicit"
  "-Werror=int-conversion" "-Werror=incompatible-pointer-types"
  "-Wformat-overflow" "-Wformat-signedness" "-Wnull-dereference" "-Winit-self"
  "-Wmissing-include-dirs" "-Wshift-negative-value" "-Wshift-overflow"
  "-Wswitch-default" "-Wuninitialized" "-Walloca" "-Warray-bounds"
  "-Wfloat-equal" "-Wshadow" "-Wpointer-arith" "-Wundef" "-Wunused-macros"
  "-Wbad-function-cast" "-Wcast-qual" "-Wcast-align" "-Wwrite-strings"
  "-Wdate-time" "-Wstrict-prototypes" "-Wold-style-definition"
  "-Wredundant-decls" "-Winvalid-utf8" "-Wvla" "-Wdisabled-optimization"
  "-Wstack-protector" "-Wdeclaration-after-statement" "-Wtrampolines"
  "-Wbidi-chars=any,ucn" "-Wformat-overflow=2" "-Wformat-truncation=2"
  "-Wshift-overflow=2" "-Wtrivial-auto-var-init" "-Wstringop-overflow=3"
  "-Wstrict-flex-arrays" "-Walloc-zero" "-Warray-bounds=2"
  "-Wattribute-alias=2" "-Wduplicated-branches" "-Wduplicated-cond"
  "-Wcast-align=strict" "-Wjump-misses-init" "-Wlogical-op" "-U_FORTIFY_SOURCE"
  "-D_FORTIFY_SOURCE=3" "-fstack-clash-protection" "-fstack-protector-all"
  "-fno-delete-null-pointer-checks" "-fno-strict-aliasing"
  "-fsanitize=address,undefined" "-fno-sanitize-recover=all"
  "-fstrict-flex-arrays=3" "-ftrivial-auto-var-init=pattern" "-fPIE"
)

gcc_machine="$(gcc -dumpmachine)"
if [ "${gcc_machine}" = 'x86_64-linux-gnu' ]; then
  gcc_hardening_options+=( '-fcf-protection=full' )
elif [ "${gcc_machine}" = 'aarch64-linux-gnu' ]; then
  gcc_hardening_options+=( '-mbranch-protection=standard' )
fi

gcc_hardening_options+=(
  "-Wl,-z,nodlopen" "-Wl,-z,noexecstack" "-Wl,-z,relro" "-Wl,-z,now"
  "-Wl,--as-needed" "-Wl,--no-copy-dt-needed-entries" "-pie"
)

gcc -g /usr/src/sdwdate/sclockadj.c -o /usr/libexec/sdwdate/sclockadj "${gcc_hardening_options[@]}"

systemd-notify --ready
