/**
 * @file   FLCURL.c
 * @brief  Sample program in C to copy files via the FLUC byte interface
 * @author limes datentechnik gmbh
 ******************************************************************************/

/* defines code page for literals for IBM mainframes to German */
#ifdef __ZOS__
#  pragma convert(1141)
#endif

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#ifdef __HAS_CURL__
#include <curl/curl.h>
#endif
#include"FLCBYT.h"

/**
 * This test program takes data from CURL to convert this data via the byte
 * interface.
 *
 * @param argc must be 4
 * @param argv contains Data-URL and the strings for fcbopen
 * @return 8=parameter error, 6=open file error, 4=read write error, 2=close error, 0=Success
 */
int main(int argc, char * argv[])
{
   void*                   pvWrt;
   unsigned int            i;
   char*                   h;
   char*                   a[5]={"","","",""};

/* defines environment variable LANG to German EBCDIC for IBM mainframes */
#ifdef __ZOS__
   __setenv("LANG","de_DE.IBM-1141",1);
#endif

   if (argc==2) { /* parse HOST parameter string */
      i=2; a[0]=argv[0]; a[1]=argv[1];
      h=strchr(a[1],',');
      if (h!=NULL) { h[0]=0x00; a[2]=h+1; i++; }
      h=strchr(a[2],',');
      if (h!=NULL) { h[0]=0x00; a[3]=h+1; i++; }
      argc=i; argv=a;
   }

   if (argc!=4) {
      fprintf(stderr,"Parameter count (%d) not correct to run this sample program\n",argc);
      fprintf(stderr,"Given parameters:\n");
      for (i=0;i<argc;i++) fprintf(stderr,"--> P%2.2u(%s)\n",i,argv[i]);
      fprintf(stderr,"Required parameters:\n");
      fprintf(stderr,"%s \"FILE\" \"write file string\" \"write format string\"\n",argv[0]);
      fprintf(stderr,"WRITE FILE STRING  : \"%s\"\n",fcbsyntax(FLC_WRITE_FILE  ,1,NULL));
      fprintf(stderr,"WRITE FORMAT STRING: \"%s\"\n",fcbsyntax(FLC_WRITE_FORMAT,1,NULL));
      exit(8);
   }

   pvWrt=fcbopen(argv[2],argv[3]);
   if (pvWrt==NULL) {
      fprintf(stderr,"Open file for write operation failed\n");
      fprintf(stderr,"pvWrt=fcbopen(\"%s\",\"%s\");\n\n",argv[2],argv[3]);
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      exit(6);
   }
#ifdef __HAS_CURL__
   CURL*                   curl;
   CURLcode                res;

   curl = curl_easy_init();
   if(curl) {
      /*
      * You better replace the URL with one that works!
      */
      curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
      /* Define our callback to get called when there's data to be written */
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fcbwrite);
      /* Set a pointer to our struct to pass to the callback */
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, pvWrt);

   #ifndef DISABLE_SSH_AGENT
      /* We activate ssh agent. For this to work you need
         to have ssh-agent running (type set | grep SSH_AGENT to check) or
         pageant on Windows (there is an icon in systray if so) */
      curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
   #endif

      /* Switch on full protocol/debug output */
      curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

      res = curl_easy_perform(curl);

      /* always cleanup */
      curl_easy_cleanup(curl);

      if(CURLE_OK != res) {
        /* we failed */
        fprintf(stderr, "curl told us %d\n", res);
      }
   }
   curl_global_cleanup();
#endif

   if (fcbclose(pvWrt)) {
      fprintf(stderr,"Close file for write operation failed\n");
      fprintf(stderr,"fcbclose(pvWrt);\n\n");
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      exit(2);
   }

   return(0);
}

/******************************************************************************/

