 /*
  * Khoros: $Id: klnfile.c,v 1.3 1991/12/18 08:54:50 dkhoros Exp $
  */

#if !defined(lint) && !defined(SABER)
static char rcsid[] = "Khoros: $Id: klnfile.c,v 1.3 1991/12/18 08:54:50 dkhoros Exp $";
#endif

 /*
  * $Log: klnfile.c,v $
 * Revision 1.3  1991/12/18  08:54:50  dkhoros
 * HellPatch3
 *
  */

/*
 *----------------------------------------------------------------------
 *
 * Copyright 1991, University of New Mexico.  All rights reserved.
 * Permission to copy and modify this software and its documen-
 * tation only for internal use in your organization is hereby
 * granted, provided that this notice is retained thereon and
 * on all copies.  UNM makes no representations as to the sui-
 * tability and operability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 * 
 * UNM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
 * NESS.  IN NO EVENT SHALL UNM BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY OTHER DAMAGES WHAT-
 * SOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PER-
 * FORMANCE OF THIS SOFTWARE.
 * 
 * No other rights, including, for example, the right to redis-
 * tribute this software and its documentation or the right to
 * prepare derivative works, are granted unless specifically
 * provided in a separate license agreement.
 *---------------------------------------------------------------------
 */

#include "unmcopyright.h"        /* Copyright 1991 by UNM */

/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 >>>>
 >>>>         File Name: klnfile.c
 >>>>
 >>>>      Program Name: klnfile
 >>>>
 >>>>      Program Desc: 
 >>>>	
 >>>>	A  utility  for  making/removing   links   in   the   khoros
 >>>>	environment.
 >>>>	
 >>>>	

 >>>>            Author: Tom Sauer and Charlie Gage
 >>>>
 >>>> Date Last Updated: Wed Sep 25 22:11:18 1991
 >>>>
 >>>>          Routines: main- the main program for klnfile
 >>>> 		 gw_usage - gives usage of the program
 >>>> 		 gw_args  - gets arguments of program from command line
 >>>>
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/


#include "klnfile.h"


/****************************************************************
 *
 * Routine Name:  main program for klnfile
 *
 *       Input:  
 *         -source  input source file 
 *
 *
 *         [-toolbox] Toolbox name
 *         [-remove] Make/Remove link
 *
 *
 *
 ****************************************************************/


main(argc, argv)
int argc;
char *argv[];
{

/* -main_variable_list */
char *sourcepath, machpath[MAXPATHLEN];
char *remove_flg, *vexpandpath(), *vbasename();
char buffer[BUFSIZE];
char *script_file, *toolboxpath, *toolbox;
/* -main_variable_list_end */

	program = VStrcpy(argv[0]);
	ac = argc;
	av = argv;

	khoros_init();

	gw_get_args();

/* -main_before_lib_call */
if (check_args()) exit(1);

if ((sourcepath = vexpandpath(klnfile->source_string, NULL, NULL)) == NULL)
{
  (void)fprintf(stderr,"Invalid path obtained for the source file.\n");
  exit(0);
}


if ((klnfile->remove_logic) == 1)
   remove_flg = "1";
else
   remove_flg = "0";


if (klnfile->toolbox_flag && klnfile->toolbox_string != NULL)
{
  toolboxpath = vtoolbox_path(klnfile->toolbox_string);
  if (toolboxpath == NULL)
  {
     fprintf(stderr, "%s:  The toolbox name '%s' is not a valid toolbox.\n",
             program, klnfile->toolbox_string);
     exit(0);
  }

  toolbox = vlower_string(klnfile->toolbox_string, NULL);

  sprintf(machpath, "%s/%s/%s_mf", toolboxpath, MACH_FILE_PATH, toolbox);
  free(toolbox);

}
else
{
  sprintf(machpath, "_NULL_");
}


/* Assign a filename to the shell script file */
script_file = "klnfile_csh";

/* Build the buffer for the system call */
(void) sprintf(buffer,"%s %s %s %s", script_file, sourcepath, remove_flg, machpath);

/* System off the klnfile_csh script */
system(buffer);

/* -main_before_lib_call_end */

/* -main_library_call */
/* -main_library_call_end */

/* -main_after_lib_call */
/* -main_after_lib_call_end */


	khoros_close();
}


/****************************************************************
*
* Routine Name:  gw_usage 
*
*
* Purpose:  prints out the usage for klnfile 
*
* Input:  none 
*
* Output:  none 
*
* Written By:  automatically generated by ghostwriter
*
****************************************************************/


