#!/bin/sh

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

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

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

echo "BUILD: Initializing build area"
ipbb init build

cd build

echo "BUILD: Checking out TLU code and IPBus code"
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 top_tlu_1e_a35.dep
cd proj/TLU_1e

# Uncomment out the following lines if the address map ( .xml ) has been changed.
# echo "BUILD: creating IPBus address decoder"
# ipbb vivado gendecoders

echo "BUILD: creating VIVADO project"
ipbb vivado project

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

echo "BUILD: Finished. Deactivating ipbb"
deactivate
