#!/bin/nawk
# $Id: sessfeatmerge.awk,v 1.3 2007/06/13 04:43:09 ca Exp $
# simple script to convert an access map:
# merge smtps_session_conf and session_features into single entry and
# put the RHS for smtps_session_conf into a tls_requirements subsection.
#
BEGIN { LINE=0; FS="\t";}
/^smtps_session_conf:/ {
	n = split($1, fields, ":");
	if (n == 2) {
		# Note: this replacement is done on a string, not per the syntax!
		# i.e., it will also replace "CN=mymin_cipher_bitser"
		sub("cipher_bits_min", "min_cipher_bits", $2);
		# on some systems works better (but it still may replace the wrong
		# part):
		# sub("[[:<:]]cipher_bits_min[[:>:]]", "min_cipher_bits", $2);
		sessconf[fields[2]] = $2;
	}
	else {
		printf("%s: wrong syntax?, n=%d\n", $1, n);
	}
	next;
}
/^session_features:/ {
	n = split($1, fields, ":");
	if (n == 2) {
		sessfeat[fields[2]] = $2;
	}
	else {
		printf("%s: wrong syntax?, n=%d\n", $1, n);
	}
	next;
}
// {print;}
END {

  for (a in sessconf) {
    if (sessconf[a] != "") {
		printf("smtps_session_conf:%s	tls_requirements { %s }",
			a, sessconf[a]);
	}
    if (sessfeat[a] != "") {
		printf(" session_feature=%s;", sessfeat[a]);
	}
	printf("\n");
  }

  for (a in sessfeat) {
    if (sessconf[a] == "" && sessfeat[a] != "") {
		printf("smtps_session_conf:%s	session_feature=%s;\n", a, sessfeat[a]);
	}
  }

}


syntax highlighted by Code2HTML, v. 0.9.1