# Copyright 2018 Google LLC
#
# 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.

# Instructions:
#
# Check out the googleapis and discovery-artifact-manager repo somewhere locally
# (e.g. from https://github.com/googleapis/googleapis):
#
# $ git clone https://github.com/googleapis/googleapis.git
# $ git clone https://github.com/googleapis/discovery-artifact-manager.git
#
# Run this script from the top-level google-cloud-java directory:
#
# $ python utilities/batch_generate_apis.py PATH_TO_GOOGLEAPIS PATH_TO_DISCOVERY_ARTIFACT_MANAGER

import argparse
import os
import sys
from subprocess import check_output

import generate_api


def run_gapic_gen(googleapis):
    def generate(artman_yaml, artifact_name=generate_api.JAVA_GAPIC):
        generate_api.run_generate_api(os.path.join(googleapis, artman_yaml),
                                      artifact_name)

    generate('google/datastore/artman_datastore.yaml', generate_api.JAVA_PROTO)

    generate('google/cloud/asset/artman_cloudasset_v1beta1.yaml')
    generate('google/cloud/automl/artman_automl_v1beta1.yaml')
    generate('google/cloud/bigquery/datatransfer/artman_bigquerydatatransfer.yaml')
    generate('google/bigtable/artman_bigtable.yaml')
    generate('google/bigtable/admin/artman_bigtableadmin.yaml')
    generate('google/container/artman_container.yaml')
    generate('google/devtools/containeranalysis/artman_containeranalysis_v1beta1.yaml')
    generate('google/cloud/dataproc/artman_dataproc_v1.yaml')
    generate('google/cloud/dataproc/artman_dataproc_v1beta2.yaml')
    generate('google/cloud/dialogflow/artman_dialogflow_v2.yaml')
    generate('google/cloud/dialogflow/artman_dialogflow_v2beta1_java.yaml')
    generate('google/privacy/dlp/artman_dlp_v2.yaml')
    generate('google/devtools/clouderrorreporting/artman_errorreporting.yaml')
    generate('google/firestore/artman_firestore.yaml')
    generate('google/cloud/iot/artman_cloudiot.yaml')
    generate('google/cloud/kms/artman_cloudkms.yaml')
    generate('google/cloud/language/artman_language_v1.yaml')
    generate('google/cloud/language/artman_language_v1beta2.yaml')
    generate('google/logging/artman_logging.yaml')
    generate('google/monitoring/artman_monitoring.yaml')
    generate('google/pubsub/artman_pubsub.yaml')
    generate('google/cloud/oslogin/artman_oslogin_v1.yaml')
    generate('google/cloud/redis/artman_redis_v1.yaml')
    generate('google/cloud/redis/artman_redis_v1beta1.yaml')
    generate('google/spanner/artman_spanner.yaml')
    generate('google/spanner/admin/database/artman_spanner_admin_database.yaml')
    generate('google/spanner/admin/instance/artman_spanner_admin_instance.yaml')
    generate('google/cloud/speech/artman_speech_v1.yaml')
    generate('google/cloud/speech/artman_speech_v1p1beta1.yaml')
    generate('google/cloud/tasks/artman_cloudtasks_v2beta2.yaml')
    generate('google/cloud/tasks/artman_cloudtasks_v2beta3.yaml')
    generate('google/cloud/texttospeech/artman_texttospeech_v1.yaml')
    generate('google/cloud/texttospeech/artman_texttospeech_v1beta1.yaml')
    generate('google/devtools/cloudtrace/artman_cloudtrace_v1.yaml')
    generate('google/devtools/cloudtrace/artman_cloudtrace_v2.yaml')
    generate('google/cloud/videointelligence/artman_videointelligence_v1beta1.yaml')
    generate('google/cloud/videointelligence/artman_videointelligence_v1beta2.yaml')
    generate('google/cloud/videointelligence/artman_videointelligence_v1p1beta1.yaml')
    generate('google/cloud/videointelligence/artman_videointelligence_v1p2beta1.yaml')
    generate('google/cloud/videointelligence/artman_videointelligence_v1.yaml')
    generate('google/cloud/vision/artman_vision_v1.yaml')
    generate('google/cloud/vision/artman_vision_v1p1beta1.yaml')
    generate('google/cloud/vision/artman_vision_v1p2beta1.yaml')
    generate('google/cloud/vision/artman_vision_v1p3beta1.yaml')
    generate('google/cloud/websecurityscanner/artman_websecurityscanner_v1alpha.yaml')


def run_discogapic_gen(discovery_repo):
    def generate(artman_yaml):
        # Run java_discogapic task. No proto or grpc libraries are generated.
        generate_api.run_generate_api(os.path.join(discovery_repo, artman_yaml),
                                      generate_api.JAVA_DISCOGAPIC)

    generate('gapic/google/compute/artman_compute.yaml')


def verify_protoc_version():
    protobuf_version_node = check_output(
        ['grep', '-zohr', '--include=pom.xml',
         '<protobuf.version>.*</protobuf.version>'])
    version_start_index = protobuf_version_node.find('>') + 1
    version_end_index = protobuf_version_node.rfind('<')
    protobuf_version = protobuf_version_node[version_start_index : version_end_index].strip()

    # This will be something like 'libprotoc 3.6.0'
    protoc_version_str = check_output(['protoc', '--version'])

    if not (protobuf_version in protoc_version_str):
        sys.exit("ERROR: Local version of protoc is %s"
                 " (see output of `which protoc`)."
                 " Please use protoc version %s"
                 " to match the version of protobuf-java used in this repo."
                 % (protoc_version_str, protobuf_version))


def main():
    verify_protoc_version()
    # TODO Make the docker image the default, add --local option
    parser = argparse.ArgumentParser(description='Batch generate all APIs.')
    parser.add_argument('googleapis', help='The path to the googleapis repo')
    parser.add_argument('discovery_repo',
                        help='The path to the discovery-artifact-manager repo')
    args = parser.parse_args()

    generate_api.dump_versions(googleapis=args.googleapis, discovery_repo=args.discovery_repo)

    run_gapic_gen(args.googleapis)
    run_discogapic_gen(args.discovery_repo)

if __name__ == '__main__':
    main()
