#!/bin/bash
FIND_PRL=$(find ${INSTALL_PATH}/BUILD/$(ls ${INSTALL_PATH}/BUILD/ | grep qt-static-${QT6_VERSION})/usr -iname "*.prl")
for FILE_PRL in ${FIND_PRL}
do
    #FILE_PRL=/home/acnas/developpackage/BUILD/qt-static-6.7.2.4/usr/qml/QtWebView/libqtwebviewquickplugin.prl
    echo ${FILE_PRL}
    QMAKE_PRL_LIBS=$(cat ${FILE_PRL} | grep 'QMAKE_PRL_LIBS .*')
    QMAKE_PRL_LIBS_FOR_CMAKE=$(cat ${FILE_PRL} | grep 'QMAKE_PRL_LIBS_FOR_CMAKE .*')
    #echo "QMAKE_PRL_LIBS: ${QMAKE_PRL_LIBS}"
    #echo "QMAKE_PRL_LIBS_FOR_CMAKE: ${QMAKE_PRL_LIBS_FOR_CMAKE}"

    SPLIT=" "
    for QMAKE_PRL_LIBS_ANY in "${QMAKE_PRL_LIBS}" "${QMAKE_PRL_LIBS_FOR_CMAKE}"
    do
        NEW_QMAKE_PRL_LIBS=${QMAKE_PRL_LIBS_ANY}
        LIBS=$(echo ${NEW_QMAKE_PRL_LIBS} | grep -oh '\-l[^ ;]*')
        for LIB in ${LIBS}
        do
            if [[ ! ${LIB} == "-lrt"\
            &&  ! ${LIB} == "-lm"\
            &&  ! ${LIB} == "-ldl"\
            &&  ! ${LIB} == "-lpthread" ]]; then
                STATIC_LIB=${LIB/-l/\$\$(SYSROOT)\/usr\/lib\/lib}.a
                #echo "sed -i 's#${LIB}#${STATIC_LIB}#g' ${FILE_PRL}"
                APPEND_LIB_FOR_MNG=""
                if [[ ${LIB} == "-lmng" ]]; then
                    APPEND_LIB_FOR_MNG="${SPLIT}\$\$(SYSROOT)/usr/lib/libz.a${SPLIT}-lm${SPLIT}\$\$(SYSROOT)/usr/lib/libjpeg.a${SPLIT}\$\$(SYSROOT)/usr/lib/liblcms2.a"
                fi
                STATIC_LIB+=${APPEND_LIB_FOR_MNG}

                NEW_QMAKE_PRL_LIBS=${NEW_QMAKE_PRL_LIBS/${LIB}/${STATIC_LIB}}
                #
                echo "${LIB} -> ${STATIC_LIB}"
            #else
                #echo "${LIB} -> ${LIB}"
            fi
        done
         TEXT="$(cat ${FILE_PRL})"
         TEXT="${TEXT/"${QMAKE_PRL_LIBS_ANY}"/"${NEW_QMAKE_PRL_LIBS}"}"
         echo -e "${TEXT}" > ${FILE_PRL}
        SPLIT=";"
    done
done
