/*
    GENMAKE - a makefile generator macro for the ME Text Editor
    
    (C) Copyright 1988  Marc Adler/Magma Systems    All Rights Reserved

    This macro will examine all of the .C files in the current directory
    and will generate a makefile from the files. You are prompted for
    the compile command, name of the exe file to be created, the name of
    the link file, the name of the default libraries to use, and the name
    of the makefile to generate.
    
    You can set the MEC environment variable to be the command string
    which your compiler expects.

*/

genmake()
{
  OrigBuffer = currbuf();
  ObjListLine = 1;

  /*
    Ask the user for the compile command.
  */
  if ((compile_cmd = getenv("MEC")) == "")
  {
    if ((compile_cmd = get_tty_str("Compile cmd: ")) == "")
      return;
  }

  /*
    Create a temp buffer to hold the makefile.
    Generate an $(OBJS) macro.
  */
  make_buf = setcurrbuf(create_buffer("makegen.$$$"));
  insert("OBJS=\n\n");
  
  /*
    We will search for all C filesin the current directory.
  */
  set_filespec("*.c");

  while ((origfname = fname = next_filespec()) != "")
  {
    /* Let the user know which file we are processing. */
    message("Processing file [%s]", fname);

    if ((pos = index(fname, ".")) > 0)
      fname = substr(fname, 1, pos-1);

    /*
       We will tack another file onto the list of object files defined
       by the $(OBJS) macro.
    */
    goline(ObjListLine);  goeol();
    if (currcol() > 65)
    {
      /* Too many OBJ files. Go onto the next line */
      insert(" \\\n");
      insert(repstr(" ", 10));
      ObjListLine++;
    }
    insert(sprintf("%s.OBJ ", fname));

    /*
      Generate the dependency line   "xxx.OBJ:  xxx.C"
    */
    goeof();
    insert(fname);  insert(".OBJ :");
    setcol(20);
    insert(fname);  insert(".C ");

    /*
      Read the C file into an invisible buffer.
    */
    c_buf = setcurrbuf(create_buffer(origfname));
    
    /*
      Search the C file for all lines which contain
         #include "xxxx"
    */
    while (fsearch("^#include *\"\\c"))
    {
      cl = substr(currline(), currcol(), 100);
      if ((pos = index(cl, "\"")) > 0)
        cl = substr(cl, 1, pos - 1);

      message("Found include file [%s]", cl);

      /*
        Insert the include file in the dependency list.
      */
      setcurrbuf(make_buf);
      goeol();
      insert(" "); insert(cl);

      setcurrbuf(c_buf);
      if (!down())
        break;
      gobol();
    }
    
    /*
      Generate the compile command.
    */
    setcurrbuf(make_buf);
    goeol();
    insert("\n");
    setcol(20);
    insert(sprintf("%s %s\n\n", compile_cmd, origfname));

    delete_buffer(c_buf);
  }

  
  /*
    Get the name of the EXE file and the link file. The user needs only to
    respond with the rootname of the EXE file; we will append the extension.
  */
  exename = get_tty_str("What is the name of the exe file? ");
  if (index(exename, ".") == 0)
    exename = strcat(exename, ".exe");
  if ((linkname = get_tty_str("What is the name of the link file? [lnk] ")) == "")
    linkname = "lnk";

  /*
    Generate the dependency line and command for the EXE file.
  */
  insert(sprintf("%s : $(OBJS)\n", exename));
  setcol(20);
  insert(sprintf("link /co @%s\n", linkname));

  /*
    Copy the $(OBJS) macro into the link file. Get rid of the
    "OBJS=" string and change all \ to +.
  */
  link_buf = create_buffer("link.$$$");
  gobof();
  markline();
  while (strlen(currline()) > 1)
    if (!down())  break;
  up();
  markgroup();
  copy();
  setcurrbuf(link_buf);
  paste();
  gobof();
  for (i = 1;  i <= 5;  i++)  delchar();    /* delete "OBJS=" */
  fsubst("\\", "+", 0);
  goeof();  gobol();
  insert(sprintf("%s\nnul.lst\n", exename));

  /*
    Ask the user for the libraries to use.
  */
  if ((libs = get_tty_str("What libs? [Press ENTER for defaults] ")) != "")
    insert(libs);
  insert("\n");
  writefile(linkname);

  /*
    Write out the makefile. The default name of the makefile will be
    the rootname of the resulting EXE file.
  */
  if ((pos = index(exename, ".")) > 0)
    exename = substr(exename, 1, pos-1);
  if ((makename = 
       get_tty_str("What is the name of the makefile? [%s] ", exename)) == "")
    makename = exename;
  setcurrbuf(make_buf);
  writefile(makename);

  /*
    Get rid of the temp buffers and insure that the focus is with the
    original buffer.
  */
  delete_buffer(link_buf);
  delete_buffer(make_buf);
  show_buffer(OrigBuffer);
}
