#!/bin/bash

#/*
# * Licensed to Open Source Radio Access Network(OS-RAN) Alliance and OAI
# * Software Alliance under one or more contributor license agreements. The
# * initial OpenXG series projects are derided from OAI projects, the files from
# * OAI projects are all in compliance with OAI Public License, Version 1.1.
# * codes and files developed from OpenXG projects and from OS-RAN Alliance
# * are all under OS-RAN licenses; you may not use this file except in compliance
# * with the license.  You may get a copy of the license at:
# * 	http://www.openxg.org.cn/?falu_69.html
# * For more information about OpenXG, please contact:
# * 	contact@openxg.org.cn
# */

# file init_nas_nos1
# brief loads the nasmesh module and sets up the radio bearers (used to provide ip interface without S1 interface)
# author Florian Kaltenberger
#
#######################################

load_module() 
{
  mod_name=${1##*/}
  mod_name=${mod_name%.*}
  if awk "/$mod_name/ {found=1 ;exit} END {if (found!=1) exit 1}" /proc/modules
    then
      echo "module $mod_name already loaded: I remove it first"
      sudo rmmod $mod_name
  fi
  echo loading $mod_name
  sudo insmod $1
}


function main() 
{
PCI=`lspci -m | grep Xilinx`
if [ -z "$PCI" ]; then
 echo "No card found. Stopping!"
 return
fi

## This part corrects the wrong configuration of the endpoint done by the bios in some machines
echo "$PCI" | while read config_reg; do
SLOT_NUMBER=`echo $config_reg | awk -F\" '{print $1}'`
sudo setpci -s $SLOT_NUMBER 60.b=10
done


load_module $OPENAIR_DIR/targets/bin/openair_rf.ko
sleep 1

if [ ! -e /dev/openair0 ]; then 
 sudo mknod /dev/openair0 c 127 0
 sudo chmod a+rw /dev/openair0
fi

   $OPENAIR_DIR/targets/bin/updatefw -s 0x43fffff0 -b -f $OPENAIR_TARGETS/ARCH/EXMIMO/USERSPACE/OAI_FW_INIT/sdr_expressmimo2_v10
}

main "$@"