/* $Id: probeTestZipResults 1634 2020-11-04 15:20:31Z miw $                           */
/* ********************************************************************* */

{
 int probeTestZipResults(const char *curdat, char *cmd);
 AUERR= probeTestZipResults(curdat, cmd);
 /* avoid compiler warnings */ { i1=0; i2=0; i3=0; d1=0.0; d2=0.0; d3=0.0; f1=0.f; f2=0.f; f3=0.f; sprintf(text, "%d, %d, %d, %.1f, %.1f, %.1f, %.1f, %.1f, %.1f", i1, i2, i3, d1, d2, d3, f1, f2, f3); }
}
QUIT

struct PerlParameters
   {
    char nameOfPerlScript[64];
    char option[8][64];
   };

int getProbeIdRunNameAndTestMode(char *probeId,char *runName, unsigned int sizeofRunName, char *testMode, unsigned int sizeofTestMode); 
int createSpectraPdf(const char *probeId, const char *inspectionLot, const char *testMode);
int createZipFile(const char *probeId, const char *inspectionLot, const char *testMode);
int transferDataToFtp(const char *topspinPath, const char *probeId, const char *inspectionLot);
int executePerl(struct PerlParameters *parameters);

int probeTestZipResults(const char *curdat, char *cmd)
{
 char dataPath[PATH_MAX], probeId[64], runName[64], testMode[64], zipFileLocation[PATH_MAX];

 sprintf(dataPath, "%s/data/npt", PathXWinNMRInst());
 DATASET("probeTestZipResults", 1, 1, dataPath, "");
 RPAR("PROTON", "all");
 VIEWDATA_SAMEWIN;

 if (getProbeIdRunNameAndTestMode(probeId, runName, sizeof(runName), testMode, sizeof(testMode))<0)
    {
     Proc_err(INFO_OPT, "AU program aborted due to user request");
     ABORT;
    }

 if (createSpectraPdf(probeId, runName, testMode)<0)
    {
     Proc_err(INFO_OPT, "Creation of spectra pdf failed!");
     ABORT;
    }

 if (createZipFile(probeId, runName, testMode)<0)
    {
     Proc_err(INFO_OPT, "Creation of zip file failed!");
     ABORT;
    }

 sprintf(zipFileLocation, "%s/npt/ProbesWithoutFactoryTest/%s_%s_results.zip", PathXWinNMRInst(), probeId, runName);
 if (Proc_err(QUESTION_OPT, "Do you want to transfer the successfully created ZIP file via FTP to Bruker Switzerland?\n\n=> Press 'OK' to transfer zip file via FTP to Bruker Switzerland\n=> Press 'Cancel' if you want to send zip file by other means to Bruker Switzerland\n\nZIP file location: %s", zipFileLocation)==0)
    {
     if (transferDataToFtp(PathXWinNMRInst(), probeId, runName)<0)
        {
         CPR_exec("setdef ackn ok", WAIT_TERM);
         Proc_err(INFO_OPT, "Data transfer to FTP server failed!\n\nPlease, send zip file by other means to Bruker Switzerland.\nZIP file location: %s", zipFileLocation);
         CPR_exec("setdef ackn no", WAIT_TERM);
         ABORT;
        }
     else
        { Proc_err(INFO_OPT, "ZIP file %s_%s.zip was successfully transfered to FTP server!", probeId, runName); }
    }

 return 0;
}

