#!/usr/bin/env bash -e
###############################################################################
#
# Copyright (C) 2018 
# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# $Id$
#
###############################################################################

#
# Initialisation script to setup the environment variables for this branch
#

# 
# Make sure it is sourced and no one accidentally gave the script execution rights and just executes it.
if [[ "$_" == "${0}" ]]; then
    echo "ERROR: Use this command with '. ' or 'source '"
    sleep 1
    exit
fi

# if exist user_components.ipx file/link remove it and make a symlink to the minimal_user_components.ipx file
# Altera/[quartus_version]/ip/altera/user_componets.ipx should be a symbolic link to Altera/user_components.ipx
user_components_file="${ALTERA_DIR}/user_components.ipx"
if [ -f $user_components_file ]; then
  rm $user_components_file
fi
# make a new symbolic link to the git version
ln -s ${RADIOHDL_WORK}/minimal_user_components.ipx $user_components_file

# Figure out where this script is located and set environment variables accordingly
export RADIOHDL_WORK="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# setup paths to build and config dir if not already defined by the user.
export HDL_BUILD_DIR=${HDL_BUILD_DIR:-${RADIOHDL_WORK}/build}
echo "HDL environment will be setup for" $RADIOHDL_WORK

if [ -z "${RADIOHDL_GEAR}" ]; then
    . ../radiohdl/init_radiohdl.sh
fi
