import os
import sys
import tempfile
import time

os.environ['PATH'] = "%s;%s" % (os.path.dirname(sys.executable), os.environ['PATH'])

def unix_cmd(cmdline):
    if not os.path.exists("z:\\bin\\sh"): return ''
    f, outputfile = tempfile.mkstemp()
    os.close(f)
    f, waitfile = tempfile.mkstemp()
    os.close(f)
    os.remove(waitfile)
    #os.system('z:\\bin\\sh -c "python wine_kludge.py %s %s"' % (name, cmdline))
    os.system('z:\\bin\\sh wine_kludge.sh "%s" "%s" %s' % (outputfile, waitfile, cmdline))
    while not os.path.exists(waitfile):
        time.sleep(0.1)
    os.remove(waitfile)
    f = open(outputfile, 'r')
    result = f.read()
    f.close()
    os.remove(outputfile)
    return result

#theoretically we should ask winepath about this, but unix_cmd is slow and we
# already rely on z:
def unix_to_windows(path):
    if path.startswith('/'):
        path = 'z:' + path
    return path.replace('/','\\')

def windows_to_unix(path):
    return unix_cmd('winepath --unix "%s"' % path).rstrip('\n')