int getProbeIdRunNameAndTestMode(char *probeId,char *runName, unsigned int sizeofRunName, char *testMode, unsigned int sizeofTestMode)
{
#define DEF_chomp(string) \
{ \
 char *s_ptr; \
 for (s_ptr=string; *s_ptr; s_ptr++) \
    { \
     if (*s_ptr=='\r' || *s_ptr=='\n') \
        { *s_ptr='\0'; } \
   } \
}

 char fileList[20][FILE_SELECT_MAX_LINE_LEN], run_p[PATH_MAX], probeId_fp[PATH_MAX];
 int nSelect=20;
 FILE *Fi_probeId;

 sprintf(run_p, "%s/npt/ProbesWithoutFactoryTest/runs/", PathXWinNMRInst());
 while (nSelect!=1)
    {
     if (Proc_err(QUESTION_OPT, "Please, select exactly one probe for which 'NMRPT Probe Test Results' should be compressed into a ZIP file!\n\n=> Press 'OK' to proceed with selection\n=> Press 'Cancel' to abort AU program")==0)
        { FileSelect(run_p, fileList, &nSelect, 0); }
     else
        { ABORT; }
    }

 sprintf(probeId, "%s", fileList[0]);
 sprintf(probeId_fp, "%s%s", run_p, probeId);
 Fi_probeId=fopen(probeId_fp, "r");
 fgets(runName, sizeofRunName, Fi_probeId);
 DEF_chomp(runName);
 fgets(testMode, sizeofTestMode, Fi_probeId);
 DEF_chomp(testMode);
 fclose(Fi_probeId);

 return 0;
}

