/*
 *  Machtype: determine machine type & display type
 *
 * RCS Info
 *	$Id: machtype_hp.c,v 1.1 1996/06/02 08:25:12 ghudson Exp $
 *	$Locker:  $
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/utsname.h>

int verbose = 0;

void usage (char *name);
void do_cpu (void);
void do_dpy (void);
void do_disk (void);
void do_memory (void);

int main(int argc, char *argv[])
{
    int i;
    int cpuflg = 0, dpyflg = 0, raflg = 0, memflg = 0;
    int doathenaV = 0;
    int dosyspV = 0;
    int dolocalV = 0;
    int dobosN = 0;
    int dobosV = 0;
    int dosysnam = 0;
    FILE *f;

    for (i = 1; i < argc; i++) {
	if (argv[i][0] != '-')
	  usage(argv[0]);

	switch (argv[i][1]) {
	case 'c':
	    cpuflg++;
	    break;
	case 'd':
	    dpyflg++;
	    break;
	case 'r':
	    raflg++;
	    break;
	case 'M':
	    memflg++;
	    break;
	case 'v':
	    verbose++;
	    break;
        case 'A':
	    doathenaV = 1;
	    break;
        case 'P':
	    dosyspV = 1;
	    break;
        case 'L':
	    dolocalV = 1;
	    break;
	case 'N':
	    dobosN = 1;
	    break;
	case 'E':
	    dobosV = 1;
	    break;
	case 'S':
	    dosysnam = 1;
	    break;
	default:
	    usage(argv[0]);
	}
    }


    if ((argc == 1) || ((argc == 2) && verbose)) {
      puts("hp");
      exit(0);
    }

	/* Print out version of Athena machtype compiled against */
    if (doathenaV) {
#ifdef ATHMAJV
      if (verbose)
	printf("Machtype version: %s.%s\n",ATHMAJV,ATHMINV);
      else
	printf("%s.%s\n",ATHMAJV,ATHMINV);
#else
      puts ("???");
#endif
    }

    /* Print out version of attached packs */
    if (dosyspV) {
      char buf[256],rvd_version[256], *p;
      if ((f = fopen("/srvd/.rvdinfo","r")) == NULL) {
	printf("Syspack information unavailable\n");
      } else {
	fgets(buf,256,f);
	fclose(f);
	
	/* If it is verbose, give the whole line, else just the vers # */
	if (verbose) {
	  printf(buf);
	} else {
	  p = strchr(buf,' '); /* skip "Athena" */
	  p = strchr(p+1,' '); /* skip "RVD" */
	  p = strchr(p+1,' '); /* Skip "RSAIX" */
	  p = strchr(p+1,' '); /* skip "version" */
	  strncpy(rvd_version,p+1,256);
	  p = strchr(rvd_version,' ');
	  *p = '\0';
	  printf("%s\n",rvd_version);
	}
      }
    }
	
    /* Print out local version from /etc/athena/version */
    if (dolocalV) {
      char buf[256],loc_version[256], *p;
      if ((f = fopen("/etc/athena/version","r")) == NULL) {
	printf("Local version information unavailable\n");
      } else {
	fseek(f,-100,2);
	while (fgets(buf,256,f) != NULL)
	  ;
	fclose(f);
	
	if (verbose) {
	  printf(buf);
	} else {
	  p = strchr(buf,' '); /* skip "Athena" */
	  p = strchr(p+1,' '); /* skip "Workstation/Server" */
	  p = strchr(p+1,' '); /* Skip "RSAIX" */
	  p = strchr(p+1,' '); /* skip "version" */
	  strncpy(loc_version,p+1,256);
	  p = strchr(loc_version,' ');
	  *p = '\0';
	  printf("%s\n",loc_version);
	}
      }
    }

    /* Print out vendor OS name */
    if (dobosN) {
	struct utsname uts;
	if (uname (&uts) < 0) puts ("???");
	else {
	    if (verbose) printf ("%s %s\n", uts.sysname, uts.release);
	    else puts (uts.sysname);
	}
    }

    /* Print out vendor OS version */
    if (dobosV) {
	struct utsname uts;
	if (uname (&uts) < 0) puts ("???");
	else puts (uts.release);
    }

    if (dosysnam) {
#ifdef ATHSYS
	puts(ATHSYS);
#else
	puts("???");
#endif
    }

    if (cpuflg)
	do_cpu();
    if (dpyflg)
	do_dpy();
    if (raflg)
	do_disk();
    if (memflg)
	do_memory();
    exit(0);
}



void usage(char *name)
{
    fprintf(stderr, "usage: %s [-c] [-d] [-r] [-M] [-v] [-A] [-P] ",name);
    fprintf(stderr, "[-L] [-N] [-E] [-S]\n");
    exit(1);
}


void do_cpu(void)
{
    struct utsname uts;

    if (uname (&uts) < 0) puts ("???");
    else puts (uts.machine);
}

void do_dpy(void)
{
    puts ("hp");
}

void do_disk(void)
{
    puts ("???");
}

void do_memory (void)
{
    puts ("???");
}
