#!/usr/bin/env python
"""
delaycat (delayed cat): Cat stdin to stdout, AFTER the pipe is closed.

This is useful when piping files that are in used while wanting to overwrite
them, for example::

   cat file.conf | sed -e 's/from/to/g' > out ; mv out file.conf

can be replaced with ::

   cat file.conf | sed -e 's/from/to/g' | delaycat file.conf

"""
__author__ = 'Martin Blais <blais@furius.ca>'

import sys

def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip())
    opts, args = parser.parse_args()
    if len(args) != 1:
        parser.error("You must specify a file to output to.")
    fn, = args

    text = sys.stdin.read()
    sys.stdin.close()

    f = open(fn, 'w')
    f.write(text)
    f.close()

if __name__ == '__main__':
    main()
