#!/bin/sh

# Put which branch of Git to use here...
IPBUS_BRANCH="-b v1.6"
TLU_BRANCH="-b 1e000024"

echo "Creating working directory"
[ -d work ] && echo "Directory Exists" || mkdir work
cd work

IPBB_VERSION="0.5.2"
echo "Installing IPBB version ${IPBB_VERSION}"
[ -d ipbb-${IPBB_VERSION} ] && echo "ipbb-${IPBB_VERSION} already installed" || curl -L https://github.com/ipbus/ipbb/archive/v${IPBB_VERSION}.tar.gz | tar xvz
# ( or git clone git@github.com:ipbus/ipbb.git )
source ipbb-${IPBB_VERSION}/env.sh

ipbb init build

cd build
ipbb add git https://github.com/ipbus/ipbus-firmware.git ${IPBUS_BRANCH}
ipbb add git https://ohwr.org/project/fmc-mtlu-gw.git ${TLU_BRANCH}
# For read/write load a valid ssh key and use the repo below ....
# ipbb add git ssh://git@ohwr.org/fmc-projects/fmc-mtlu/fmc-mtlu-gw.git 


echo "BUILD: ipbb proj create"
ipbb proj create vivado TLU_1e fmc-mtlu-gw:AIDA_tlu/projects/TLU_v1e -t top_tlu_1e_a35.dep
cd proj/TLU_1e

echo "BUILD: creating VIVADO project"
ipbb vivado project

echo "BUILD: creating IPBus address decoder"
ipbb vivado gendecoders

echo "Copying generated decoder into source tree"
cp decoders/ipbus_decode_TLUaddrmap.vhd ../../src/fmc-mtlu-gw/AIDA_tlu/projects/TLU_v1e/firmware/hdl/ipbus_decode_TLUaddrmap.vhd

# Set TLU timing contraints *.xdc file to have "late" processing order
# vivado -mode tcl -nojournal -nolog -notrace -source ../../src/fmc-mtlu-gw/AIDA_tlu/projects/TLU_v1e/firmware/ucf/TLU_enclustra_v1e_setProcessingOrder.tcl top/top.xpr

echo "BUILD: ipbb impl"
ipbb vivado impl
echo "BUILD: ipbb bitfile"
ipbb vivado bitfile
echo "BUILD: ipbb package"
ipbb vivado package
deactivate
