#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import sys
import os

if 'CONAN_HOME' not in os.environ:
    user_home = os.environ['HOME'] if 'HOME' in os.environ else '/tmp'
    if sys.platform == 'linux':
        conan_home = os.path.join(user_home, '.cache', 'hadron')
    elif sys.platform == 'darwin':
        conan_home = os.path.join(user_home, 'Library', 'Caches', 'hadron')
    elif sys.platform == 'win32':
        user_home = os.environ['LOCALAPPDATA'] if 'LOCALAPPDATA' in os.environ else '/tmp'
        conan_home = os.path.join(user_home, 'hadron')
    else:
        conan_home = '/tmp'
        print(f'Warning, unsupported platform {sys.platform}')
    os.environ['CONAN_HOME'] = conan_home
else:
    conan_home = os.environ['CONAN_HOME']

print(f'conan building packages in {conan_home}', file=sys.stderr)
print('  This directory can grow to a considerable size', file=sys.stderr)

file_path = os.path.realpath(__file__)
sys.path.insert(0, os.path.dirname(os.path.dirname(file_path)))
from conans import conan

if 'profile' not in sys.argv and not os.path.exists(os.path.join(conan_home, 'profiles', 'default')):
    import subprocess
    r = subprocess.run([sys.executable, __file__, 'profile', 'detect'])
    if r.returncode != 0:
        sys.exit(r.returncode)

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(conan.run())
