/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * 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.
 */

package jetbrains.buildServer.clouds.vmware.client;

import java.io.File;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Eugene Petrenko
 *         Created: 16.07.2010 11:58:40
 */
public class CommandlineArgumentsParser {
  private static final String SERVER = "/server:";
  private static final String VMRUN = "/vmrun:";
  private static final String STORE = "/store:";
  private static final String SILENT = "/silent";
  private static final String MAX_INSTANCES = "/maxInstances:";

  @Nullable
  public static Configration parseConfig(@NotNull final String[] args) {
    try {
      return parseConfigEx(args);
    } catch (Throwable t) {
      usage();
      System.out.println(t.getMessage());
      Logger.getLogger(CommandlineArgumentsParser.class).error(t, t);
      return null;
    }
  }
  private static Configration parseConfigEx(@NotNull final String[] args) {
    String server = null;
    String vmWare = null;
    String store = null;
    boolean isSilent = false;
    int maxInstances = 1;

    for (String arg : args) {
      if (arg.startsWith(SERVER)) {
        server = arg.substring(SERVER.length());
      } else if (arg.startsWith(VMRUN)) {
        vmWare = arg.substring(VMRUN.length());
      } else if (arg.startsWith(MAX_INSTANCES)) {
        final String val = arg.substring(MAX_INSTANCES.length());
        maxInstances = Integer.parseInt(val);
      } else if (arg.startsWith(STORE)) {
        store = arg.substring(STORE.length());
      } else if (arg.equalsIgnoreCase(SILENT)) {
        isSilent = true;
      }
    }

    if (server == null || vmWare == null || store == null) {
      usage();
      return null;
    }

    if (!new File(vmWare).isFile()) {
      System.out.println("Failed to find path to vmrun.exe: " + vmWare);
      usage();
      return null;
    }

    if (!new File(store).isDirectory()) {
      //noinspection ResultOfMethodCallIgnored
      new File(store).mkdirs();
    }

    final Configration cfg = new Configration(server, store, vmWare, isSilent, maxInstances);
    System.out.println("Looking for images from: " + cfg.getVMStoreHome().getPath());
    System.out.println("Connecting to TeamCity server at: " + cfg.getServerUrl());
    System.out.println("Will use VMWare VIX from: " + cfg.getVMRunPath());
    System.out.println();
    return cfg;
  }

  private static void usage() {
    System.out.println("Usage:");
    System.out.println(" tool.exe " +
                       SERVER + "<TeamCity server url> " +
                       VMRUN + "<path to vmrun.exe> " +
                       STORE + "<path to VM store> " +
                       "[" + SILENT + "] " +
                       "[" + MAX_INSTANCES + "<number>]");
  }
}
