/**
 * @file   SCFCUDIF.c
 * @brief  Sample program in C to run FLUC-DIFF
 * @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>

#include"FLMRTC.h"
#include"FLUCUP.h"

/**
 * This test program takes a list of file names from command line to
 * logical compare the element net data of these files via the
 * corresponding FLUC subprogram and write the report data to STDOUT.
 *
 * @param argc must be 2 or more
 * @param argv contains the list of file names for flucdiff
 * @return flucup error
 */
int main(int argc, char * argv[])
{
   int                  i,j,siErr;
   char*                h;
   char*                a[256];
   char                 acCmd[1024];
   int                  isRep=0;
   char*                pcOfs="";
   char*                pcFul="";
   char*                pcMin="";

/* 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 (comma separation) */
      a[0]=argv[0]; a[1]=argv[1];
      for (i=2,h=strchr(a[i-1],',');i<256 && h!=NULL;i++,h=strchr(a[i-1],',')) {
         h[0]=0x00;
         a[i]=h+1;
      }
      argc=i; argv=a;
   }

   if (argc<3) {
ERROR:fprintf(stderr,"\nThis program takes options and a list of file names from command line\n"
                     "for a logical compare of the resulting net data after decoding, decryption,\n"
                     "decompression, character conversion and formatting in neutral FLAM elements.\n"
                     "It use the FLUCDIFF subprogram and write the report to STDOUT. The first file\n"
                     "is compared with all remaining files in the list and the program stops at the\n"
                     "first error.\n\n");
      fprintf(stderr,"Parameter count (%d) not correct (<3) to run this sample program\n\n",argc);
      fprintf(stderr,"Given parameter:\n\n");
      for (i=0;i<argc;i++) fprintf(stderr,"--> P%2.2d(%s)\n\n",i,argv[i]);
      fprintf(stderr,"Required parameter:\n\n");
      fprintf(stderr,"   :> %s \"[-f] [-o] [-m] [-r] [reportfile] filename1 filename2 [filename3 ... filenameN]\"\n\n",argv[0]);
      fprintf(stderr,"Options:\n\n");
      fprintf(stderr,"   -f Make a fully compare of data or elements\n");
      fprintf(stderr,"   -o Print byte offsets and values to report file\n");
      fprintf(stderr,"   -m Minimize to protocol output on STDERR\n");
      fprintf(stderr,"   -r The first given filename is used for the report file\n");
      fprintf(stderr,"\n");
      exit(20);
   }

   for (siErr=FLMRTC_OK,j=i=1;i<argc-1 && (siErr==FLMRTC_OK) && argv[i]!=NULL;i++) {
      if (strcmp(argv[i],"-f")==0) {
         pcFul="FULLY"; j++;
         continue;
      }
      if (strcmp(argv[i],"-o")==0) {
         pcOfs="OFFSET"; j++;
         continue;
      }
      if (strcmp(argv[i],"-m")==0) {
         pcOfs="MESSAGE(MIN)"; j++;
         continue;
      }
      if (strcmp(argv[i],"-r")==0) {
         isRep=1; j++;
         continue;
      }
      if (isRep) {
         if (argc==j+3) {
            snprintf(acCmd,sizeof(acCmd),"REPORT='%s' READ.FILE='%s' COMPARE.FILE='%s' %s %s %s",argv[j],argv[j+1],argv[j+2],pcFul,pcOfs,pcMin);
            fprintf(stderr,"FLCL DIFF %s\n",acCmd);
            siErr=flucdiff(acCmd,NULL,":STDERR",NULL);
         } else {
            goto ERROR;
         }
      } else {
         if (argc==j+2) {
            snprintf(acCmd,sizeof(acCmd),"REPORT=STDOUT READ.FILE='%s' COMPARE.FILE='%s' %s %s %s",argv[j],argv[j+1],pcFul,pcOfs,pcMin);
            fprintf(stderr,"FLCL DIFF %s\n",acCmd);
            siErr=flucdiff(acCmd,NULL,":STDERR",NULL);
         } else {
            goto ERROR;
         }
      }
   }
   return(siErr);
}

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