# Copyright 2022, FOSS-VG Developers and Contributers
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# version 3 along with this program.
# If not, see https://www.gnu.org/licenses/agpl-3.0.en.html

echo ">>> Loading shell environment for FOSS-VG development..."

export PROJECT_BASE_DIR="$( cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 ; pwd -P )"
echo "Project base directory is $PROJECT_BASE_DIR"

if [ -f "$PROJECT_BASE_DIR/.localenv.bashrc" ]; then
    source "$PROJECT_BASE_DIR/.localenv.bashrc"
    echo "Applied local environment customizations."
fi

alias clean="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/clean.sh; popd >/dev/null 2>&1"
alias clean_dependencies="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/clean_dependencies.sh; popd >/dev/null 2>&1"
alias setup_project="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/setup_project.sh; popd >/dev/null 2>&1"

function run_tests {
    pushd "$PROJECT_BASE_DIR" >/dev/null 2>&1
    scripts/test.sh
    popd >/dev/null 2>&1
}

function build {
    pushd "$PROJECT_BASE_DIR" >/dev/null 2>&1
    scripts/build.sh
    popd >/dev/null 2>&1
}

echo "Added aliases and functions."

export PATH="$PROJECT_BASE_DIR/bin:$PROJECT_BASE_DIR/bin/tools:$PROJECT_BASE_DIR/scripts/tools:$PATH"
if uname -s | tr [:upper:] [:lower:] | grep cygwin >/dev/null; then
    echo "Adding Cygwin workaound for library path."
    export PATH="$PROJECT_BASE_DIR/bin/lib:$PATH"
fi
echo "PATH is $PATH"

if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH="$PROJECT_BASE_DIR"/bin/lib
else
    export LD_LIBRARY_PATH="$PROJECT_BASE_DIR"/bin/lib:"$LD_LIBRARY_PATH"
fi
echo "LD_LIBRARY_PATH is $LD_LIBRARY_PATH"

echo ">>> Checking for dependencies..."
MISSING_DEPS=0

if command -v wget > /dev/null 2>&1; then
    true
else
    if command -v curl > /dev/null 2>&1; then
        true
    else
        echo "WARNING: \`wget\` or \`curl\` is needed to download some additional dependencies."
        MISSING_DEPS=1
    fi
fi

if command -v sha256sum > /dev/null 2>&1; then
    true
else
    if command -v sha256 > /dev/null 2>&1; then
        true
    else
        echo "WARNING: Coreutils \`sha256sum\` or a \`sha256\` as found on NetBSD is needed to verify downloaded files."
        MISSING_DEPS=1
    fi
fi

if command -v gzip > /dev/null 2>&1; then
    true
else
    echo "WARNING: \`gzip\` is needed to decompress downloaded dependencies."
    MISSING_DEPS=1
fi

if command -v tar > /dev/null 2>&1; then
    true
else
    echo "WARNING: \`tar\` is needed to unpack downloaded dependencies."
    MISSING_DEPS=1
fi

if command -v cmake > /dev/null 2>&1; then
    true
else
    echo "WARNING: \`cmake\` is needed to build downloaded dependencies."
    MISSING_DEPS=1
fi

if [ -z "$CXX" ]; then
    if command -v c++ > /dev/null 2>&1; then
        true
    else
        echo "WARNING: Your system does not appear to have a standard C++ compiler. If you have a C++ compiler installed, but not linked to \`c++\` on your PATH, set it manually using \`CXX=/path/to/your/compiler\`."
        MISSING_DEPS=1
    fi
fi

if [ "$MISSING_DEPS" -eq 0 ]; then
    echo "All set."
fi

unset MISSING_DEPS
