/* **********************************************************************
 * *	                                                                *
 * *                Help Support Routines                               *
 * *                                                                    *
 * **********************************************************************
 * File: libs/user/help.c
 *
 *        COPYRIGHT (c) 1994, 1996, 1997, 1999, 2001, 2002  BY
 *         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>


#include "sigtypes.h"
#include "user_lib.h"

static int acroreadIsRunning = FALSE;
static int acroreadPID;

static void forkAcroread(int imanual_type_a, char *sfilename_a);


void helpStart(int imanual_type_a, const char *sfilename_a, int ioption_type_a)
{

  char   filename[50];
  char   tempfilename[50];
  char   *sptr;

  if (getenv("IRIS_BIN_ACROBAT")) 
    {
      switch( ioption_type_a )
	{
        case HELP_MENU :
	  if (strstr(sfilename_a, ".pl"))
	    { 
	      strcpy(tempfilename, sfilename_a);
	      sptr = strchr(tempfilename, '.');
	      *sptr = '\0';
	      sprintf(filename,"%s.pdf", tempfilename);
	    }
	  else
	    strcpy(filename, sfilename_a);
	  break;
	  
        case HELP_CONTEXT :
	  strcpy(filename, "0toc.pdf");
	  break;
	  
        case HELP_INDEX :
	  strcpy(filename, "index.pdf");
	  break;
	}
      forkAcroread(imanual_type_a, filename); 
    }
}

void helpExit(void)
{
   if (acroreadIsRunning)
      kill(acroreadPID, SIGTERM);
   acroreadIsRunning = FALSE;
}

static void forkAcroread(int imanual_type_a, char *sfilename_a)
{
  int    pid;
  char   acroread_exe[100];
  char   *ppdf_path_name;
  char   pdf_path_and_file[140];

  switch(imanual_type_a)
  {
    case MANUALS_IRIS :
       ppdf_path_name = getenv("IRIS_MANUALS_IRIS");
       break;

    case MANUALS_UTIL : 
      ppdf_path_name = getenv("IRIS_MANUALS_UTIL");
      break;

    case MANUALS_INST : 
      ppdf_path_name = getenv("IRIS_MANUALS_INST");
      break;

    case MANUALS_NOTE : 
      ppdf_path_name = getenv("IRIS_MANUALS_NOTE");
      break;

    case MANUALS_PROG : 
      ppdf_path_name = getenv("IRIS_MANUALS_PROG");
      break;

    case MANUALS_RVP7 : 
      ppdf_path_name = getenv("IRIS_MANUALS_RVP7");
      break;

    case MANUALS_RCP02 : 
      ppdf_path_name = getenv("IRIS_MANUALS_RCP02");
      break;

    case MANUALS_RVP8 : 
      ppdf_path_name = getenv("IRIS_MANUALS_RVP8");
      break;

    case MANUALS_RCP8 : 
      ppdf_path_name = getenv("IRIS_MANUALS_RCP8");
      break;

    case MANUALS_IRISRAD: 
      ppdf_path_name = getenv("IRIS_MANUALS_IRISRAD");
      break;

    case MANUALS_EXTRA: 
      ppdf_path_name = getenv("IRIS_MANUALS_EXTRA");
      break;
  };

  if (imanual_type_a != MANUALS_EXTRA)
  {
    if (ppdf_path_name == NULL)
    {
       printf("Missing IRIS_MANUALS environment variables\n");
       return;
    }
    sprintf(pdf_path_and_file, "%s%s", ppdf_path_name, sfilename_a);
  }
  else
    sprintf(pdf_path_and_file, "%s", sfilename_a);

  sprintf(acroread_exe, "%sacroread", getenv("IRIS_BIN_ACROBAT"));

  pid = fork();
      
  /* error */
  if (pid == -1 )
  {
     fprintf(stderr,"fork_failed\n");
     exit(EXIT_FAILURE);
  }
  /* child process */
  if (pid == 0 )
  {

     execl(acroread_exe, "acroread", pdf_path_and_file, NULL);
     fprintf(stderr,"Unable to start <%s>\n", acroread_exe);
     _exit(0);
   }

   /* parent process */
   if (!acroreadIsRunning)
     acroreadPID = pid;
  
   acroreadIsRunning = TRUE;
}

