#!/bin/sh
# Fail before installation if the selected OS/CPU cannot run the static binaries.
set -eu
fail() { printf '%s\n' "$1" >&2; exit 1; }
version_file=/etc.defaults/VERSION
[ -r "$version_file" ] || version_file=/etc/VERSION
[ -r "$version_file" ] || fail "Cannot identify DSM version."
major=$(sed -n 's/^majorversion="\([0-9]*\)".*/\1/p' "$version_file")
build=$(sed -n 's/^buildnumber="\([0-9]*\)".*/\1/p' "$version_file")
[ "$major" = "6" ] || fail "This package requires DSM 6.x."
case "$build" in ''|*[!0-9]*) fail "Cannot identify DSM build." ;; esac
[ "$build" -ge 7321 ] || fail "DSM build is too old for this package."
[ "$(uname -m)" = "aarch64" ] || fail "Wrong CPU architecture: this package is arm64."
kernel=$(uname -r)
kmajor=${kernel%%.*}
rest=${kernel#*.}
kminor=${rest%%.*}
case "$kmajor:$kminor" in *[!0-9:]*|:*) fail "Cannot identify Linux kernel." ;; esac
if [ "$kmajor" -lt 3 ] || { [ "$kmajor" -eq 3 ] && [ "$kminor" -lt 2 ]; }; then
    fail "This client requires Linux kernel 3.2 or newer."
fi
exit 0
