/* X-Designer
** (c) 1992, 1993, 1994, 1995
** Imperial Software Technology (IST),
** 95 London Street,
** Reading,
** Berkshire,
** United Kingdom RG1 4QA.
**
** Telephone: +44 1734 587055
** Fax:       +44 1734 589005
** Email:     support@ist.co.uk
*/
 
#if !defined(lint) && !defined(NOSCCS)
static char *sccsid = "@(#)nscontrol.c     1.3" ; /* 96/04/18 */
#endif /* lint && NOSCCS */
 
/* 
** File:       helplink.c
** Purpose:    Simple control over the Netscape WWW browser
** Created by: Giles P.J. Thomas (giles@ist.co.uk)
** Date:       1st February 1996
*/

#include <stdio.h>
#include <X11/Intrinsic.h>

/*
** Some Netscape commands
*/

/* Tell a running Netscape to load a URL in the current window */
#define NETSCAPECMD "netscape -remote 'openURL(%s)' > /dev/null 2>&1"

/* Tell a running Netscape to load a URL in a new window */
#define NEWWINNETSCAPECMD \
        "netscape -remote 'openURL(%s, new-window)' > /dev/null 2>&1"

/* Start a new Netscape, sending it to a particular URL */
#define NETSCAPESTARTCMD "netscape %s &"

/*
** A function to send Netscape to a particular URL.  If no
** Netscape is running, start one.  If /ownWindow/ then
** open a new browser window to show the URL.  If a new Netscape
** had to be started, return 0 - otherwise return 1.
*/
int
SendNetscapeTo( url, ownWindow )
    char *url ;
    Bool  ownWindow ;
{
    char *templateCmd ;
    char *command ;
    int   result = 1 ;

    /* Build an appropriate command to send a running Netscape to the URL */
    if ( ownWindow )
	templateCmd = NEWWINNETSCAPECMD ;
    else
	templateCmd = NETSCAPECMD ;

    command = (char *) XtMalloc( strlen( templateCmd ) + strlen( url ) + 1 ) ;
    sprintf( command, templateCmd, url ) ;

    /* Execute the command */
    if ( system( command ) ) {

	/* If we get here the command failed, presumably because no Netscape
	   was running. */
	XtFree( (XtPointer) command ) ;

	command = (char *) XtMalloc( strlen( NETSCAPESTARTCMD ) 
				     + strlen( url ) + 1 ) ;
	sprintf( command, NETSCAPESTARTCMD, url ) ;
	if ( system( command ) )
	    fprintf( stderr,  
		     "Could not start Netscape to send it to %s.\n",
		     url) ;

	result = 0 ;
    }

    XtFree( (XtPointer) command ) ;

    return result ;
}
