#!/bin/bash

PATH_CONF="/opt/fofb/cfg/configuration"

logger -t "fofb-fpgainit" "Reading configuration file ${PATH_CONF}"

if [ ! -f ${PATH_CONF} ]; then
    logger -s -t "fofb-fpgainit" "Configuration file not found: ${PATH_CONF}"
    exit 1
fi

source ${PATH_CONF}

case $FOFB_APP in
    cellnode|centralnode)
        logger -t "fofb-fpgainit" "Found configuration ${FOFB_APP}"
        ;;
    *)
        logger -s -t "fofb-fpgainit" "Could not establish FOFB application"
        exit 1
        ;;
esac

# Taking the last binary file in the sorted list (higher tag)
PATH_FPGABIN=$(ls -S /lib/firmware/base/${FOFB_APP}/*.bin | tail -n 1)
logger -t "fofb-fpgainit" "Loading FPGA image ${PATH_FPGABIN}"
fpgautil -b ${PATH_FPGABIN}


# Linking the correct MAP file
PATH_MAP="/opt/fofb/map/app_{FOFB_APP}.mapt"
logger -t "fofb-fpgainit" "Linking map file ${PATH_MAP}"
ln -s ${PATH_MAP} /opt/fofb/map/app.mapt

