#!/bin/bash
# parses debian/control file, and generates an XML output.

CONTROL=${1:-debian/control}

if [ ! -f $CONTROL ]; then
    echo "File \"$CONTROL\" could not be found" >&2
    exit 1;
fi

cat $CONTROL | \
    awk '
function flushinput () {
  if (name != ""){
    print "    <" name ">\n      " data "\n    </" name ">" 
  } 
}

BEGIN{section="sourcepackage"; name=""; print "<?xml version=\"1.0\"?>\n<debiancontrol>\n  <"section">"}
/^[^ \t]/{
flushinput();
data=""; name=$1; data=$0; gsub ("^[^:]*: ", "", data); gsub (":.*$", "", name);}
/^[\t ]/{if ($0==" .") {data = data "\n" } else {data=data $0}}
/^$/{flushinput();name=""; data=""; print "  </"section">"; section="binarypackage" ; print "  <"section">"}
END{flushinput();print "  </"section">\n</debiancontrol>"; }
'