#!/usr/bin/env sh

# This is a valid shell script and also a valid Python script. When this file
# is executed as a shell script, it finds a python binary and executes this
# file as a Python script, passing along all of the command line arguments.
# When this file is executed as a Python script, it loads and runs Dotbot. This
# is useful because we don't know the name of the python binary.

''':' # begin python string; this line is interpreted by the shell as `:`
command -v python3 >/dev/null 2>&1 && exec python3 "$0" "$@"
command -v python  >/dev/null 2>&1 && exec python  "$0" "$@"
>&2 echo "error: cannot find python"
exit 1
'''

# python code

import os
import sys

# this file is syntactically valid Python 2; bail out if the interpreter is Python 2
if sys.version_info[0] < 3:
    print('error: this version of Dotbot is not compatible with Python 2:\nhttps://github.com/anishathalye/dotbot/wiki/Troubleshooting#python-2')
    sys.exit(1)
if sys.version_info < (3, 7):
    print('error: this version of Dotbot requires Python 3.7+')
    sys.exit(1)

project_root_directory = os.path.dirname(
    os.path.dirname(os.path.realpath(__file__)))

def inject(lib_path):
    path = os.path.join(project_root_directory, 'lib', lib_path)
    sys.path.insert(0, path)

inject('pyyaml/lib')

if os.path.exists(os.path.join(project_root_directory, 'src', 'dotbot')):
    src_directory = os.path.join(project_root_directory, 'src')
    if src_directory not in sys.path:
        sys.path.insert(0, src_directory)
        os.putenv('PYTHONPATH', src_directory)

import dotbot

dotbot.cli.main()
