#!/usr/bin/env sh

# SPDX-License-Identifier: CC0-1.0

# SPDX-FileCopyrightText: 2021-2025 Stefan Schmidt
# SPDX-FileCopyrightText: 2021 Mike Davis

readlink_cmd="readlink"

if [ $(uname -s) = 'Darwin' ]; then
    export PATH=/opt/homebrew/opt/llvm/bin/:/usr/local/opt/llvm/bin/:$PATH
    readlink_cmd="greadlink"
fi

export NXDK_DIR="${NXDK_DIR:-$(dirname $(dirname "$(${readlink_cmd} -f "$0")")../)}"
export PATH="${NXDK_DIR}/bin:$PATH"

CLANG_VERSION=$(clang --version | grep version | grep -o -m 1 "[0-9]\+\.[0-9]\+\.*[0-9]*" | head -n 1)
CLANG_VERSION_NUM=$(echo "$CLANG_VERSION" | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/')

if [ $CLANG_VERSION_NUM -lt 100000 ]; then
    echo You have clang $CLANG_VERSION installed, but nxdk requires at least version 10. You may experience breakage. >&2
elif [ $CLANG_VERSION_NUM -ge 190000 -a $CLANG_VERSION_NUM -lt 200103 ]; then
    echo You have clang $CLANG_VERSION installed. Versions in the 19.x.x-20.1.2 range are affected by a bug that can result in your code breaking when compiled with optimizations turned on. For details, see https://github.com/llvm/llvm-project/issues/134607 >&2
fi

if [ "$1" = "-s" ]; then cat <<- DONE
    export NXDK_DIR="$NXDK_DIR";
    export PATH="$PATH";
DONE
else
    exec "${@:-${SHELL:-sh}}"
fi
