/* $Header: boot.c,v 1.13 89/05/11 14:28:51 mar Exp $
 */


long tftp_use();
short noddt = 0;

char host_name_buf[80];
char *host_name = 0;
long host = 0;

char file_buf[80];
char *default_file = "";
char *file = 0;

char gfile_buf[80];
char *gfile = 0;

long gateway = 0;
long net_mask = 0;
long in_me = 0;

main()
{
	extern int flags, vmbinfo;
	unsigned char *mladdr = (unsigned char *)0;
	unsigned char *net_init();
	long trash;
	char buf[500];
	char *local, *s, *ss;
#ifdef vax
	extern int sid, sid_ext;

	if (sid != 0x08000000) {
		printf("Non-Microvax processors not supported (sorry)\n");
		return 1;
	}
#endif
#ifdef ibm032
	rt_init();
#endif ibm032
	
#ifdef ATHENA_INSTALL
	puts("\n\n\nAthena network bootstrap version 1.0\n\n");
    again:
	gateway = 0;
	host = 0;
	buf[0] = 0;
	puts("Boot specification is [localhost=][server:]file\n");
	puts("Boot (just press Enter or wait 30 seconds):");
	if (tgets(300, buf) < 0) {
	    putchar('\n');
	}
	putchar('\n');
#ifdef ibm032
	gfile = "athena_rt";
#endif
#ifdef vax
	switch (sid_ext) {
	case 1:
		gfile = "athena_vs2";
		break;
	case 4:
		gfile = "athena_vs2k";
		break;
	default:
		printf("What kind of machine am I?\n");
		gfile = "athena_vs";
		break;
	}
#endif
	s = buf;
	local = host_name = 0;
	while (*s) {
	    for (ss = s; *s && *s != '=' && *s != ':'; s++);
	    if (*s == '=') {
		if (local == 0 && host_name == 0) {
		    *s++ = 0;
		    local = ss;
		} else {
		    printf("Syntax error, try again.\n");
		    goto again;
		}
	    }
	    if (*s == ':') {
		if (host_name == 0) {
		    *s++ = 0;
		    host_name = ss;
		} else {
		    printf("Syntax error, try again.\n");
		    goto again;
		}
	    }
	    if (*s == 0)
	      gfile = ss;
	}

#ifdef DEBUG
	printf("Myaddr = %s; serverhost = %s; gfile = %s\n",
	       local, host_name, gfile);
#endif
	if (mladdr == 0)
	  mladdr = net_init();

	if (local != 0 && in_me == 0)
	  in_me = res_name(local);
	if (nip_set_address(in_me)) {
	    printf("No NIP response, unable to set local network address\n");
	    goto again;
	}
	if (host_name != 0)
	  host = res_name(host_name);
	file = gfile;
	if (host == 0 &&
	    do_bootp(gfile, mladdr, &trash, &host, &gateway, &file) == 0)
	  goto again;
	if ((in_me & net_mask) == (host & net_mask))
	  gateway = 0;

	printf("Booting %s from ", file);
	print_ip_addr(host);
	if (gateway) {
	    printf(" via ");
	    print_ip_addr(gateway);
	}
	printf(" using local address ");
	print_ip_addr(in_me);
	printf("\n");
	tftp_use(host, file);
	puts("\ntftp done\n\n");
	goto again;

#else /* !defined(ATHENA_INSTALL) */

    in_me = 0;
    puts ("boot flags = "); 
    phex (flags);
    puts ("\n");
    printf("gfile = %s\n", gfile);
    if ((flags&(0x8|0x4)) == 0) {
	    do_bootp(gfile, mladdr, &in_me, &host, &gateway, &file);
    }
    else if (flags & 0x8) {
	puts("Enter generic boot file (");
	puts(gfile);
	puts("):\n");
	if (gets(gfile_buf) != 0)
	  gfile = gfile_buf;
	do_bootp(gfile, mladdr, &in_me, &host, &gateway, &file);
    }
    else {
	if ((flags & 0x4) != 0) {
	    puts("My address");
	    if (in_me == 0)
	      puts("\n");
	    else {
		puts(" (");
		phex(in_me);
		puts("):\n");
	    }
	    if (gets(host_name_buf) != 0)
	      in_me = res_name(host_name_buf);
	}
	else
	  do_bootp(gfile, mladdr, &in_me, &host, &gateway, &file);

	puts("Enter boot file (");
	puts(file);
	puts("):\n");
	if (gets(file_buf) != 0)
	  file = file_buf;

	puts("Enter boot host (");
	phex(host);
	puts("):\n");
	if (gets(host_name_buf) != 0) {
	    host = res_name(host_name_buf);
	    if (host == 0) {
		puts("couldn't parse host ");
		puts(host_name_buf);
		return;
	    }
	}
    }
    
    printf("Booting %s from ", file);
    print_ip_addr (host);
    if (gateway == 0) puts ("\n");
       else {
          printf (" via gateway");
	  print_ip_addr (gateway);
          puts ("\n");
	  }

    tftp_use(host, file);
    puts("\ntftp done\n");
#endif
}


/* XXX this doesn't belong here */

print_ip_addr (a)
     unsigned long a;
{
  unsigned char *ac;

  ac = (unsigned char *) &a;

  printf ("%u.%u.%u.%u",
	  ac[0] & 0xff, ac[1] & 0xff,
	  ac[2] & 0xff, ac[3] & 0xff);
}
 
