#!/bin/sh
set -e

PREREQS=""
case $1 in
    prereqs) echo "${PREREQS}"; exit 0;;
esac

. /usr/share/initramfs-tools/hook-functions

# Fetch the device parameters
for x in $(cat /proc/cmdline); do
    case $x in
        mobile.qcomsoc=*) QCOMSOC="${x#mobile.qcomsoc=}" ;;
        mobile.model=*) MODEL="${x#mobile.model=}" ;;
        mobile.vendor=*) VENDOR="${x#mobile.vendor=}" ;;
    esac
done

if [ "${QCOMSOC}" ] && [ "${MODEL}" ]; then
    # Special case for the OnePlus 6 & 6T: both models use the same
    # firmware folder: sdm845/oneplus6
    if [ "${MODEL}" = "enchilada" ] || [ "${MODEL}" = "fajita" ]; then
        MODEL="oneplus6"
    # Special case for the "fpN" (Fairphone N): these models use the
    # "fairphoneN" firmware folder
    elif [ "${MODEL}" = "fp4" ]; then
        MODEL="fairphone4"
    elif [ "${MODEL}" = "fp5" ]; then
        MODEL="fairphone5"
    elif [ "${MODEL}" = "spacewar" ]; then
        MODEL="${VENDOR}/${MODEL}"
    fi
    # Special case for "bonito" (Pixel 3a XL): this model uses the
    # "sargo" (Pixel 3a) firmware folders
    if [ "${MODEL}" = "bonito" ]; then
        MODEL="sargo"
    fi

    FW_LIST="${QCOMSOC}/${MODEL}/adsp.mbn \
             ${QCOMSOC}/${MODEL}/cdsp.mbn \
             ${QCOMSOC}/${MODEL}/ipa_fws.mbn"

    if [ "${QCOMSOC}" = "qcom/sdm845" ]; then
        FW_LIST="${FW_LIST} \
                 ${QCOMSOC}/${MODEL}/a630_zap.mbn \
                 ${QCOMSOC}/${MODEL}/slpi.mbn \
                 ath10k/WCN3990/hw1.0/board-2.bin \
                 qca/${MODEL}/crnv21.bin \
                 qca/crbtfw21.tlv \
                 qca/crnv21.bin \
                 "
    elif [ "${QCOMSOC}" = "qcom/sdm670" ]; then
        FW_LIST="${FW_LIST} \
                 ${QCOMSOC}/${MODEL}/a615_zap.mbn \
                 ath10k/WCN3990/hw1.0/board-2.bin \
                 qca/crbtfw21.tlv \
                 qca/crnv21.bin \
                 "
    elif [ "${QCOMSOC}" = "qcom/sm7225" ]; then
        FW_LIST="${FW_LIST} \
                 ${QCOMSOC}/${MODEL}/a615_zap.mbn \
                 ath10k/WCN3990/hw1.0/board-2.bin \
                 qca/apbtfw11.tlv \
                 qca/apnv11.bin \
                 "
    elif [ "${QCOMSOC}" = "qcom/sm7325" ]; then
        FW_LIST="${FW_LIST} \
                 ${QCOMSOC}/${MODEL}/wpss.mbn \
                 ${QCOMSOC}/${MODEL}/modem.mbn \
                 ${QCOMSOC}/${MODEL}/a660_zap.mbn \
                 ${QCOMSOC}/${MODEL}/vpu20_1v.mbn \
                 qca/msbtfw11.mbn \
                 qca/msnv11.bin \
                 "
    elif [ "${QCOMSOC}" = "qcom/qcm6490" ]; then
        FW_LIST="${FW_LIST} \
                 ${QCOMSOC}/${MODEL}/a660_zap.mbn \
                 qca/msbtfw11.tlv \
                 qca/msnv11.bin \
                 "
    fi

    for fwfile in ${FW_LIST}; do
        if [ -f "/lib/firmware/updates/${version?}/${fwfile}" ] ||
           [ -f "/lib/firmware/updates/${fwfile}" ]             ||
           [ -f "/lib/firmware/${version}/${fwfile}" ]          ||
           [ -f "/lib/firmware/${fwfile}" ]; then
            add_firmware "${fwfile}"
        else
            echo "I: Ignoring missing firmware ${fwfile}"
        fi
    done
fi
