# -*- mode: sh -*-
# Common functions for autodep8
# 

parse_control(){
    # Function to extract items in a field of debian/control return comma
    # separated
    # tested with Build-Depends
    # Don't use with Recommends as some packages add substvars (like cdbs)
    #
    # input1: control field
    # input2: items to filter out (optional)
    
    control_field=$1
    filter="$2"
    if [ -f debian/control ]; then
        out=$(
            grep-dctrl -n -s $control_field -F $control_field -r . debian/control \
                | grep -v '^\s*#' \
                | sed -e 's/,\s*/\n/g; s/^\s*//' \
                | sed -e 's/\s*<[^)]*>\s*$//' \
                | tr '\n' ', ' \
                | sed 's/,,/,/g')
    else
        out=''
    fi
    for pkg in $filter ; do
        out=$(echo ,$out | sed -e "s/,$pkg,/,/" -e "s/,$pkg ([^,]*),/,/" -e 's/,,/,/')
        out=${out#,}
    done

    echo $out
}
