/*
 * cassette label postscript maker.
 *
 * Originally based on a program by Scott Anderson. (sanders3@ub.D.umn.edu)
 * and modified by Greg Belaus (gandalf@athena.mit.edu)
 */

#include <stdio.h>
#include <strings.h>

#define MAXCHARS 255

char a[2][MAXCHARS];					    /* artists */
char t[2][MAXCHARS];					    /* titles */
char y[2][MAXCHARS];					    /* years */

FILE *f[2];

int  artists=1,titles=1;

usage()
{
	puts("usage: clabel <side1-file1> <side2-file2>");
	puts("File format:");
	puts("  <Artist name>");
	puts("  <title name>");
	puts("  <year of composition>");
	puts("  <song1>");
	puts("  <song2>");
	puts("  ...");
	puts("  <songn>");
}

main(argc,argv)
char **argv;
{
	int  i;

	if(argc!=3) usage();

	
	/* Open the input files */

	for(i=0;i<2;i++){
		f[i] = fopen(argv[1+i],"r");
		if(!f[i]){
			perror(argv[1+i]);
			exit(1);
		}

		/* Read the artists and the titles of the sides */

		mygets(a[i],MAXCHARS,f[i]);
		mygets(t[i],MAXCHARS,f[i]);
		mygets(y[i],MAXCHARS,f[i]);
		if(feof(f[i])){
			fprintf(stderr,"End of file on %s?\n",argv[1+i]);
			exit(2);
		}
	}

	/* Determine number of artists and titles */

	if(strcmp(t[0],t[1])) titles=2;
	if(strcmp(a[0],a[1])) artists=2;
	
	fprintf(stderr,"artists: %d  titles: %d\n",artists,titles);

	/* Now print the appropriate prologue */

	prologue();

	/* Do tape #1 */

	printf("afont artist1 (%s)show\n",a[0]);
	printf("tfont title1 (%s)show\n",t[0]);
	printf("yfont year%d (%s) showleft\n",titles==1 ? 2 : 1,y[0]);
	printf("normalfont songs1\n");
	process("new1",f[0]);

	/* Do tape #2 */

	if(titles==2){
		if(artists==2){
			printf("afont artist2 (%s)show\n",a[1]);
		}
		printf("tfont title2 (%s)show\n",t[1]);
		printf("yfont year2 (%s) showleft\n",y[1]);
	}
	printf("normalfont songs2\n");
	process("new2",f[1]);

	/* End */

	puts("showpage");
	exit(0);
}

process(str,file)
char *str;
FILE *file;
{
	char buf[MAXCHARS];

	while(!feof(file)){
		buf[0]=0;
		if(mygets(buf,MAXCHARS,file)){
			printf("%s (%s) show\n",str,buf);
		}
	}
}

/* Like fgets, but deletes the trailing \n */
mygets(s,n,stream)
char *s;
FILE *stream;
{
	char *cc;

	s[0] = 0;
	fgets(s,n,stream);

	if(cc = index(s,26))   *cc = 0;
	if(s[0]==0) return(0);

	if(cc = index(s,'\n')) *cc = 0;

	return(1);
}


#define p(x) puts(x)
prologue()
{
p("%!PS-Adobe-1.0 ");
p("% Values that a user might want to change:");
p("%  fonts and sizes for artists, titles and songs:");
p("  /artistfont /Helvetica-Bold def");
p("  /artistsize 14 def");
p("");
p("  /titlefont  /Helvetica-Bold def");
p("  /titlesize  12 def");
p("");
p("  /yearfont   /Helvetica def");
p("  /yearsize   6 def");
p("");
p("  /songfont   /Helvetica def");
p("  /songsize   10 def");
p("%");
p("% song leding");
p("%");
p("  /songleading songsize 2 add def");
p("%");
p("  /in {72 mul } def ");
p(" /intt { 2 setlinewidth 0 setgray newpath 0 0 moveto }def");
p(" /drawgraybox { .4 setgray  ");
p(" 0 in 4 in lineto  ");
p(" .625 in 4 in lineto  ");
p(" .625 in 0 in lineto ");
p(" 0 0 lineto  ");
p(" fill intt } def ");
p(" /drawtapebox { 2 setlinecap .625 in 0 moveto .625 in 4 in lineto stroke ");
p(" 0 0 moveto 0 4 in lineto stroke ");

p(" 1.125 in 0 moveto 1.125 in 4 in lineto stroke "); /* bottom horiz line */
p(" 3.75 in 0 moveto 3.75 in 4 in lineto stroke ");
p(" 0 0  moveto 3.75 in 0 lineto  stroke ");
if(titles==1){
  p("1.125 in 2 in moveto 3.75 in 2 in lineto stroke "); /* middle vert line */
}
if(titles==2){
  p(".625 in 2 in moveto 3.75 in 2 in lineto stroke "); /* middle vert line */
}
p(" 0 4 in moveto 3.75 in 4 in lineto stroke ");
p(" 0 setlinecap ");
p(" } def ");
p(" /makeit { intt drawgraybox drawtapebox 3.75 in 0 in translate 90 rotate } def ");
p(" /showleft { dup stringwidth pop currentpoint pop exch sub currentpoint exch pop moveto show} def");
p(" /leftmargin 7 def ");
p(" /year1margin 1.9 in def");
p(" /middlemargin 155 def ");
p(" /year2margin 3.9 in def ");
p(" /artist1 { newpath leftmargin 2.9 in moveto } def ");
p(" /artist2 { newpath middlemargin 2.9 in moveto } def ");
p(" /title1 { newpath leftmargin 2.7 in moveto } def ");
p(" /title2 { newpath middlemargin 2.7 in moveto } def ");
p(" /year1  { newpath year1margin 2.7 in moveto } def ");
p(" /year2  { newpath year2margin 2.7 in moveto } def ");
p(" /new1 { currentpoint songleading sub leftmargin exch moveto pop } def ");
p(" /new2 { currentpoint songleading sub middlemargin exch moveto pop } def ");
p(" /afont { artistfont findfont artistsize scalefont setfont} def ");
p(" /tfont { titlefont findfont titlesize scalefont setfont} def ");
p(" /yfont { titlefont findfont yearsize scalefont setfont} def ");
p("/normalfont { songfont findfont songsize scalefont setfont} def ");
p(" /songs1 { newpath leftmargin 2.5 in moveto } def ");
p(" /songs2 { newpath middlemargin 2.5 in moveto } def ");
p(" .5 in 1.0 in translate ");
p(" makeit ");
}
#undef p
