#include <sys/types.h>
#include <grp.h>
#include "buffer.h"
#include "readwrite.h"
#include "exit.h"
#include "scan.h"
#include "fmt.h"

void outs(s)
char *s;
{
  if (buffer_puts(buffer_1,s) == -1) _exit(111);
}

int numeric(s) 
char *s;
{
  for (; *s; s++) {
    switch(*s) {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        break;
      default:
        return 0;
    }
  }
  return 1;
}

int main(argc,argv)
int argc;
char **argv;
{
  char *name;
  char *value;
  struct group *gr;
  char strnum[FMT_ULONG];
  char *group;

  name = argv[1];
  if (!name) _exit(100);
  value = argv[2];
  if (!value) _exit(100);

  if (numeric(value)) {
    group = value;
  }
  else {
    gr = getgrnam(value);
    if (!gr) {
      buffer_puts(buffer_2,"fatal: unable to find group ");
      buffer_puts(buffer_2,value);
      buffer_puts(buffer_2,"\n");
      buffer_flush(buffer_2);
      _exit(111);
    }
    strnum[fmt_ulong(strnum,(unsigned long) gr->gr_gid)] = 0;
    group = strnum;
  }

  outs("int ");
  outs(name);
  outs(" = ");
  outs(group);
  outs(";\n");
  if (buffer_flush(buffer_1) == -1) _exit(111);
  _exit(0);
}