gw_usage()
{
	fprintf(stderr, "klnfile :\n");
	fprintf(stderr, "\t\n");
	fprintf(stderr, "\tA  utility  for  making/removing   links   in   the   khoros\n");
	fprintf(stderr, "\tenvironment.\n");
	fprintf(stderr, "\t\n");
	fprintf(stderr, "\t\n");

/* -usage_additions */
/* -usage_additions_end */

	fprintf(stderr,"\t-source  input source file  (string)\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "\n");
	fprintf(stderr,"\t[-toolbox]  Toolbox name (string) [null]\n");
	fprintf(stderr,"\t[-remove]  Make/Remove link (boolean) [false]\n");
	fprintf(stderr, " \n");
	fprintf(stderr, "\n");
	fprintf(stderr, "\t[-V] Gives the version for klnfile\n");
	fprintf(stderr, "\t[-U] Gives the usage for klnfile\n");
	fprintf(stderr, "\t[-P] Prompts for command line options\n");
	fprintf(stderr, "\t[-A [file1]] Creates the answer file called klnfile.ans or file1 \n");
	fprintf(stderr, "\t[-a [file1]] Uses klnfile.ans or file1 as the answer file \n\n\n");

}


/****************************************************************
*
* Routine Name:  gw_getargs 
*
*
* Purpose:  gets arguments off command line for klnfile
*
* Input:  none 
*
* Output:  none 
*
* Written By:  automatically generated by ghostwriter
*
****************************************************************/


gw_get_args()
{

char	*source_pstr = 
	"\nEnter: (source) input source file \n        {string}: "; 
char	*source_def = NULL;
char	*toolbox_pstr = 
	"\nEnter: (toolbox) Toolbox name \n        {string}: "; 
char	*toolbox_def = NULL;
char	*remove_pstr = 
	"\nEnter: (remove) Make/Remove link \n        {boolean, (y/n)  [no] }: "; 
char  answer_file[512], temp[512];



int	fid = -1,
	error, query, ok, count;
	int prompt_flag = FALSE;

	/*
	 * print version if necessary
	 */
	vgparml(fid, "-V", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	{
	    printf ("\n%s: %s: Version %d.%d\n\n",
	    "klnfile", RELEASE_NAME, RELEASE_MAJOR, RELEASE_MINOR);
	    exit(1);
	}

	/*
	 * print usage if necessary
	 */
	vgparml(fid, "-U", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	{
	    gw_usage();
	    exit(1);
	}

	/*
	 * see if interactive prompting is desired
	 */
	vgparml(fid, "-P", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	    prompt_flag = TRUE;

	/*
	 * see if answer file is to be used
	 */
	vgparms(fid, "-a", "noprompt", answer_file, 0, 0, &error);
	if (error == 0)
	{
	     if (VStrlen(answer_file) == 0)
		sprintf(answer_file, "klnfile.ans");
	     while (fid < 0)
	     {
		if ((fid = open(answer_file, O_RDONLY, 0666))== -1)
		{
		    fprintf(stderr, "klnfile: can't open answer file \"%s\"",
			    answer_file); 
		    fprintf(stderr, "please re-enter: ");
		    (void) gets(answer_file);
		    if (VStrlen(answer_file) == 0)
			sprintf(answer_file, "klnfile.ans");
		}

	     }

	}

	/*
	 * allocate the klnfile structure
	 */
	klnfile = (klnfile_struct *) 
	       calloc((unsigned)1, (unsigned) sizeof (klnfile_struct));

	/*
	 * get required arguments for klnfile
	 */
	if (prompt_flag == TRUE)
	    fprintf(stderr,"\nRequired Arguments:\n\n");

	if (!(klnfile->source_flag = vget_string(fid, prompt_flag,
			"-source", source_pstr, &(klnfile->source_string),
			source_def)))
	{
	    if (prompt_flag == TRUE)
	    {
		while(!(klnfile->source_flag))
		{
		    fprintf(stderr, "\t\tsource is required, please re-enter: \n");
		    klnfile->source_flag = vget_string(fid, prompt_flag,
		    "-source", source_pstr, &(klnfile->source_string),
		    source_def);

		}
	    }
	    else
	    {
		fprintf(stderr, "Error: '-source' is a required argument\n");
		gw_usage();
		exit(1);
	    }
	}

	/*
	 * get optional arguments for klnfile
	 */
	if (prompt_flag == TRUE)
	   fprintf(stderr,"\nOptional Arguments:\n\n");

	klnfile->toolbox_flag = vget_string(fid, prompt_flag,
	"-toolbox", toolbox_pstr, &(klnfile->toolbox_string),
	toolbox_def);

	if (prompt_flag == FALSE)
	{
	    if ((klnfile->toolbox_flag == 1)&&(klnfile->toolbox_string == NULL))
    	{
		fprintf(stderr, "klnfile:\n");
		fprintf(stderr, "you must specify a name following '-toolbox'\n");
		gw_usage();
		exit(1);
		}
	}
	klnfile->remove_flag = vget_logic(fid, prompt_flag,
	"-remove", remove_pstr, &(klnfile->remove_logic), 0);


}



