# include "relphot.h"
# define DEBUG 0

int strextend (char *input, char *format,...) {

  char tmpextra[1024], tmpline[1024];
  va_list argp;

  va_start (argp, format);
  vsnprintf (tmpextra, 1024, format, argp);
  snprintf (tmpline, 1024, "%s %s", input, tmpextra);
  strcpy (input, tmpline);

  return TRUE;
}

int launch_region_hosts (RegionHostTable *regionHosts) {

  int i;

  // clear the I/O files
  for (i = 0; i < regionHosts->Nhosts; i++) {
    char *syncfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "meanmags.sync");
    truncate (syncfile, 0);
    free (syncfile);

    char *fitsfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "meanmags.fits");
    truncate (fitsfile, 0);
    free (fitsfile);

    char *imsyncfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "imagemags.sync");
    truncate (imsyncfile, 0);
    free (imsyncfile);

    char *imfitsfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "imagemags.fits");
    truncate (imfitsfile, 0);
    free (imfitsfile);

    char *loopsyncfile = make_filename (CATDIR, regionHosts->hosts[i].hostname, regionHosts->hosts[i].hostID, "loop.sync");
    truncate (loopsyncfile, 0);
    free (loopsyncfile);
  }

  for (i = 0; i < regionHosts->Nhosts; i++) {

    RegionHostInfo *host = &regionHosts->hosts[i];

    // communication files:
    // subset images per host : CATDIR/Image.HOSTNAME.fits
    char filename[1024];
    snprintf (filename, 1024, "%s/Image.%d.fits", CATDIR, host->hostID);

    // write the image subset for this host
    ImageTableSave (filename, host->image, host->Nimage);

    char command[1024];
    snprintf (command, 1024, "relphot %s -parallel-images %s -region-hosts %s -region-hostID %d -D CATDIR %s -region %f %f %f %f -statmode %s -D CAMERA %s -D STAR_TOOFEW %d -minerror %f", 
	      PhotcodeList, filename, REGION_FILE, host->hostID, CATDIR, host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat, STATMODE, CAMERA, STAR_TOOFEW, MIN_ERROR);

    if (VERBOSE)       	     strextend (command, "-v");
    if (VERBOSE2)      	     strextend (command, "-vv");
    if (RESET)         	     strextend (command, "-reset");
    if (RESET_ZEROPTS) 	     strextend (command, "-reset-zpts");
    if (!KEEP_UBERCAL) 	     strextend (command, "-reset-ubercal");
    if (DophotSelect)  	     strextend (command, "-dophot %d", DophotValue);
    if (ImagSelect)    	     strextend (command, "-instmag %f %f", ImagMin, ImagMax);
    if (MaxDensityUse) 	     strextend (command, "-max-density %f", MaxDensityValue);
    if (SyntheticPhotometry) strextend (command, "-synthphot");

    if (UPDATE)        	     strextend (command, "-update");
    if (MOSAIC_ZEROPT) 	     strextend (command, "-mosaic");
    if (FREEZE_IMAGES) 	     strextend (command, "-imfreeze");
    if (FREEZE_MOSAICS)	     strextend (command, "-mosfreeze");
    if (PARALLEL)      	     strextend (command, "-parallel");
    if (PARALLEL_MANUAL)     strextend (command, "-parallel-manual");
    if (PARALLEL_SERIAL)     strextend (command, "-parallel-serial");

    // XXX deprecate this if we are happy with the new version
    if (SET_MREL_VERSION != 1) strextend (command, "-set-mrel-version %d", SET_MREL_VERSION);

    fprintf (stderr, "command: %s\n", command);
    
    if (PARALLEL_REGIONS_MANUAL) continue;

    // launch the job, then wait for it to be done loading catalogs.  force the remote
    // client to generate the file
    char *syncfile = make_filename (CATDIR, host->hostname, host->hostID, "loadcat.sync");
    clear_sync_file (syncfile);

    // launch the job on the remote machine (no handshake)
    int errorInfo = 0;
    int pid = rconnect ("ssh", host->hostname, command, host->stdio, &errorInfo, FALSE);
    if (!pid) {
      if (DEBUG) fprintf (stderr, "failure to start %s (error %d)\n", host->hostname, errorInfo);
      exit (1);
    }
    host->pid = pid; // save for future reference

    // remote client is done, go ahead with next client
    check_sync_file (syncfile, 1);
    free (syncfile);
  }

  if (PARALLEL_REGIONS_MANUAL) {
    fprintf (stderr, "run the relphot_client commands above.  when these are done, hit return\n");
    getchar();
  } else {
    RegionHostTableWaitJobsGetIO (regionHosts, __FILE__, __LINE__, VERBOSE);
  }
 
  int status = TRUE;
  for (i = 0; i < regionHosts->Nhosts; i++) {
    status = status && (regionHosts->hosts[i].status == 0);
  }

  return status;
}
