#! /usr/bin/python2

from __future__ import print_function
import argparse
import os
import sys

def astra_interpreters_lock_enabled():
    import subprocess
        
    try:
        astra_interpreters_lock_output = subprocess.Popen(
            ['systemctl', 'is-enabled', 'astra-interpreters-lock.service'], 
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE
        )
        stdout, stderr = astra_interpreters_lock_output.communicate()
        return "enabled" in stdout
    except:
        return False

# find the import relatively if available to work before installing catkin or overlaying installed version
if os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'python', 'catkin', '__init__.py')):
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'python'))
if not (astra_interpreters_lock_enabled() and not os.path.abspath(__file__).startswith('/usr/bin')):
    from catkin.init_workspace import init_workspace


def main():
    parser = argparse.ArgumentParser(description='Initializes a catkin workspace by creating a top-level CMakeLists.txt.')
    parser.add_argument('workspace', nargs='?', default='.', help='The path to an existing folder (default: .)')
    args = parser.parse_args()

    # verify that workspace folder exists
    workspace = os.path.abspath(args.workspace)
    if not os.path.isdir(workspace):
        parser.error('Workspace "%s" does not exist' % workspace)

    try:
        init_workspace(workspace)
    except Exception as e:
        sys.stderr.write(str(e))
        sys.exit(2)


if __name__ == '__main__':
    main()
