/*
 * Copyright (C) 2007 Helge Hess
 *
 * This file is part of JOPE.
 *
 * JOPE is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later version.
 *
 * JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JOPE; see the file COPYING. If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.opengroupware.jope.jsapp;

import java.io.File;

import org.opengroupware.jope.jetty.WOJettyRunner;

/**
 * The runner class which starts Jetty for a JavaScript JOPE application. Call
 * it on a MyApp.woa directory, eg:<pre>
 *   java org.opengroupware.jope.jsapp.run MyApp.woa
 * </pre>
 */
public class run extends WOJettyRunner {

  public static void main(String[] _args) {
    WOJettyRunner runner = new WOJettyRunner();
    
    String path = null;
    for (String _arg: _args) {
      if (_arg == null || _arg.length() == 0)
        continue;
      
      if (path == null && !_arg.startsWith("-"))
        path = _arg;
    }
    if (path == null)
      path = System.getProperty("user.dir");
    
    File root = new File(path);
    root = root != null ? root.getAbsoluteFile() : null;
    if (root == null || !root.isDirectory()) {
      System.err.println("root is not a directory: " + root);
      System.exit(1);
      return;
    }
    
    JSApplication.appRoot = root;
    
    System.out.println("app root: " + root);
    String shortAppName = root.getName();
    int extidx = shortAppName.indexOf('.');
    if (extidx > 0)
      shortAppName = shortAppName.substring(0, extidx);
    
    runner.initWithClassAndNameAndPort
      (JSApplication.class, shortAppName, null /* Jetty www */, 8080);
    
    runner.run();
  }

}
