def _write_mathematica_script(filen, text):
    """
    Write the given text (mathematica code) to the given filename. It will
    be wrapped in some MathKernel calling stuff first.
    """
    with open(filen, 'w') as fileh:
        fileh.write(
            """MathKernel -noprompt -run "commandLine={${1+"$1"}}; $(sed '1,/^exit/d' $0) ; Exit[]"
exit $?
"""
            )
        fileh.write(text)
        fileh.close()
