/*
 * submake.c
 *
 * Handle recursive makes
 *
 *  1998 Straylight/Edgeware
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*----- Header files ------------------------------------------------------*/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "swis.h"
#include "swiv.h"

#include "alloc.h"
#include "glob.h"

/*----- Main code ---------------------------------------------------------*/

typedef struct submk_ctx {
  char **p;
  size_t i;
  size_t sz;
} submk_ctx;

static void submk_add(const char *p, void *ctx)
{
  int len = _swi(OS_FSControl, _inr(0, 5) | _return(5), 37, p, 0, 0, 0, 0);
  char *q = xmalloc(1 - len);
  submk_ctx *sx = ctx;

  _swi(OS_FSControl, _inr(0, 5), 37, p, q, 0, 0, 1 - len);
  if (sx->i == sx->sz) {
    sx->sz += 256;
    sx->p = xrealloc(sx->p, sx->sz * sizeof(sx->p[0]));
  }
  sx->p[sx->i++] = q;
}

static char *nstrcpy(char *d, const char *s)
{
  while ((*d++ = *s++) != 0)
    ;
  return (d - 1);
}

int main(int argc, char *argv[])
{
  char prefix[1024];
  int is_prefix = 1;
  char *tail;
  submk_ctx sx;

  /* --- Remember the original DDEUtils prefix --- */

  {
    int len;
    if (_swix(OS_ReadVarVal, _inr(0, 4) | _out(2),
              "Prefix$Dir", prefix, sizeof(prefix), 0, 0,
              &len))
      is_prefix = 0;
    else
      prefix[len] = 0;
  }

  /* --- Initialise the context --- */

  sx.sz = 256;
  sx.p = xmalloc(sx.sz * sizeof(sx.p[0]));
  sx.i = 0;

  /* --- Mangle the command line arguments --- */

  {
    int i;

    /* --- Expand wildcarded makefile names --- */

    for (i = 1; i < argc && strcmp(argv[i], "--"); i++) {
      if (glob(argv[i], submk_add, &sx) == 0)
        submk_add(argv[i], &sx);
    }

    /* --- Take the rest of the arguments --- *
     *
     * This is one of those times that C shows its Unix roots most
     * irritatingly.  I've got to grab all the carefully separated
     * arguments, and stick them all back together.
     */

    if (i >= argc)
      tail = "";
    else {
      size_t sz = 0;
      int j;
      char *p;

      i++;
      for (j = i; j < argc; j++)
        sz += strlen(argv[j]) + 1;
      tail = xmalloc(sz + 1);
      p = tail;
      *p++ = ' ';
      p = nstrcpy(p, argv[i++]);
      for (j = i; j < argc; j++) {
        *p++ = ' ';
        p = nstrcpy(p, argv[j]);
      }
      *p = 0;
    }
  }

  /* --- Now start work on making things --- */

  {
    int i, e;
    char cmdbuf[1024];

    if (is_prefix)
      _swi(XDDEUtils_Prefix, _in(0), 0);
    for (i = 0; i < sx.i; i++) {
      sprintf(cmdbuf, "amu -desktop -f %s%s", sx.p[i], tail);
      printf("submake: Making%s with `%s'\n", tail, sx.p[i]);
      if ((e = system(cmdbuf)) != 0) {
        printf("submake: Failed%s with `%s' [%i]\n", tail, sx.p[i], e);
        exit(1);
      }
      printf("submake: Successfully made%s with `%s'\n", tail, sx.p[i]);
    }
  }

  /* --- Restore the original prefix --- */

  if (is_prefix)
    _swi(XDDEUtils_Prefix, _in(0), prefix);

  /* --- That's it: return --- */

  return (0);
}
