#!/usr/bin/env python3

import argparse
import os


CONFIG_PY_NAME = 'config.py'
CONFIG_SH_NAME = 'setup.sh'

CONFIG_PY_TEXT = """
SPEC_FRAMEWORK_DIR="/home/ayrat/projects/spec-framework/"
"""

CONFIG_SH_TEXT = """
#!/bin/bash
# to run
# . ./setup.sh
# yes, with .

export LD_LIBRARY_PATH=/home/ayrat/projects/pycudd2.0.2/cudd-2.4.2/lib
"""


def _get_root_dir():
    return os.path.dirname(os.path.abspath(__file__))


def _user_confirmed(question):
    answer = input(question + ' [y/n] ').strip()
    assert answer in 'yYnN', answer
    return answer in 'yY'


def _check_files_exist(files):
    existing = list(filter(lambda f: os.path.exists(f), files))
    return existing


def main():
    config_py = os.path.join(_get_root_dir(), CONFIG_PY_NAME)
    existing = _check_files_exist([CONFIG_PY_NAME, CONFIG_SH_NAME])
    if not existing or \
            _user_confirmed('{files} already exist(s).\n'.format(files=existing) +
                            'Replace?'):
        with open(config_py, 'w') as file:
            file.write(CONFIG_PY_TEXT)
        with open(CONFIG_SH_NAME, 'w') as file:
            file.write(CONFIG_SH_TEXT)

        print('Created {files}.\n'
              'Now edit them with your paths.'.
              format(files=[CONFIG_PY_NAME, CONFIG_SH_NAME]))

        return True

    return False


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=
                                     'Generate local configuration file')

    args = parser.parse_args()
    res = main()
    print(['not done', 'done'][res])
    exit(res)

@@ -43,16 +43,27 @@
 port os%0A
+import stat
 %0A%0ACONFIG
@@ -759,16 +759,78 @@
 Y_NAME)%0A
+    config_sh = os.path.join(_get_root_dir(), CONFIG_SH_NAME)%0A
     exis
@@ -1138,30 +1138,25 @@
 th open(
-CONFIG_SH_NAME
+config_sh
 , 'w') a
@@ -1201,16 +1201,172 @@
 SH_TEXT)
+%0A        # make 'sh' config executable%0A        os.chmod(config_sh,%0A                 os.stat(config_sh).st_mode %7C stat.S_IXUSR %7C stat.S_IXGRP %7C stat.S_IXOTH)
 %0A%0A      

use configurator