#include <stdio.h>
#include "rolo.h"
#include "roloP.h"

int complines(rr)
	RoloRec *rr;
{
  int n;
  char *s;
  for ( n = 1, s = rr->text; *s != '\0'; s++ )
    if ( *s == '\n' ) n++;
  if ( s != rr->text )
    if ( *(s-1) != '\n' ) n++;
  return n;
}

int box, equal, fontsize, card;
char *filtertext;

void InitProlo(argc,argv)
     int argc;
     char *argv[];
{
  int c, n;
  char buf[255];
  extern char *optarg;

  box = equal = card = 0;
  filtertext = NULL;
  fontsize = 5;

  while ((c = getopt(argc, argv, "cf:beF:hs:")) != -1) {
    switch (c) {
    case 'c':
      card = 1;
      break;
    case 'f':
      sprintf(buf,"ROLODEX=%s",optarg);
      putenv(buf);
      break;
    case 'b':
      box = 1;
      break;
    case 'e':
      equal = 1;
      break;
    case 'F':
      filtertext = optarg;
      break;
    case 'h':
      fprintf(stderr,"%s [-c] [-b] [-e] [-F filtertext] [-h] [-f rolofile] [-s fontsize]\n",argv[0]);
      exit(1);
      break;
    case 's':
      n = sscanf(optarg,"%d",&fontsize);
      if ( n == 0 ) fontsize = 5;
      break;
    }
  }
}

