
/*
 *   UUCP.c written by William Loftus
 *   Copyright 1988 by William Loftus.	All rights reserved.
 */

#include <stdio.h>
#include "/version.h"

IDENT(".00");

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

CXBRK()
{
    return(0);
}

void
main(argc,argv)
int argc;
char **argv;
{
    int to_bang, from_bang;

    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("UUSPOOL:")) {
	printf("Couldn't change current working directory to UUSPOOL:\n");
	exit(1);
    }

    read_ctl();

    seq = GetSequence(2);
    if (seq < 0) {
	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++);

    LockFile(command_file);

    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 {
	UnLockFile(command_file);
	perror(command_file);
	chdir(path);
	exit(1);
    }
    UnLockFile(command_file);
    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++);

    LockFile(command_file);

    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 {
	UnLockFile(command_file);
	perror(command_file);
	chdir(path);
	exit(1);
    }
    UnLockFile(command_file);
    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;
	}
    }
}

#define CTL_DELIM " \t\r\n"

/*
 * Read the control file and grab a few parameters.
 */

void
read_ctl()
{
    FILE  *fd;
    static char  buf[128];

    if (! (fd = fopen("UULIB: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);
}


