Eventually the nested if statements should be replaced by a more general condition, but this is sufficient to make it work on FreeBSD.