/*
 *	$Source: /afs/athena.mit.edu/contrib/faces/src/labels/RCS/labels.c,v $
 *	$Author: jtkohl $
 *	$Header: /afs/athena.mit.edu/contrib/faces/src/labels/RCS/labels.c,v 1.2 89/08/09 10:28:16 jtkohl Exp $
 */

#ifndef lint
static char rcsid_labels_c[] = "$Header: /afs/athena.mit.edu/contrib/faces/src/labels/RCS/labels.c,v 1.2 89/08/09 10:28:16 jtkohl Exp $";
#endif lint

/* Simple program to read in a UUNET formatted picture and other info
 * and print 30 labels via PostScript.
 *
 * Copyright 1988,1989 Metron Computerware, Ltd. Oakland, CA 94602
 * Unpublished work of Metron Computerware, Ltd.  All rights reserved.
 *
 * Last revised 07 June 1989.
 *
 * This program first outputs a postscript program to stdout.
 * It then reads stdin or a file to get the data and the hexified image.
 * It sends this postscript stuff to stdout.
 *
 * Usage: labels [fn] [< uunet hexpicturefile] > postscriptdevice
 *        if any argument is supplied at all, the program expects to read
 *        that as a uunet formatted file.  Otherwise it reads that format
 *        from stdin.
 *
 * cc -o labels labels.c
 *
 */

#include <stdio.h>

int nlines;

char scanline[256];                 /* Input for one scanline           */
main(argc,argv)
    int argc;
    char **argv;
{
    int d,i,j,k,l,m,x;
    char inbuffer[256];
    char textout[256];
    char fname[128];                /* The first name behind the face   */
    char lname[128];                /* The last name behind the face    */
    char company[128];
    char address1[128];               /* Street address                   */
    char address2[128];               /* Street address                   */
    char citystatezip[128];
    char telephone[128];
    char netaddr[128];              /* Net address, if any              */
    char pixinfo[128];              /* width, height, depth of data     */
    char imageinfo[128];            /* width, height, depth of image    */
    unsigned char ch,*cp;
    FILE *fp;
    int done;
    char *facesave = "USENIX Baltimore Conference FaceSaver Project, Metron Studios, June 1989";
    int pt = 18;                    /* pointsize for message            */

    k = 128;                        /* Normal image                     */
    l = 108;

    if (argc > 1) {
	fp = fopen(argv[1],"r");
	if (fp == NULL) {
	    printf("Can't open %s for input\n",argv[1]);
	    exit (1);
	}
    }
    else
	fp = stdin;


    /* Get the header text input, remove assumptions about order */
    nlines = 0;
    done = 0;
    while(fgets(inbuffer,  127,fp)) {
	shorten(inbuffer);
	switch(ch = inbuffer[0]) {
	case '\0':
	case '\n':
	    done = 1;
	    break;
	case 'F':       /* First Name   */
	    stripcopy(fname,inbuffer,0);
	    break;
	case 'L':       /* Last Name    */
	    stripcopy(lname,inbuffer,0);
	    break;
	case 'E':       /* E-mail       */
	    stripcopy(netaddr,inbuffer,1);
	    break;
	case 'C':       /* Company      */
	    if (inbuffer[1] == 'o')
		stripcopy(company,inbuffer,1);
	    else        /* CityStateZip */
		stripcopy(citystatezip,inbuffer,1);
	    break;
	case 'A':       /* Street address*/
	    if (inbuffer[7] == '1')
		stripcopy(address1,inbuffer,1);
	    else
		stripcopy(address2,inbuffer,1);
	    break;
	case 'T':       /* Voice telephone */
	    stripcopy(telephone,inbuffer,1);
	    break;
	case 'P':       /* Picture      */
	    stripcopy(pixinfo,inbuffer,0);
	    break;
	case 'I':       /* Image        */
	default:        /* Discard anything else */
	    /* Throw away aspect info, as we assume it  */
	    break;
	}
	if (done)
	    break;
    }

    sscanf(pixinfo,"%d %d %d",&l,&k,&d);
    if (l == 108 || l == 96)
	k = 128;
    else {
	printf("Bad picture width %d\n", l);
	exit (1);
    }

    strcat(fname," ");
    strcat(fname,lname);
    nlines++;

#ifdef LABELCMD
    system(LABELCMD);        /* Output the main postscript stuff */
#else
    system("cat labels.ps");        /* Output the main postscript stuff */
#endif
    printf("/width { %d } def\n",l);
    printf("/data [ %d (%s) (%s) (%s) (%s) (%s) (%s) (%s)] def\nmain\n",
	nlines,fname,telephone,netaddr,company,address1,address2,citystatezip);

    while(fgets(inbuffer,  127,fp))
	printf("%s",inbuffer);

    /* Terminate the PostScript with an EOF */
    printf("/#copies 1 def\nshowpage\n\004");
}

/* Strip off header, copy info to buffer */
stripcopy(out,in,n)
    register char *out,*in;
{
    register char *cp;
    for( ; *in; in++)
	if (*in == ':') {
	    in++;
	    break;
	}
    if (*in == ' ')
	in++;
    if (strlen(in))
	nlines += n;
    strcpy(out,in);
}

shorten(buf)
    char *buf;
{
    register char *cp;
    for (cp = buf + strlen(buf) -1; cp != buf; cp--) {
	if (*cp != '\n' && *cp != ' ')
	    break;
	*cp = '\0';
    }
}


