#!/bin/bash

rmmod fmc_adc_100m14b   2> /dev/null
rmmod zio               2> /dev/null
rmmod rawrabbit         2> /dev/null
rmmod spec              2> /dev/null
rmmod fmc               2> /dev/null
rmmod cp210x            2> /dev/null


topdirname="fmcmasterfip"
projectpath="."
LOGDIR="${HOME}/Desktop/pts_log/fmcmasterfip"

#######################################################################
############# Getting absolute path for project #######################
#######################################################################

if [ -n "$1" ]
then
    top="$1"
else
    prg=$0

    if [ ! -e "$prg" ]; then
      case $prg in
        (*/*) exit 1;;
        (*) prg=$(command -v -- "$prg") || exit;;
      esac
    fi
    dir=$(
      cd -P -- "$(dirname -- "$prg")" && pwd -P
    ) || exit
    prg=$dir/$(basename -- "$prg") || exit 
    
    top=$prg
    while true
    do
        if [ `basename "$top"` == "$topdirname" ]
        then
            break;
        else
            top=`dirname "$top"`
        fi
    done
fi


# path verification

if [ ! -d "${top}/${projectpath}/software" ]; then
    echo "Couldn't find project path!"
    echo "Please make sure that top directory of PtsFmcMastrefip is called \"${topdirname}\""
    exit 1
fi

echo "Identified $top as FMC masterFip top directory"

insmod "${top}/${projectpath}/software/fmc-bus/kernel/fmc.ko" 
insmod "${top}/${projectpath}/software/spec-sw/kernel/spec.ko" "fw_name=../../${top}/${projectpath}/gateware/spec-init.bin-2012-12-14"
# insmod "${top}/${projectpath}/software/zio/zio.ko"
# insmod "${top}/${projectpath}/software/fmc-adc-100m14b4cha-sw/kernel/fmc-adc-100m14b.ko" "gateware=../../${top}/${projectpath}/gateware/syn/spec/spec_top_fmc_masterfip.bin"
modprobe usbserial
insmod "${top}/${projectpath}/software/cp210x/cp210x.ko"

