/**
 * @file   FLPGPKEY.c
 * @brief  Sample program in C to run FLUC-KEY
 * @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<ctype.h>

#include"FLUCUP.h"

static void printHelp(void) {
   fprintf(stderr,"\n");
   fprintf(stderr,"Usage: flpgpkey OPTION [command string]\n");
   fprintf(stderr,"Does management of PGP keys\n");
   fprintf(stderr,"\n");
   fprintf(stderr,"  Options:\n");
   fprintf(stderr,"   -?            Prints this help list or help for a certain command\n");
   fprintf(stderr,"   -h            Prints this help list or help for a certain command \n");
   fprintf(stderr,"   -g            Generate PGP keys\n");
   fprintf(stderr,"   -e            Export PGP keys\n");
   fprintf(stderr,"   -i            Import PGP keys\n");
   fprintf(stderr,"   -l            List PGP keys\n");
   fprintf(stderr,"   -d            Delete PGP keys\n");
   fprintf(stderr,"\n");
   fprintf(stderr,"  Command string:\n");
   fprintf(stderr,"   use -h g     to get help for the generate command string\n");
   fprintf(stderr,"   use -h e     to get help for the export command string\n");
   fprintf(stderr,"   use -h i     to get help for the import command string\n");
   fprintf(stderr,"   use -h l     to get help for the list command string\n");
   fprintf(stderr,"   use -h d     to get help for the delete command string\n");
   fprintf(stderr,"\n");
}

/**
 * This test program takes parameter from command line to run import,
 * generate, export, list and delete functions for PGP key management.
 *
 * @param argc must be 2 or more
 * @param argv[1] must contain the command (-i -g -e -l -d)
 * @param argv[2] must contain the command string conform to FLCL
 * @return flucup error
 */
int main(int argc, char * argv[])
{
   int                  i,r,l;
   char*                h;
   char*                a[256];

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

   if (argc<2){printHelp(); return -1;}

   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 (argv[1][0]=='-' && (toupper(argv[1][1])=='?' || toupper(argv[1][1])=='H')) {
      if (argc>2) {
         if (toupper(argv[2][0])=='G') {
            flucdoc(FLUC_KEY, "KEY.GENERATE.PGP", NULL);
         } else if (toupper(argv[2][0])=='I') {
            flucdoc(FLUC_KEY, "KEY.IMPORT.PGP", NULL);
         } else if (toupper(argv[2][0])=='E') {
            flucdoc(FLUC_KEY, "KEY.EXPORT.PGP", NULL);
         } else if (toupper(argv[2][0])=='L') {
            flucdoc(FLUC_KEY, "KEY.LIST.PGP", NULL);
         } else if (toupper(argv[2][0])=='D') {
            flucdoc(FLUC_KEY, "KEY.DELETE.PGP", NULL);
         } else printHelp();
      } else printHelp();
   } else {
      for (l=0,i=2;i<argc;i++) l+=strlen(argv[i])+1;
      if (l) {
         h=malloc(l+20);
         if (h!=NULL) {
            if (argv[1][0]=='-' && toupper(argv[1][1])=='G') {
               strcpy(h,"GENERATE.PGP(");
            } else if (argv[1][0]=='-' && toupper(argv[1][1])=='I') {
               strcpy(h,"IMPORT.PGP(");
            } else if (argv[1][0]=='-' && toupper(argv[1][1])=='E') {
               strcpy(h,"EXPORT.PGP(");
            } else if (argv[1][0]=='-' && toupper(argv[1][1])=='L') {
               strcpy(h,"LIST.PGP(");
            } else if (argv[1][0]=='-' && toupper(argv[1][1])=='D') {
               strcpy(h,"DELETE.PGP(");
            } else printHelp();
            for (i=2;i<argc;i++) {
               strcat(h,argv[i]);
               if (i<argc-1) strcat(h," ");
            }
            strcat(h,")");
            fprintf(stderr,"FLCL KEY \"%s\"\n",h);
            r=fluckey(h,NULL,":STDERR",NULL);
            free(h);
            exit(r);
         } else printHelp();
      } else printHelp();
   }
   exit(0);
}

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