#! /usr/bin/env python
# (c) Copyright 2011 CodeWeavers, Inc.

import sys
import os
import subprocess
import traceback

cleanup = False
while (1):
    try:
        if not sys.stdin.read(1):
            cleanup = True
            print >> sys.stderr,"Reaper detected EOF.  Cleaning up..."
            break
    except IOError, e:
        if e.errno == erno.EINTR:
            continue
        else:
            traceback.print_exc()
            break
    except EOFError:
        cleanup = True
        break

if cleanup:
    winepath = os.environ.get('REAPER_WINEPATH')
    bottle = os.environ.get('REAPER_BOTTLE')

    if not winepath or not bottle:
        print >> sys.stderr,"Reaper unable to determine wineserver or bottle.  Unable to reap!"
        sys.exit(1)

    args = [winepath, "--no-update", "--bottle", bottle, "--ux-app", "wineserver", "-k"]
    subprocess.Popen(args, close_fds=True)

sys.exit(0)

