/****************************************************************************/
/***      Function to get input from user and check files can be opened   ***/
/***                                                                      ***/
/***                    Jo Wood,  V1.0, 13th September 1994               ***/
/****************************************************************************/

#include "example.h"

interface(argc,argv) 

    int      argc;              /* Number of command line arguments     */
    char    *argv[];            /* Contents of command line arguments.  */

{
    /*---------------------------------------------------------------------*/
    /*                               INITIALISE				   */
    /*---------------------------------------------------------------------*/ 

    struct Option       *rast_in;   	/* Pointer to  structures holding  */
    				   	/* the text to describe each option*/
                                        /* It also stores the user's input */

    struct Option       *rast_out;      /*  structure to output raster     */           


    G_gisinit (argv[0]);                /* Link with GRASS interface.	   */


    /*---------------------------------------------------------------------*/
    /*                              SET PARSER OPTIONS                     */
    /*---------------------------------------------------------------------*/

    rast_in  = G_define_option();	/* Pointer to reserved memory for  */
    					/* each command line option.	   */
    rast_out  = G_define_option();

    /* Each option needs a 'key' (short description),
         		 a 'description` (a longer one),
         		 a 'type' (eg intiger, or string),
         	     and an indication whether manditory or not */

    rast_in->key          = "in";
    rast_in->description  = "First raster layer to process";
    rast_in->type         = TYPE_STRING;
    rast_in->required     = YES;

    rast_out->key         = "out";
    rast_out->description = "Output raster layer";
    rast_out->type        = TYPE_STRING;
    rast_out->required    = YES;

    if (G_parser(argc,argv))            /* Performs the prompting for      */
        exit(-1);                       /* keyboard input.                 */

    rast_in_name  = rast_in->answer; 	/* Now that keyboard input has     */
    					/* been parsed, place the contents */
    rast_out_name  = rast_out->answer;  /* contents into string arrays.    */


    /*---------------------------------------------------------------------*/
    /*                   CHECK INPUT RASTER FILES EXIST			   */
    /*---------------------------------------------------------------------*/

    if ((mapset_in=G_find_cell2(rast_in_name,""))==NULL)
    {
        char err[256];
        sprintf(err,"Raster map [%s] not available.",rast_in_name);
        G_fatal_error(err);
    }

    /*--------------------------------------------------------------------*/
    /*                  CHECK OUTPUT RASTER FILE DOES NOT EXIST           */
    /*--------------------------------------------------------------------*/

    mapset_out = G_mapset();            /* Set output to current mapset.  */

    if (G_legal_filename(rast_out_name)==NULL)
    {
        char err[256];
        sprintf(err,"Illegal output file name. Please try another.");
        G_fatal_error(err);
    }
    else
    {
        if (G_find_cell2(rast_out_name,mapset_out) !=NULL)
        {
            char err[256];
            sprintf(err,"Raster map [%s] exists.\nPlease try another\n",
							rast_out_name);
	    G_fatal_error(err);
        }
    }
}

