/*
 * Copyright 2000-2014 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 java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import jetbrains.buildServer.clouds.vmware.vmrun.VMRun;
import jetbrains.buildServer.clouds.vmware.vmrun.local.LocalVMRun;
import jetbrains.buildServer.clouds.vmware.vmrun.local.WMMachineInjection;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.client.Client;
import jetbrains.buildServer.clouds.vmware.vmrun.remote.xmlrpc.XmRpcClient;
import jetbrains.buildServer.log.LogInitializer;
import jetbrains.buildServer.messages.XStreamHolder;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * @author Eugene Petrenko
 *         Created: 08.12.2009 18:35:50
 */
public class Main {
  public static void main(String[] args) {
    LogInitializer.init(LogInitializer.propertyConfigurator(true), LogInitializer.resourceConfigurator("vmware-client-log4j.xml"));

    final Configration cfg = CommandlineArgumentsParser.parseConfig(args);
    if (cfg == null) {
      System.exit(1);
      return;
    }

    setupLogger(cfg);

    final XStreamHolder xh = new XStreamHolder();
    final ImagesStore imagesStore = new ImagesStore(cfg, xh);

    final ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
    final LocalVMRun localRun = new LocalVMRun(cfg, service, new WMMachineInjection(cfg), imagesStore);
    final VMRun dRun = new DownloadingVMRun(localRun, imagesStore);
    final VMRun lRun = new LoggingVMRun(dRun);

    final XmRpcClient rpc = new XmRpcClient(xh, cfg.getServerUrl());
    new Client<VMRun>(lRun, rpc, service, 3000);

    System.out.println("Client is waiting for commands from TeamCity server...");

    while (true) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  private static void setupLogger(final Configration cfg) {
    try {
      final FileAppender fo = new FileAppender(new PatternLayout("[%d{ABSOLUTE}] %6p - %30.30c - %m \n"),
                                               new File(cfg.getVMStoreHome(), "machine.log").getAbsolutePath(), true);
      Logger.getLogger(WMMachineInjection.class).addAppender(fo);
      Logger.getLogger(WMMachineInjection.class).setLevel(Level.INFO);
    } catch (IOException e) {
      //
    }
  }
}
