#!/usr/bin/python3

import subprocess
import os
import pathlib
import argparse

def may_extract(prefix, tarball, env):
  updated = prefix / '.updated'

  try:
    st = updated.stat()
  except FileNotFoundError:
    need_update = True
  else:
    tar_st = tarball.stat()
    if tar_st.st_ctime > st.st_mtime:
      need_update = True
    else:
      need_update = False

  if need_update:
    prefix.mkdir(parents=True, exist_ok=True)
    subprocess.check_call([
      'bsdtar', '-xf', tarball, '-C', prefix,
    ])
    subprocess.check_call([
      'winetricks', 'sandbox',
    ], env=env)
    subprocess.check_call([
      'wine', 'regedit', r'C:\wechat.reg',
    ], env=env)
    os.unlink(prefix / 'drive_c' / 'wechat.reg')

    with open(updated, 'wb'):
      pass

if __name__ == '__main__':
  parser = argparse.ArgumentParser(description='Wine WeChat')
  parser.add_argument('-c', '--config', action='store_true',
                      help='run winecfg')
  parser.add_argument('-d', '--dir', action='store_true',
                      help='open Wine prefix directory')
  parser.add_argument('-p', '--profile', type=str,
                      help='use alternative profile')
  parser.add_argument('args', nargs='*',
                      help='arguments for WeChat.exe')
  args = parser.parse_args()

  root = os.environ.get('XDG_DATA_DIR', os.environ['HOME'] + '/.local')
  root = pathlib.Path(root)
  prefix = root / 'lib/wine-wechat' / (args.profile or 'default')
  tarball = pathlib.Path('/opt/wine-wechat/wine-wechat.tar.zst')

  env = os.environ.copy()
  env['WINEDLLOVERRIDES'] = 'winemenubuilder.exe=d'
  env['WINEARCH'] = 'win32'
  env['WINEPREFIX'] = str(prefix)

  may_extract(prefix, tarball, env)

  if args.config:
    os.execvpe('winecfg', ['winecfg'], env)

  if args.dir:
    os.execvp('xdg-open', ['xdg-open', prefix])

  os.chdir(prefix / 'drive_c/Program Files/Tencent/WeChat')
  os.execvpe('wine', ['wine', 'WeChat.exe'] + args.args, env)
