# include "addstar.h"
# include "loadstarpar.h"
# define DEBUG 1

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 loadstarpar_save_remote (StarPar_Stars *stars, int Nstars, HostTable *hosts, SkyRegion *region, char *fullname, AddstarClientOptions *options) {

  char uniquer[12];
  int TIME = time(NULL);
  int PID = getpid();
  snprintf (uniquer, 12, "%05d.%05d", PID, TIME % 100000);

  // if this region is a parallel thing, save and launch remote
  if (!PARALLEL) { 
    loadstarpar_catalog (stars, Nstars, region, fullname, options);
  } else {
    int N = hosts->index[region->hostID];
    HostInfo *hostMach = &hosts->hosts[N];

    // save to a unique filename
    char filename[1024]; // CATDIR/tmpdir/starpar.PID.index.fits
    snprintf (filename, 1024, "%s/tmpdir/starpar.%s.%05d.fits", CATDIR, uniquer, region->index);

    // write the data to the given FITS file
    loadstarpar_save_stars (filename, stars, Nstars);

    int slot = -1;
    while (slot == -1) {
      slot = find_empty_slot ();
      if (slot == -1) {
	usleep (50000);
	slot = harvest_host();
	myAssert (slot != -2, "we should not call harvest_host here if we have open slots");
      }
    }

    // allocate a host for this job
    HostInfo *host = NULL;
    ALLOCATE (host, HostInfo, 1);

    // we want to run this job on the host described by hostMach.  copy
    // immutable data from hostMach to a locally allocated host:
    host->hostID = hostMach->hostID;
    host->hostname = strcreate (hostMach->hostname);
    host->pathname = strcreate (hostMach->pathname);
    InitIOBuffer (&host->stdout, 1000);
    InitIOBuffer (&host->stderr, 1000);

    // got a valid slot, so launch a new host

    // need to generate the remote command
    char command[1024];
    snprintf (command, 1024, "loadstarpar_client");
    strextend (command, "-hostID %d", host->hostID);
    strextend (command, "-D CATDIR %s", CATDIR);
    strextend (command, "-hostdir %s", host->pathname);
    strextend (command, "-cpt %s", region->name);
    strextend (command, "-input %s", filename);

    // 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
    
    save_remote_host (host);
  }
  return TRUE;
}
