# vim: filetype=sh

# kernel command line customization
# ---------------------------------

# The behavior of the following function is better
# understood with the following example:
#
# $ to_bootarglist quiet rootdelay=3 -quiet rootdelay=2 +break=init
# + quiet
# + rootdelay 3
# - quiet
# + rootdelay 2
# + break init
# $

to_bootarglist()
{
    echo $@ | tr ' ' '\n' | tr '=' ' ' | \
        sed -e 's/^\([^+-]\)/+\1/g' | sed -e 's/^\(.\)/\1 /g'
}

# The following function allows to generate an appropriate kernel cmdline
# given a set of bootarg definitions.
# For a given bootarg name, last definition provided takes precedence.
# Example:
#
# $ aggregate_kernel_cmdline quiet rootdelay=3 -quiet rootdelay=2 +break=init
# break=init rootdelay=2
# $

aggregate_kernel_cmdline()
{
    bootarglist="$(to_bootarglist $@)"
    bootargnames="$(echo "$bootarglist" | awk '{print $2}' | sort -u)"
    cmdline=""
    for bootarg in $bootargnames
    do
        # last definition of given bootarg takes precedence
        set -- $(echo "$bootarglist" | grep -w "$bootarg" | tail -n 1)
        if [ "$1" = "+" ]   # otherwise ignore this bootarg
        then
            # ok register this bootarg
            if [ -z "$3" ]
            then
                cmdline="$cmdline $bootarg"     # bootarg with no value
            else
                cmdline="$cmdline $bootarg=$3"  # bootarg with value
            fi
        fi
    done
    echo $cmdline
}
