/***************************************************************************

   UUX.c by William Loftus
   Copyright 1988 by William Loftus.   All rights reserved.

   This is not the same as uux on unix (sorry it would take too much of
   my time to write a real one)!

   Usage: uux filename command

   Example: 1> uux mail-message "burdvax!rmail wpl"

***************************************************************************/

# include <stdio.h>

char user[128];
char file_name[128];
char command[128];

char exec_file[128];
char x_exec_file[128];
char command_file[128];
char data_file[128];
int seq;

char path[128];

void read_ctl();
void GetTo();
void GetSubject();
void GetSeq();
void Queue();
void Copy();

#define TRUE 1
#define FALSE 0

int
is_in_L_sys_file(system_name)

  char *system_name;

{
  char buf[256];
  char system[128];
  FILE *fd;

  if (!(fd = fopen("UUCP:lib/L.sys","r"))) {
    printf("Couldn't open UUCP:lib/L.sys file\n");
    chdir(path);
    exit(1);
  }

  sprintf(system,"%s ", system_name);

  while (NULL != fgets(buf, sizeof buf, fd)) {
    if (buf[0] == '#') continue;
    if (strncmp(buf, system, strlen(system)) == 0) {
      fclose(fd);
      return TRUE;
    }
  }

  fclose(fd);
  return FALSE;

}


void
main(argc, argv)
  int argc;
  char **argv;
{
  getcwd(path,128);
  chdir("uucp:spool");
  read_ctl();
  if (argc == 3) {
    strcpy(file_name, argv[1]);
    strcpy(command, argv[2]);
  } else {
    printf("Usage: uux file-name command\n");
    printf("Example: 1> uux mail-message \"burdvax!rmail wpl\"\n");
    chdir(path);
    exit(1);
  }
  GetSeq();
  Queue();
  chdir(path);
}

void
GetSeq()
{
  FILE *fp;
  char buf[128];

  fp = fopen("uucp:lib/seq","r");

  if (fp) {
    fgets(buf, sizeof buf, fp);
    seq = atoi(&buf[0]);
    fclose(fp);
  } else {
    printf("Can't open UUCP:lib/seq for reading.\n");
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }

  fp = fopen("uucp:lib/seq", "w");

  if (fp) {
    fprintf(fp,"%d", seq+4);
    fclose(fp);
  } else {
    printf("Can't open UUCP:lib/seq for writing.\n");
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }
}

void
Queue()
{
  FILE *fp;
  char system_name[32];
  int bang;

  bang = (int)strchr(command,'!');
  bang = bang - (int)command;

  strncpy(system_name,command,bang);

  system_name[bang] = '\0';

  if (!is_in_L_sys_file(system_name)) {
    printf ("System \"%s\" not in L.sys file.\n", system_name);
    chdir(path);
    exit(1);
  }

  system_name[7] = '\0';

  sprintf(exec_file,"D.%sX%04d", system_name, seq++);
  sprintf(x_exec_file,"X.%sX%04d", system_name, seq++);
  sprintf(command_file,"C.%sA%04d", system_name, seq++);
  sprintf(data_file,"D.%sB%04d", system_name, seq);

  fp = fopen(exec_file,"w");
  if (fp) {
    fprintf(fp,"U %s\n", user);
    fprintf(fp,"F %s\n", data_file);
    fprintf(fp,"I %s\n", data_file);
    fprintf(fp,"C %s\n", (char *)command+bang+1);
    fclose(fp);
  } else {
    perror(exec_file);
    chdir(path);
    exit(1);
  }

  fp = fopen(command_file,"w");
  if (fp) {
    fprintf(fp,"S %s %s %s - %s 0666\n", data_file, data_file, user,
             data_file);
    fprintf(fp,"S %s %s %s - %s 0666\n", exec_file, x_exec_file, user,
             exec_file);
    fclose(fp);
  } else {
    perror(command_file);
    chdir(path);
    exit(1);
  }

  chdir(path);
  Copy(file_name, data_file);
  chdir("uucp:spool");

}

/*
 * Read the control file and grab a few parameters.
 */
void
read_ctl()
{
        FILE  *fd;
        char  buf[128];
#define CTL_DELIM " \t\n\r"

        if (! (fd = fopen("uucp:lib/config", "r"))) {
                printf("Can't Find config file");
                perror("config");
                chdir(path);
                exit(3);
        }

        while (NULL != fgets(buf, sizeof buf, fd)) {
                     if (strncmp(buf, "UserName", 8) == 0)
                        strcpy(user, strtok(&buf[9], CTL_DELIM));
        }

        fclose(fd);
}

void
Copy(from, to)
char *from;
char *to;
{
  FILE *fd;
  FILE *td;
  int c;
  char to_buf[128];

  fd = fopen(from, "r");
  if (!fd) {
    printf("Could not open %s.\n", from);
    perror(from);
    chdir(path);
    exit(1);
  }

  strcpy(to_buf, "uucp:spool/");
  strcat(to_buf, to);

  td = fopen(to_buf, "w");
  if (!td) {
    printf("Could not open %s.\n", to_buf);
    perror(to);
    chdir(path);
    exit(1);
  }

  while ((c = fgetc(fd)) != EOF) {
    fputc((char)c, td);
  }

  fclose(fd);
  fclose(td);
}
