#!/usr/bin/awk -f BEGIN { fieldstr = "pkgname _ver pkgver pkgrel pkgdesc epoch" \ " *arch *license *options" \ " install changelog" \ " *depends *makedepends *checkdepends *optdepends" \ " *conflicts *provides" \ " url *source *noextract *md5sums *sha512sums" \ " _dir" fieldcnt = split(fieldstr, fields) for(i = 1; i <= fieldcnt; i++){ f = fields[i] if(sub(/^[*]/, "", f)){ arrfield[f] = 1 }else if(sub(/^_/, "", f)){ cfield[f] = 1 }else{ strfield[f] = 1 } fields[i] = f } COLS = 78; FS = "\n"; RS = "" } NF < 2 { next } $1 == "packager" { packager = $2; next } $1 == "maintainer" { maintainer = $2; next } # pkgdesc has the only value where parameters do not expand. $1 == "pkgdesc" { output[$1] = qnoexpand($2); next } { for(i = 2; i <= NF; i++) $i = qexpand($i) if(($1 in strfield) || ($1 in cfield)){ output[$1] = $2 }else { arrtxt = wraparray(length($1) + 2) if(arrfield[$1]){ output[$1] = arrtxt } } } END { if(maintainer){ print "# Maintainer: " maintainer "\n" }else if(packager){ print "# Packager: " packager "\n" }else{ print "# Packager: Anonymous\n" } OFS = "="; ORS = "\n"; for(i = 1; i <= fieldcnt; i++){ f = fields[i] if(!(f in output)) continue v = output[f] if(f in cfield) f = "_" f print f, v } } function wraparray (indent) { if(NF == 1) return "()" # this shouldn't happen but just in case. line = "" delete lines linecount = 0 i = 2 while(i <= NF){ linelen = length(line) if((indent + linelen + 1 + length($i) > COLS) && linelen > 0){ lines[++linecount] = line line = "" }else{ if(linelen == 0) line = $(i++) else line = line " " $(i++) } } if(length(line) > 0) lines[++linecount] = line indtxt = sprintf("%" indent "s", "") txt = "(" lines[1] for(i=2; i<=linecount; i++) txt = txt "\n" indtxt lines[i] txt = txt ")" return txt } # Quote field value so that parameters ARE expanded. # NEVER expand command substition (ie backtick or $(...)) function qexpand (v) { if(v ~ /[$']/){ gsub(/["`]/, "\\\\&", v) gsub(/[$][(]/, "\\$(", v) return sprintf("\"%s\"", v) }else if(v ~ /[ <>`"]/){ return sprintf("'%s'", v) }else{ return v } } # Quote field value so that parameters ARE NOT expanded. function qnoexpand (v) { if(v ~ /'/){ gsub(/[$"`]/, "\\\\&", v) return sprintf("\"%s\"", v) }else if(v ~ /[ $"`<>]/){ return sprintf("'%s'", v) }else{ return v } }