main(argc,argv)
     int argc;
     char *argv[];
{
  char *text;
  RoloRec *rr;
  int high, height;

  InitProlo(argc,argv);
  InitRolo(argc,argv,0);
  SetupRolo();

  RoloFilter(filtertext);

  /* find tallest entry */
  high = 0;
  for ( rr = RoloList; rr != NULL; rr = rr->next ) {
    if ( ! rr->on ) continue;
    height = complines(rr);
    if ( height > high ) high = height;
  }

  printf("%%!\n\n");
  printf("\n%% user parameters:  feel free to change the following parameters\n");
  printf("/fontsize %d def  %% size for font, play with this!\n",fontsize);
  printf("/equalbox %d def  %% if 1, put each entry in equal sized boxes\n",equal);
  printf("/boxmode %d def   %% if 1, draw boxes around each entry\n",box);
  printf("/cardinfo %d def  %% if 1, print card contents\n",card);
  printf("\n%% other parameters\n");
  printf("/ystart 750 def\n");
  printf("/xwidth 525 def\n");
  printf("/tallest %d def   %% tallest entry in # of lines\n",high);
  printf("cardinfo 0 eq { /tallest 1 def } if\n");
  printf("0.1 setlinewidth\n");
  printf("/Times-Roman findfont fontsize scalefont setfont\n");
  printf("/mw 0 def\n");
  printf("/mw2 0 def\n\n");
  printf("/mw3 0 def\n\n");
  printf("/mw4 0 def\n\n");

  printf("\n%% define procedures\n");
  printf("/newpage { showpage fontsize 5 mul 750 moveto } def\n");
  printf("/movecol { currentpoint pop mw add mw2 add ystart moveto } def\n");
  printf("\n/newcol {  %% shift to a new column, if no more columns shift to new page\n");
  printf("  movecol\n");
  printf("  currentpoint pop mw2 add\n");
  printf("  xwidth gt { newpage } if\n");
  printf("} def\n");

  printf("\n/checkloc {  %% given # of lines, check if we can fit in this column\n");
  printf("  equalbox 1 eq { pop tallest } if  %% if in equal box mode, ignore # lines tall\n");
  printf("  height mul 100 add      %% projected location after next printing (with padding)\n");
  printf("  currentpoint exch pop   %% space left in column\n");
  printf("  gt { newcol } if        %% if it won't fit, start new column\n");
  printf("} def\n");

  printf("\n/boxit {  %% given # of lines, draw a box mw+mw2 wide and # lines tall\n");
  printf("  boxmode 1 eq {\n");
  printf("    equalbox 1 eq { pop tallest } if  %% if in equal box mode, ignore # lines tall\n");
  printf("    gsave\n");
  printf("      currentpoint\n");
  printf("      newpath\n");
  printf("        exch 1 sub exch height sub 1 sub moveto\n");
  printf("        height mul\n");
  printf("        dup 0 exch rlineto\n");
  printf("        mw mw2 add 0 rlineto\n");
  printf("        neg 0 exch rlineto\n");
  printf("      closepath\n");
  printf("      stroke\n");
  printf("    grestore\n");
  printf("  } if\n");
  printf("} def\n");

  printf("\n/fixloc { %% # of lines just printed, adjust for equalbox\n");
  printf("  equalbox 1 eq {\n");
  printf("    height mul neg vjump add  %% compute how much farther down we need to go\n");
  printf("    0 exch rmoveto\n");
  printf("  } if\n");
  printf("} def\n");

  printf("\n");
  printf("/sizestr { stringwidth /h exch def /w exch def } def\n");
  printf("/chmax { w mw gt { /mw w def } if } def\n");
  printf("/chmax2 { w mw2 gt { /mw2 w def } if } def\n");
  printf("/chmax3 { w mw3 gt { /mw3 w def } if } def\n");
  printf("/chmax4 { w mw4 gt { /mw4 w def } if } def\n");
  printf("/nshow { dup show sizestr mw w sub 0 rmoveto } def\n");
  printf("/tshow { cardinfo 1 eq { dup show sizestr w neg height rmoveto } if } def\n");
  printf("/pshow1 { dup show sizestr mw3 w sub 0 rmoveto } def\n");
  printf("/pshow2 { dup show sizestr mw3 w add neg height rmoveto } def\n");
  printf("/height fontsize neg def\n");
  printf("/vjump height tallest mul def\n");

  printf("\n%% find widest entry\n");
  for ( rr = RoloList; rr != NULL; rr = rr->next )
    if ( rr->on ) printf("(%s) sizestr chmax\n",rr->name);

  printf("/mw mw fontsize add def\n");

  printf("\n%% find widest text\n");
  for ( rr = RoloList; rr != NULL; rr = rr->next ) {
    if ( ! rr->on ) continue;
    text = rr->text;
    while ( *text != '\0' ) {
      printf("(");
      while (*text != '\n' && *text != '\0') putc(*text++,stdout);
      printf(") sizestr chmax2\n");
      if ( *text != '\0' ) text++;
    }
  }

  printf("\n%% find widest primary number\n");
  for ( rr = RoloList; rr != NULL; rr = rr->next ) {
    if ( ! rr->on ) continue;
    printf("(%s) sizestr chmax3\n",rr->number);
  }

  printf("/mw3 mw3 fontsize add def\n");

  printf("\n%% find widest secondary number\n");
  for ( rr = RoloList; rr != NULL; rr = rr->next ) {
    if ( ! rr->on ) continue;
    printf("(%s) sizestr chmax4\n",rr->number2);
  }

  printf("mw3 mw4 add mw2 gt { /mw2 mw3 mw4 add def } if\n");

  printf("/mw2 mw2 fontsize add def\n");

  printf("\n%% setup and print\n");
  printf("fontsize 5 mul 750 moveto\n");

  for ( rr = RoloList; rr != NULL; rr = rr->next ) {
    if ( ! rr->on ) continue;
    height = complines(rr);
    printf("%d checkloc\n",height);
    printf("%d boxit\n",height);
    printf("(%s) nshow\n",rr->name);
    printf("(%s) pshow1\n",rr->number);
    printf("(%s) pshow2\n",rr->number2);
    text = rr->text;
    while ( *text != '\0' ) {
      printf("(");
      while (*text != '\n' && *text != '\0') putc(*text++,stdout);
      printf(") tshow\n");
      if ( *text != '\0' ) text++;
    }
    printf("mw neg 0 rmoveto\n");
    printf("%d fixloc\n",height);
  }

  printf("showpage\n");
  return 0;
}

