  Aims. The magnetic star HD148937 is the only Galactic Of?p star surrounded by
a nebula. The structure of this nebula is particularly complex and is composed,
from the center out outwards, of a close bipolar ejecta nebula (NGC6164/5), an
ellipsoidal wind-blown shell, and a spherically symmetric Stromgren sphere. The
exact formation process of this nebula and its precise relation to the star's
evolution remain unknown. Methods. We analyzed infrared Spitzer IRS and
far-infrared Herschel/PACS observations of the NGC6164/5 nebula. The Herschel
imaging allowed us to constrain the global morphology of the nebula. We also
combined the infrared spectra with optical spectra of the central star to
constrain its evolutionary status. We used these data to derive the abundances
in the ejected material. To relate this information to the evolutionary status
of the star, we also determined the fundamental parameters of HD148937 using
the CMFGEN atmosphere code. Results. The H$\alpha$ image displays a bipolar or
"8"-shaped ionized nebula, whilst the infrared images show dust to be more
concentrated around the central object. We determine nebular abundance ratios
of N/O = 1.06 close to the star, and N/O = 1.54 in the bright lobe constituting
NGC6164. Interestingly, the parts of the nebula located further from HD148937
appear more enriched in stellar material than the part located closer to the
star. Evolutionary tracks suggest that these ejecta have occured $\sim$1.2-1.3
and $\sim$0.6 Myrs ago, respectively. In addition, we derive abundances of
argon for the nebula compatible with the solar values and we find a depletion
of neon and sulfur. The combined analyses of the known kinematics and of the
new abundances of the nebula suggest either a helical morphology for the
nebula, possibly linked to the magnetic geometry, or the occurrence of a binary
merger.
