set -e

usage() {
    echo "usage: $0 [variants] [--debug] [--diagnostic] [--test caseid] [--cpri/--ecpri/--jesd] [--pcie] [--mask bitmap]" 1>&2
    msg=${1:-}
    if [ ! -z "${msg}" ]; then
        echo "error:$1"
    fi
    exit 1
}

variants=
debug_option="no"
diagnostic_option="no"
fronthaul_option="cpri"
backhaul_option="tmac"
#osp_test_option="no"
#fh_test_option="no"
test_option="no"
#board_option="evb"

#osp_case_id=0
#cpri_case_id=34
#ecpri_case_id=100
#jesd_case_id=43
#fh_case_id=
case_id=21
ape_core_mask=0xf
platform_build_data=0x20230926

while [[ "$#" > 0 ]]; do
    case $1 in
        --debug) debug_option="yes"; shift;;     
        --diagnostic) diagnostic_option="yes"; shift;;   

#        --evmy) board_option="evmy"; shift;;   
        --cpri) fronthaul_option="cpri"; shift;;  
        --ecpri) fronthaul_option="ecpri"; shift;;  
        --jesd) fronthaul_option="jesd"; shift;;   
        --pcie) backhaul_option="pcie"; shift;; 
		
        --mask)              
            if [[ ! -z "$2" ]] && [[ -n "$(echo $2 | sed -n " /^[0-9]\+$/p")" ]]; then 
                ape_core_mask=$2; shift;
            fi
            shift;;

        --data)              
            if [[ ! -z "$2" ]] && [[ -n "$(echo $2 | sed -n " /^[0-9]\+$/p")" ]]; then 
                platform_build_data=$2; shift;
            fi
            shift;;

        --test) test_option="yes";             
            if [[ ! -z "$2" ]] && [[ -n "$(echo $2 | sed -n " /^[0-9]\+$/p")" ]]; then 
                case_id=$2; shift;
            fi
            shift;;   
        
        --*| -*) usage "unknown option $1"; exit 1;;
        *) variants+=" $1"; shift;;
    esac;
done

if [[ "${variants}" == "" ]]; then
    make_list="ape_spu ecs_rfm_spu0 ecs_rfm_spu1 pet_rfm_spu0 pet_rfm_spu1"
else   
    make_list=${variants}
fi

DIR_SCRIPT=$(cd `dirname "$0"`;pwd)
#echo "# script_dir:${DIR_SCRIPT}"
export DIR_ROOT=${DIR_SCRIPT}/public
export BUILD_DIR=${DIR_SCRIPT}/build

#if [ "${board_option}" == "evmy" ]; then
#    BOARD_DIR=${DIR_SCRIPT}/EVMY
#else
#    BOARD_DIR=${DIR_SCRIPT}/EVB
#fi    
#export BOARD_DIR
#echo "# BOARD_DIR:${BOARD_DIR}"

export MAKE_DIR=${DIR_ROOT}/make
RTOS_DIR=${DIR_ROOT}/rtos/lib

if [ -d ${BUILD_DIR} ]; then
    rm -rf ${BUILD_DIR}
fi

if [ -d ${RTOS_DIR} ]; then
    rm -rf ${RTOS_DIR}
fi

#export board_option=${board_option} 
export diagnostic_option=${diagnostic_option} 
export debug_option=${debug_option} 
export fronthaul_option=${fronthaul_option} 
export backhaul_option=${backhaul_option}
export test_option=${test_option}
export test_id=${case_id}
export ape_core_mask=$((ape_core_mask))
export platform_build_data=$((platform_build_data))
#echo "# ape_core_mask:$ape_core_mask,platform_build_data:$platform_build_data"
printf "#ape_core_mask[0x%x], #platform_build_data[0x%x]\n" $ape_core_mask $platform_build_data 

make -f  ${MAKE_DIR}/makefile.lib
makefile_set="makefile.ape_spu makefile.ecs_rfm_spu0 makefile.ecs_rfm_spu1 makefile.pet_rfm_spu0 makefile.pet_rfm_spu1"

for list in ${make_list}; do
    MAKEFILE=makefile.${list}    
    if ! [[ "${makefile_set}" =~ "${MAKEFILE}" ]]; then
        echo "error:unknown makefile name[${MAKEFILE}]"
        exit 2
    fi

    if [ "${list}" == "ape_spu" ]; then
        for ((ape_id=0; ape_id<8; ape_id++)); do
            valid_flag=$((${ape_core_mask}>>ape_id &1));
            echo "ape_core_mask=${ape_core_mask}, ape_id=${ape_id}, valid_flag=${valid_flag}"
            if [ ${valid_flag} -eq 1 ]; then
                ape_list=${list}${ape_id}
                LIB_TARGET_DIR=${BUILD_DIR}/${list}/lib
                mkdir -p ${LIB_TARGET_DIR}
                cp -f ${RTOS_DIR}/${list}/*  ${LIB_TARGET_DIR}/  
                echo "###### start compiling ${ape_list}..."
                if [ ${ape_id} -eq 0 ]; then
                    make all -f  ${MAKE_DIR}/${MAKEFILE} build_variable=${list} ape_id=${ape_id}
                else
                    make bin -f  ${MAKE_DIR}/${MAKEFILE} build_variable=${list} ape_id=${ape_id}
                fi
                echo -e "\n"
                echo -e "\n"
            fi
        done       
    else
        LIB_TARGET_DIR=${BUILD_DIR}/${list}/lib
        mkdir -p ${LIB_TARGET_DIR}
        cp -f ${RTOS_DIR}/rfm_spu/*  ${LIB_TARGET_DIR}/ 
        echo "###### start compiling ${list}..."

        if ! [[ "${makefile_set}" =~ "${MAKEFILE}" ]]; then
            echo "error:unknown makefile name[${MAKEFILE}]"
            exit 2
        fi        
        make -f  ${MAKE_DIR}/${MAKEFILE} build_variable=${list}
        echo -e "\n"
    fi
done

rm -rf ${RTOS_DIR}