# SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
# SPDX-License-Identifier: Apache-2.0

export_or_unset()
{
    local var=$1

    if [ -z "${!var+x}" ]; then
        return
    fi

    if [ -n "$2" ]; then
        export $var="$2"
    else
        unset $var
    fi

}

if direnv_version "2.30.0" \
&& has nix \
&& nix show-config experimental-features 2>/dev/null | grep -wqF flakes
then
    local oldtmp="$TMP"
    local oldtemp="$TEMP"
    local oldtmpdir="$TMPDIR"
    local oldtempdir="$TEMPDIR"

    watch_file ../packaging/nix/*.nix
    use flake ../packaging/nix#villas-python

    export_or_unset TMP "$oldtmp"
    export_or_unset TEMP "$oldtemp"
    export_or_unset TMPDIR "$oldtmpdir"
    export_or_unset TEMPDIR "$oldtempdir"
fi
