/*
   UUCP.c written by William Loftus
   Copyright 1988 by William Loftus.  All rights reserved.
*/
# include <stdio.h>

char to_buf[128];
char from_buf[128];
char path[128];
char user[8];

char command_file[128];
int seq;

void BuildReceiveFile();
void BuildSendFile();
void read_ctl();
char *expand_file();
char *strchr();
char *getcwd();

#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;
{

  int to_bang, from_bang;
  FILE *fp;
  char buf[5];

  if (argc != 3) {
    printf("Usage: UUCP from_file to_file\n");
    exit(1);
  } else {
    strcat(from_buf, argv[1]);
    strcat(to_buf, argv[2]);
  }

  getcwd(path,128);

  if (chdir("UUCP:SPOOL")) {;
    printf("Couldn't change current working directory to UUCP:spool\n");
    exit(1);
  }

  read_ctl();

  fp = fopen("uucp:lib/seq","r");
  if (fp) {
    fgets(buf, sizeof buf, fp);
    seq = atoi(&buf[0]);
    fclose(fp);
  } else {
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }

  fp = fopen("uucp:lib/seq", "w");
  if (fp) {
    fprintf(fp,"%d", seq+2);
    fclose(fp);
  } else {
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }

  from_bang = (int)strchr(from_buf,'!');
  to_bang = (int)strchr(to_buf,'!');

  if (from_bang && to_bang) {
    printf("Can not specify a remote system in both arguments.\n");
    chdir(path);
    exit(1);
  }

  if (from_bang) {
    BuildReceiveFile();
  }

  if (to_bang) {
    BuildSendFile();
  }

  chdir(path);
  exit(0);

}

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

  strcpy(from_buf,expand_file(from_buf));

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

  strncpy(system_name,to_buf,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(command_file,"C.%sA%04d", system_name, seq++);

  fp = fopen(command_file,"w");
  if (fp) {
    /* srcnam desnam who flags temp mode who */
    fprintf(fp,"S %s %s %s %s %s %s %s\n", from_buf, to_buf+bang+1, user,
                "-c",from_buf,"0666",user);
    fclose(fp);
  } else {
    perror(command_file);
    chdir(path);
    exit(1);
  }

  printf("Command queue for transfer to %s.\n", system_name);

}

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

  strcpy(to_buf,expand_file(to_buf));

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

  strncpy(system_name,from_buf,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(command_file,"C.%sA%04d", system_name, seq++);

  fp = fopen(command_file,"w");
  if (fp) {
    /* srcnam desnam who flags */
    fprintf(fp,"R %s %s %s %s\n", from_buf+bang+1, to_buf, user,
                "-c");
    fclose(fp);
  } else {
    perror(command_file);
    chdir(path);
    exit(1);
  }

  printf("Command queue for transfer from %s.\n", system_name);

}

static char name[128];

char *
expand_file(file_name)
  char *file_name;
{
    char *colon;

    colon = strchr(file_name,':');
    if ((colon != (char*)NULL) && (colon != file_name)) {
      return(file_name);
    } else {
      if (path[strlen(path)-1] != ':') {
        sprintf(name,"%s/%s",path,file_name);
        return name;
      } else {
        sprintf(name,"%s%s",path,file_name);
        return name;
      }
    }

}

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

        if (! (fd = fopen("UUCP:lib/config", "r"))) {
                printf("Can't Find config file.\n");
                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);
}


