/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.browser.spencer;

import java.io.*;
import java.net.*;
import luxor.status.*;

public class BrowserLauncher
{

   /**
    *  The flag to display a url.
    */
   private final static String UNIX_FLAG = "-remote openURL";

   /**
    *  The default browser under unix.
    */
   private final static String UNIX_PATH = "netscape";

   /**
    *  The flag to display a url.
    */
   private final static String WIN_FLAG = "url.dll,FileProtocolHandler";

   /**
    *  Used to identify the windows platform.
    */
   private final static String WIN_ID = "Windows";
   /**
    *  The default system browser under windows.
    */
   private final static String WIN_PATH = "rundll32";

   public static boolean isWindowsPlatform()
   {
      String os = System.getProperty( "os.name" );

      if( os != null && os.startsWith( WIN_ID ) )
         return true;
      else
         return false;
   }

   public static void showDocument( String url )
   {
      if( url == null )
         return;

      boolean windows = isWindowsPlatform();
      String cmd = null;

      try
      {
         if( windows )
         {
            // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
            cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
            Process p = Runtime.getRuntime().exec( cmd );
         }
         else
         {
            // Under Unix, Netscape has to be running for the "-remote"
            // command to work. So, we try sending the command and
            // check for an exit value. If the exit command is 0,
            // it worked, otherwise we need to start the browser.

            // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
            cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
            Process p = Runtime.getRuntime().exec( cmd );

            try
            {
               // wait for exit code -- if it's 0, command worked,
               // otherwise we need to start the browser up.
               int exitCode = p.waitFor();

               if( exitCode != 0 )
               {
                  // Command failed, start up the browser
                  // cmd = 'netscape http://www.javaworld.com'
                  cmd = UNIX_PATH + " " + url;
                  p = Runtime.getRuntime().exec( cmd );
               }
            }
            catch( InterruptedException x )
            {
               Status.error( "*** failed to bring up browser using command '" + cmd + "': " + x.toString() );
            }
         }
      }
      catch( IOException x )
      {
         // couldn't exec browser
         Status.error( "*** failed to bring up browser using command '" + cmd + "': " + x.toString() );
      }
   }
}