#!/usr/bin/python
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Language detection script.

import json
import os
import sys

# Augment the path with our library directory.
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
sys.path.append(os.path.join(ROOT_DIR, 'lib'))

import comm


def main(args):

    if len(args) != 2:
        # If we're being called incorrectly, this probably isn't happening from a
        # framework.
        sys.stderr.write('Invalid Usage: %s <source-root-directory>' %
                         sys.argv[0])
        return 1

    # Get the first argument, should be the source root directory.
    path = args[1]

    # Show warnings or errors depending on whether nodejs has been explicitly
    # specified.
    config = comm.get_config()
    if ((config.params.appinfo and config.params.appinfo.runtime) or
        config.params.runtime):
        warn = comm.error
    else:
        warn = comm.debug

    comm.info('Checking for Node.js.')
    package_json = os.path.join(path, 'package.json')
    got_shrinkwrap = False

    if not os.path.isfile(package_json):
        comm.debug('node.js checker: No package.json file.')
        got_package_json = False
        got_npm_start = False
        node_version = None
    else:
        got_package_json = True

        # Try to read the package.json file.
        try:
            with open(package_json) as f:
                contents = json.load(f)
        except (IOError, ValueError) as ex:
            # If we have an invalid or unreadable package.json file, there's
            # something funny going on here so fail recognition.
            warn('node.js checker: error accesssing package.json: %r' % ex)
            return 1

        # See if we've got a scripts.start field.
        got_npm_start = bool(contents.get('scripts', {}).get('start'))

        # See if a version of node is specified.
        try:
            node_version = contents.get('engines', {}).get('node', None)
            comm.info('node version is %s', node_version)
        except AttributeError:
            # Most likely "engines" wasn't a dictionary.
            comm.warn('node.js checker: ignoring invalid "engines" field in '
                    'package.json')
            node_version = None

        if node_version is None:
            comm.warn('No node version specified.  Please add your node '
                      'version, see '
                      'https://docs.npmjs.com/files/package.json#engines')

    if got_npm_start or os.path.exists(os.path.join(path, 'server.js')):
        comm.send_runtime_params({'got_package_json': got_package_json,
                                  'got_npm_start': got_npm_start,
                                  'node_version': node_version})
    else:
        warn('node.js checker: No npm start and no server.js')
        return 1

    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))
