#!/usr/bin/env awk -f
#
# unf - unfold header lines in an internet message, don't touch the body
#
# Usage:
#    unf FILE
#    unf <FILE
#    unf FILE1 FILE2...
#    curl ‐I http://www.example.net/ | unf

BEGIN { buf = "" }

# function to write and empty the buffer
function wrbuf() {
    if (length(buf))
        print buf
    buf = ""
}

# flag to stop processing once we hit the first blank line
!length($0) {
    wrbuf()
    body = 1
}
body {
    print
    next
}

# write any buffer contents once we hit a line not starting with a space
/^[^ \t]/ { wrbuf() }

# append the current line to the buffer
{
    sub(/^[ \t]+/, " ")
    buf = buf $0
}

# write the buffer out again when we hit the end
END { wrbuf() }
