package io.gapi.vgen;

import com.google.api.tools.framework.tools.ToolOptions;

import java.util.ArrayList;
import java.util.List;

// TODO(jgeiger): inherit from GoogleToolDriverBase or the open-source equivalent once available
// to use ToolOptions instead of parsing the args ourselves
//
// Example usage: (assuming environment variable BASE is the base directory of the project
// containing the yamls, descriptor set, and output)
//
//     CodeGeneratorTool --base=$BASE/src/main/
//        --descriptorSet=$BASE/generated/_descriptors/bigtable.desc
//        --serviceYaml=$BASE/configs/bigtabletableadmin.yaml
//        --veneerYaml=$BASE/configs/bigtable_table_veneer.yaml
public class CodeGeneratorTool {

  static String base = "";
  static String descriptorSet = "";
  static List<String> serviceYaml = new ArrayList<>();
  static List<String> veneerYaml = new ArrayList<>();

  public static void main(String[] args) {
    parseArgs(args);
    generate(base, descriptorSet, serviceYaml, veneerYaml);
  }

  private static void parseArgs(String[] args) {
    String missingArgMsg = "Command-line argument '%s' must be followed by an argument";

    List<String> argList = new ArrayList<>();
    for (String arg : args) {
      String[] eqs = arg.split("=", 2);
      argList.add(eqs[0]);
      if (eqs.length > 1) {
        String[] params = eqs[1].split(" ");
        for (String param : params) {
          argList.add(param);
        }
      }
    }

    for (int i = 0; i < argList.size(); i++) {
      if(argList.get(i).equals("--base")) {
        assertOrDie(++i < argList.size(), String.format(missingArgMsg, "--base"));
        base = argList.get(i);

      } else if(argList.get(i).equals("--descriptorSet")) {
        assertOrDie(++i < argList.size(), String.format(missingArgMsg, "--descriptorSet"));
        descriptorSet = argList.get(i);

      } else if(argList.get(i).equals("--serviceYaml")) {
        assertOrDie(++i < argList.size(), String.format(missingArgMsg, "--serviceYaml"));
        serviceYaml.add(argList.get(i));
        while (++i < args.length && argList.get(i).charAt(0) != '-') {
          serviceYaml.add(argList.get(i));
        }
        i--;

      } else if(argList.get(i).equals("--veneerYaml")) {
        assertOrDie(++i < argList.size(), String.format(missingArgMsg, "--veneerYaml"));
        veneerYaml.add(argList.get(i));
        while (++i < args.length && argList.get(i).charAt(0) != '-') {
          veneerYaml.add(argList.get(i));
        }
        i--;

      } else {
        System.err.println("Unexpected option: " + argList.get(i));
        System.err.println("Usage: CodeGeneratorTool [--base=B] [--descriptorSet=D] "
            + "[--serviceYaml=S ... ] [--veneerYaml=V ...]");
        System.exit(1);
      }
    }
  }

  private static void assertOrDie (boolean cond, String msg) {
    if (!cond) {
      System.err.println(msg);
      System.exit(1);
    }
  }

  private static void generate(String base, String descriptorSet, List<String> apiConfigs,
      List<String> generatorConfigs) {

    ToolOptions options = ToolOptions.create();
    options.set(ToolOptions.DESCRIPTOR_SET, base + descriptorSet);
    List<String> configs = new ArrayList<String>();
    for (String config : apiConfigs) {
      configs.add(base + config);
    }
    options.set(ToolOptions.CONFIG_FILES, configs);
    options.set(CodeGeneratorApi.OUTPUT_FILE, base);
    List<String> genConfigs = new ArrayList<String>();
    for (String genConfig : generatorConfigs) {
      genConfigs.add(base + genConfig);
    }
    options.set(CodeGeneratorApi.GENERATOR_CONFIG_FILES, genConfigs);
    CodeGeneratorApi codeGen = new CodeGeneratorApi(options);
    codeGen.run();
  }
}



@@ -462,41 +462,8 @@
 ool 
---base=$BASE/src/main/%0A//        
 --de
@@ -480,16 +480,25 @@
 t=$BASE/
+src/main/
 generate
@@ -525,16 +525,18 @@
 ble.desc
+ %5C
 %0A//     
@@ -550,32 +550,41 @@
 rviceYaml=$BASE/
+src/main/
 configs/bigtable
@@ -598,16 +598,18 @@
 min.yaml
+ %5C
 %0A//     
@@ -630,16 +630,25 @@
 l=$BASE/
+src/main/
 configs/
@@ -673,16 +673,43 @@
 eer.yaml
+ %5C%0A//        --output=$BASE
 %0Apublic 
@@ -751,20 +751,31 @@
  String 
-base
+outputDirectory
  = %22%22;%0A 
@@ -996,22 +996,16 @@
 enerate(
-base, 
 descript
@@ -1034,16 +1034,33 @@
 neerYaml
+, outputDirectory
 );%0A  %7D%0A%0A
@@ -1588,20 +1588,22 @@
 uals(%22--
-base
+output
 %22)) %7B%0A  
@@ -1674,20 +1674,22 @@
 Msg, %22--
-base
+output
 %22));%0A   
@@ -1693,20 +1693,31 @@
         
-base
+outputDirectory
  = argLi
@@ -2710,19 +2710,8 @@
 Tool
- %5B--base=B%5D
  %5B--
@@ -2787,16 +2787,29 @@
 l=V ...%5D
+ %5B--output=O%5D
 %22);%0A    
@@ -3033,21 +3033,8 @@
 ing 
-base, String 
 desc
@@ -3100,24 +3100,48 @@
 ratorConfigs
+, String outputDirectory
 ) %7B%0A%0A    Too
@@ -3228,15 +3228,8 @@
 SET,
- base +
  des
@@ -3350,23 +3350,16 @@
 igs.add(
-base + 
 config);
@@ -3463,20 +3463,31 @@
 T_FILE, 
-base
+outputDirectory
 );%0A    L
@@ -3609,15 +3609,8 @@
 add(
-base + 
 genC

Change command-line options for CodeGeneratorTool