#!/usr/bin/python3.6
"""Autoconfigure a web app based on a github URL.

- downloads the repo
- detects a requirements.txt and installs it into a virtualenv
- autodetects django / flask and creates wsgi configuration for them
- adds static files config
- adds placeholders for any detected environment variables

Usage:
  pa_autoconfigure_webapp.py <git-repo-url> [--domain=<domain> --python=<python-version>] [--nuke]

Options:
  --domain=<domain>         Domain name, eg www.mydomain.com   [default: your-username.pythonanywhere.com]
  --python=<python-version> Python version, eg "2.7"    [default: 3.6]
  --nuke                    *Irrevocably* delete any existing web app config on this domain. Irrevocably.
"""

from docopt import docopt
import getpass
from pathlib import Path
import subprocess
import shutil

from pythonanywhere.sanity_checks import sanity_checks
from pythonanywhere.virtualenvs import create_virtualenv
from pythonanywhere.api import create_webapp


def download_repo(repo, domain, nuke):
    target = Path('~').expanduser() / domain
    if nuke:
        shutil.rmtree(target)
    subprocess.check_call(['git', 'clone', repo, target])
    return target


def main(repo_url, domain, python_version, nuke):
    if domain == 'your-username.pythonanywhere.com':
        username = getpass.getuser().lower()
        domain = f'{username}.pythonanywhere.com'

    sanity_checks(domain, nuke=nuke)
    project_folder = download_repo(repo_url, domain, nuke=nuke)
    virtualenv = create_virtualenv(domain, python_version, nuke=nuke)
    create_webapp(domain, python_version, virtualenv, project_folder, nuke=nuke)





if __name__ == '__main__':
    arguments = docopt(__doc__)
    main(arguments['<git-repo-url>'], arguments['--domain'], arguments['--python'], nuke=arguments.get('--nuke'))


@@ -1172,17 +1172,16 @@
 target%0A%0A
-%0A
 def main
@@ -1546,87 +1546,569 @@
 ke)%0A
-    create_webapp(domain, python_version, virtualenv, project_folder, nuke=nuke
+%0A    # update_settings_file(domain, project_path)%0A    # run_collectstatic(virtualenv_path, project_path)%0A%0A    create_webapp(domain, python_version, virtualenv, project_folder, nuke=nuke)%0A%0A    # create_webapp(domain, python_version, virtualenv_path, project_path, nuke=nuke)%0A    # add_static_file_mappings(domain, project_path)%0A    # wsgi_file_path = '/var/www/' + domain.replace('.', '_') + '_wsgi.py'%0A    # update_wsgi_file(wsgi_file_path, project_path)%0A    # reload_webapp(domain)%0A%0A    # print(snakesay(f'All done!  Your site is now live at https://%7Bdomain%7D')
 )%0A%0A%0A

add commented-out todos