/******************************************************************************
**
** Source File Name = makeapi.sqc
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**       This program demonstrates precompiling and binding embedded SQL programs.
**       Input parameters from the command line are as follows:
**
**             makeapi <program_name>.sqc <database_name>
**
**
**    STRUCTURES USED :
**       sqlopt
**       sqlca
**
**    APIs USED :
**       START DATABASE MANAGER     sqlepstart()
**       PREOCOMPILE PROGRAM        sqlaprep()
**       BIND                       sqlabndx()
**       STOP DATABASE MANAGER      sqlepstp()
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  strncpy
**
**       DBMS LIBRARY :
**          sqlenv.h -  see "APIs USED" above
**
**       OTHER :
**
**    EXTERNAL DEPENDANCIES :
**       - Existing database for precompile purposes.
**       - Precompile with the SQL precompiler (PREP in DB2)
**       - Binding to a database (BIND in DB2)
**       - Compiling and linking with the appropriate compiler
**
**    EXAMPLE USAGE:
**       - "makeapi updat.sqc sample" 
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include "utilemb.h"


EXEC SQL INCLUDE SQLCA ;

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

   struct sqlopt * sqloptStructPointer ;
   struct sqloptions * optionsPointer ;
   int i, rc ;
   char programName[15] ;
   char bindName[15] ;
   char * extension ;
   char connInput[19] ;
   char option ;
   char *db2path;

   EXEC SQL BEGIN DECLARE SECTION ;
      char dbAlias[9] ;
      char userid[9] ;
      char passwd[19] ;
   EXEC SQL END DECLARE SECTION ;

   if ( argc != 3 ) 
   {
      printf( "\nUSAGE : makeapi program.sqc db_name\n\n" ) ;
      return( 1 ) ;
   }

   if ( ( extension = strstr( argv[1], "." ) ) == 0 ) 
   {
     printf("\nInclude the extension with the program name.\n\n");
     return( 1 );
   }

   if ( strcmp( extension, ".sqc" ) == 0 ) 
   {
         strcpy( programName, argv[1] ) ;
         i = strlen( programName ) - 1 ;
         while ( i > 0 ) 
         {
           if ( programName[i] == '.' ) 
           {
              programName[i] = '\0' ;
              break ;
           }
           i-- ;
         }
    }
    else
    {
        printf("\nInclude the '.sqc' extension with the program name.\n\n");
        return(1);
    }

         strcpy( bindName, programName ) ;
         strcat( bindName, ".bnd" ) ;

               printf( "--->starting the Database Manager\n" ) ;
               /***********************************\
               * START DATABASE MANAGER API called *
               \***********************************/
               sqlepstart(NULL, &sqlca) ;

               printf( "input your user id:\n" ) ;
               gets( connInput ) ;
               strcpy( userid, connInput ) ;

               printf( "input your password:\n" ) ;
               gets( connInput ) ;
               strcpy( passwd, connInput ) ;

               printf( "\n--->Connecting to %s database\n", argv[2] ) ;
               /* by definition of this program,
                  the last argument must be the name of the database */
               strcpy( dbAlias, argv[2] ) ;
               EXEC SQL CONNECT TO :dbAlias USER :userid USING :passwd ;
               EMB_SQL_CHECK( "CONNECT TO database" ) ;
               sqloptStructPointer = ( struct sqlopt * )
                 malloc( sizeof( struct sqlopt ) + ( sizeof( struct sqloptions ) * 2 ) ) ;
               sqloptStructPointer->header.allocated = 2 ;
               sqloptStructPointer->header.used = 2 ;
               optionsPointer = sqloptStructPointer->option ;

               optionsPointer->type = SQL_BIND_OPT ;
               optionsPointer->val = 0 ;            /* bind with default name */
               optionsPointer++ ;
               optionsPointer->type = SQL_PKG_OPT ;
               optionsPointer->val = 0 ;

               printf( "\n--->precompiling the program: %s\n", argv[1] ) ;
               /*******************************\
               * PRECOMPILE PROGRAM API called *
               \*******************************/
               sqlaprep( argv[1], "message.pb1", sqloptStructPointer, &sqlca ) ;
               EMB_SQL_CHECK( "precompile program" ) ;
               printf( "precompiling successful\n" ) ;
               /* nullifying the options for the BIND API */
               optionsPointer = sqloptStructPointer->option ;
               optionsPointer->type = SQL_NO_OPT ;
               optionsPointer->val = 0 ;
               optionsPointer++ ;
               optionsPointer->type = SQL_NO_OPT ;
               optionsPointer->val = 0 ;

               printf( "\n--->binding the application: %s\n", bindName ) ;
               /*****************\
               * BIND API called *
               \*****************/
               sqlabndx( bindName, "message.pb2", sqloptStructPointer, &sqlca ) ;
               EMB_SQL_CHECK( "binding the application" ) ;

               printf( "\n--->connect resetting\n" ) ;
               EXEC SQL CONNECT RESET ;
               EMB_SQL_CHECK( "CONNECT RESET" ) ;

               printf( "\nWould you like to stop the Database Manager? (y/n)\n" ) ;
               option = getc( stdin ) ;
               if ( option == 'y' ) 
               {
                  printf( "\n--->stopping the Database Manager\n" ) ;
                  /**********************************\
                  * STOP DATABASE MANAGER API called *
                  \**********************************/
                  sqlepstp(NULL, &sqlca ) ;
                  EMB_SQL_CHECK( "stopping database manager" ) ;
               }  /* endif */

   return( 0 ) ;

}  /* end of program : makeapi.sqc */