#! /usr/bin/env python

import logging
import os
import sys


def is_packaged_app():
  try:
    sys._MEIPASS
    return True
  except AttributeError:
    return False

if not is_packaged_app():
  base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
  sys.path.extend([base_dir])

# Fixes PyInstaller issue when sys.getsystemencoding is None.
# https://github.com/mitsuhiko/click/issues/355
if sys.getfilesystemencoding() is None:
  sys.getfilesystemencoding = lambda: 'utf-8'

import click

logging.basicConfig(level=logging.INFO, format='%(message)s')

# Fixes PyInstaller issues with below imports.
import copy
import exceptions
import weakref
import werkzeug
from email.mime.application import MIMEApplication

# Fixes simplejson and PyInstaller.
import json
sys.modules['simplejson'] = json

# Fixes issue with httplib2, requests, cacerts and PyInstaller.
from grow.common import ca_certs_locater
sys.modules['ca_certs_locater'] = ca_certs_locater
from grow.common import utils
os.environ['REQUESTS_CA_BUNDLE'] = utils.get_cacerts_path()

# Fixes sys.getfilesystemencoding() and PyInstaller.
from watchdog.utils import unicode_paths
unicode_paths.fs_encoding = unicode_paths.fs_fallback_encoding

from grow import commands
from grow.common import sdk_utils
from grow.common import utils

_grow_dir = utils.get_grow_dir()
ver = open(os.path.join(_grow_dir, 'VERSION')).read().strip()
text = ('Grow is a declarative file-based website generator. Read docs at '
        'https://grow.io. This is version {}.'.format(ver))


@click.group(help=text)
@click.version_option(sdk_utils.get_this_version(), message='%(version)s')
@click.option('--auth', help='Information used to sign in to services that'
              ' require authentication. --auth should be an email address.',
              envvar='GROW_AUTH')
@click.option('--clear-auth', default=False, is_flag=True,
              help='Clears stored auth information.')
@click.option('--auth-key-file', help='Path to a private key file used for'
              ' services that require authentication.', envvar='GROW_KEY_FILE')
@click.option(
    '--interactive-auth', default=False, is_flag=True,
    envvar='INTERACTIVE_AUTH',
    help='Whether to automatically open an authorization page in your'
         ' default web browser for any steps that require authentication.'
         ' If you are running Grow on a machine with access to a web browser,'
         ' you may use --interactive-auth to automatically open the web'
         ' browser. By default, this option is turned off, requiring you to'
         ' manually copy and paste an authorization code.')
def grow(auth, clear_auth, auth_key_file, interactive_auth):
  if interactive_auth not in (None, False):
    os.environ['INTERACTIVE_AUTH'] = str(interactive_auth)
  if auth is not None:
    os.environ['AUTH_EMAIL_ADDRESS'] = str(auth)
  if auth_key_file is not None:
    os.environ['AUTH_KEY_FILE'] = str(auth_key_file)
  if clear_auth:
    os.environ['CLEAR_AUTH'] = '1'


commands.add(grow)


if __name__ == '__main__':
  grow()