int createSpectraPdf(const char *probeId, const char *inspectionLot, const char *testMode)
{
 char xml_fp[PATH_MAX], targetPdf_fp[PATH_MAX-13];
 FILE *Fi_xml;

 sprintf(xml_fp, "%s/NptGenerateSpectraPdf.xml", PROCPATH(""));
 if ((Fi_xml=fopen(xml_fp, "w"))==NULL)
    {
     Proc_err(INFO_OPT, "File %s is not writeable", xml_fp);
     ABORT;
    }

 Proc_err(INFO_OPT, "Creation of spectra pdf in progress");
 fprintf(Fi_xml, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
 fprintf(Fi_xml, "<NMRPT>\n");
 fprintf(Fi_xml, "\t<FILE NAME=\"NptGenerateSpectraPdf.xml\" VERSION=\"1.0\"/>\n");

 sprintf(targetPdf_fp, "%s/npt/Probes/%s/%s/%s/Results/customer_%s.pdf", PathXWinNMRInst(), probeId, testMode, inspectionLot, inspectionLot);
 fprintf(Fi_xml, "\t<GENERATESPECTRAPDF REPORTTYPE=\"ProbeReport\" SUMMARY=\"true\" TARGETPDF=\"%s\" TESTMODE=\"%s\">\n", targetPdf_fp, testMode);
 fprintf(Fi_xml, "\t\t<USEDRUNS>\n");
 fprintf(Fi_xml, "\t\t\t<RUN PROBEID=\"%s\" RUNNAME=\"%s\" TESTMODE=\"%s\"/>\n", probeId, inspectionLot, testMode);
 fprintf(Fi_xml, "\t\t</USEDRUNS>\n");
 fprintf(Fi_xml, "\t</GENERATESPECTRAPDF>\n");
 fprintf(Fi_xml, "</NMRPT>\n");
 fclose(Fi_xml);

 if (CPR_exec("nmrpt_ut createspectrapdf", WAIT_TERM)<0)
    {
     Proc_err(INFO_OPT, "Function nmrpt_ut createspectrapdf failed!");
     ABORT;
    }
 if (Proc_err(QUESTION_OPT, "Pdf of probe %s inspection lot %s was created successully.\n\n=> Press 'Ok' to open PDF\n=> Press 'Cancel' to proceed without viewing PDF\n", probeId, inspectionLot)==0)
    {
     char command[PATH_MAX];
     sprintf(command, "sendgui help %s", targetPdf_fp);
     CPR_exec(command, WAIT_TERM);
    }
 return 0;
}

int createZipFile(const char *probeId, const char *inspectionLot, const char *testMode)
{
 struct PerlParameters parameters;

 sprintf(parameters.nameOfPerlScript, "probeTestDataToZip.pl");
 sprintf(parameters.option[0], "%s", probeId);
 sprintf(parameters.option[1], "%s", inspectionLot);
 sprintf(parameters.option[2], "%s", testMode);
 sprintf(parameters.option[3], "%s", "");
 sprintf(parameters.option[4], "%s", "");
 sprintf(parameters.option[5], "%s", "");
 sprintf(parameters.option[6], "%s", "");
 sprintf(parameters.option[7], "%s", "");
 if (executePerl(&parameters)<0)
    {
     Proc_err(INFO_OPT, "Function executePerl(%s) failed!", parameters.nameOfPerlScript);
     ABORT;
    }

 return 0;
}

int transferDataToFtp(const char *topspinPath, const char *probeId, const char *inspectionLot)
{
 struct PerlParameters parameters;

 sprintf(parameters.nameOfPerlScript, "probeTestDataToFtp.pl");
 sprintf(parameters.option[0], "%s", topspinPath);
 sprintf(parameters.option[1], "%s", probeId);
 sprintf(parameters.option[2], "%s", inspectionLot);
 sprintf(parameters.option[3], "%s", "AZE");
 sprintf(parameters.option[4], "%s", "S0G1C2");
 sprintf(parameters.option[5], "%s", "");
 sprintf(parameters.option[6], "%s", "");
 sprintf(parameters.option[7], "%s", "");
 if (executePerl(&parameters)<0)
    {
     Proc_err(INFO_OPT, "Function executePerl(%s) failed!", parameters.nameOfPerlScript);
     ABORT;
    }

 return 0;
}

int executePerl(struct PerlParameters *parameters)
{
 int i, firstLineFlag=1;
 char pathXWinNMRInst[PATH_MAX-100], line[PATH_MAX], perlSource_fp[PATH_MAX], perlTarget_fp[PATH_MAX-12];
 const char** argList = (const char**) malloc((9) * sizeof(char*));
 FILE *Fi_source, *Fi_target;

 sprintf(pathXWinNMRInst, "%s", PathXWinNMRInst());
 sprintf(perlSource_fp, "%s/npt/ProbesWithoutFactoryTest/perl/%s", pathXWinNMRInst, parameters->nameOfPerlScript);
 sprintf(perlTarget_fp, "%s/bin/%s", PathXWinNMRProg(), parameters->nameOfPerlScript);

 if ((Fi_source=fopen(perlSource_fp, "r"))==NULL)
    {
     Proc_err(INFO_OPT, "Source file %s is not readable", perlSource_fp);
     ABORT;
    }

 if ((Fi_target=fopen(perlTarget_fp, "w"))==NULL)
    {
     fclose(Fi_source);
     Proc_err(INFO_OPT, "Target file %s is not writable", perlTarget_fp);
     ABORT;
    }

 while (fgets(line, sizeof(line), Fi_source))
    {
     if (firstLineFlag==1)
        {
#ifdef linux
         fprintf(Fi_target, "#! %s/perl/bin/perl -I%s/perl/lib/perl5\n", pathXWinNMRInst, pathXWinNMRInst);
#else
         fprintf(Fi_target, "#! %s/perl/bin/perl.exe -I%s/perl/lib\n", pathXWinNMRInst, pathXWinNMRInst);
#endif
         firstLineFlag=0;
        }
     else
        {
         fputs(line, Fi_target);
        }
    }

 fclose(Fi_target);
 fclose(Fi_source);

#ifdef linux
 {
  char chmodCommand[PATH_MAX];
  sprintf(chmodCommand, "chmod 755 %s", perlTarget_fp);
  system(chmodCommand);
 }
#endif

 argList[0]=perlTarget_fp;
 for (i=0; i<8; i++)
    { 
     if (strlen(parameters->option[i])>0)
        { argList[i+1]=parameters->option[i]; }
     else
        { argList[i+1]=0; }
    }

 return ExecProgram(argList, 0);
}
