#!/bin/bash
totalDataSize=0
totalBssSize=0
dataSize=0
bssSize=0
#objectFile=$1
bssThreshold=$1
dataThreshold=$2
# Hardcoded array with module names and paths
declare -A moduleArray=(
["logger"]="./exo_fw/logger/"
["obc"]="./exo_core/obc/"
["nvm"]="./exo_fw/nvm/"
["state_machine"]="./exo_fw/state_machine/"
["adcs_intf"]="./exo_lib/adcs_intf/common/"
["cubespace/lib"]="./exo_lib/adcs_intf/cubespace/lib/"
["cubsepace/vapi"]="./exo_lib/adcs_intf/cubespace/vapi/"
["comms_intf/sband/prop_cli/"]="./exo_lib/comms_intf/sband/prop_cli/"
["comms_intf/sband/srs_4/"]="./exo_lib/comms_intf/sband/srs_4/"
["/comms_intf/sband/"]="./exo_lib/comms_intf/sband/"
["comms_intf/uhf/"]="./exo_lib/comms_intf/uhf/"
["comms_intf/xband/"]="./exo_lib/comms_intf/xband/"
["file_transfer"]="./exo_lib/file_transfer/"
["tc_tm/lib"]="./exo_lib/tc_tm/lib/"
["api_req_api_resp"]="./exo_lib/api_req_api_resp/"
["exo_ipcc"]="./exo_lib/exo_ipcc/"
["hmac"]="./exo_lib/security/hmac/"
["libcrc"]="./exo_lib/security/libcrc-2.0/"
["ipc_mbmr"]="./exo_os/exo_osal/ipc_mbmr/"
["exo_osal/memory_management/"]="./exo_os/exo_osal/memory_management/"
["/exo_osal/task_management/"]="./exo_os/exo_osal/task_management/"
["exo_ral/cpu_arch/stm32f7xx/"]="./exo_os/exo_ral/cpu_arch/stm32f7xx/"
["exo_ral/exo_ral_common/"]="./exo_os/exo_ral/exo_ral_common/"
["ut"]="./exo_os/ut/"
["adcs_ctrlr"]="./exo_services/adcs_ctrlr/"
["comms_ctrlr/sband/"]="./exo_services/comms_ctrlr/sband/"
["comms_ctrlr/uhf/"]="./exo_services/comms_ctrlr/uhf/"
["comms_ctrlr/xband/"]="./exo_services/comms_ctrlr/xband/"
["eps_ctrlr"]="./exo_services/eps_ctrlr/"
["otsu"]="./exo_services/exo_otsu/"
["module_config"]="./exo_services/exo_otsu/module_config/"
["scheduler"]="./exo_services/scheduler/"
["tc_tm_mngr"]="./exo_services/tc_tm_mngr/"
["libcsp"]="./exo_stack/libcsp/"
# Add more modules as needed
)
# Iterate All the modules in the list and calculate the .bss and .data sizes
echo "===================Module wise size================================================"

printf "%-30s %-15s %-15s %-15s\n" "Module Name" ".data Size" ".bss Size" "total ram size"
echo "------------------------------------------------------------------------------------"
for moduleName in "${!moduleArray[@]}"; do
    totalDataSize=0
    totalBssSize=0

    for objectFile in ${moduleArray[$moduleName]}/obj/src/*.o; do
        dataSize=$(objdump -h "$objectFile" | awk '/\.data/ {print $3}')
        bssSize=$(objdump -h "$objectFile" | awk '/\.bss/ {print $3}')

        # Check if dataSize and bssSize are not empty
        if [ -n "$dataSize" ]; then
            dataSize=$(printf "%d" 0x"$dataSize")
            totalDataSize=$((totalDataSize + dataSize))
        fi

        if [ -n "$bssSize" ]; then
            bssSize=$(printf "%d" 0x"$bssSize")
            totalBssSize=$((totalBssSize + bssSize))
        fi
    done
    totalRamSize=$((totalDataSize + totalBssSize))
    printf "%-30s %-15s %-15s %-15s\n" "$moduleName" "$totalDataSize" "$totalBssSize" "$totalRamSize"
done

echo "===================Object file wise size=============================================================="

printf "%-50s %-15s %-15s %-15s\n" "Object File" ".data Size" ".bss Size" "total ram size"
echo "------------------------------------------------------------------------------------------------------"
for objectFile in ./obj/*.o; do
    dataSize=$(objdump -h "$objectFile" | awk '/\.data/ {print $3}')
    bssSize=$(objdump -h "$objectFile" | awk '/\.bss/ {print $3}')

    # Check if dataSize and bssSize are not empty
    if [ -n "$dataSize" ]; then
        dataSize=$(printf "%d" 0x"$dataSize")
    fi

    if [ -n "$bssSize" ]; then
        bssSize=$(printf "%d" 0x"$bssSize")
    fi

    totalRamSize=$((dataSize + bssSize))
    printf "%-50s %-15s %-15s %-15s\n" "$objectFile" "$dataSize" "$bssSize" "$totalRamSize"
done
