/*
	Writes out Flexowriter code on a PostScript printer.

	(C) Copyright 2001 by Mogens Kjaer


   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>

/* getopt stuff */

extern char *optarg;
extern int optind, opterr, optopt;
extern int userightshift;

int lpi=6, lines=-1, is_letter=0;

double fontsize=11;
int uadvance=0;

extern unsigned char flx2a(char, int*);
extern unsigned char remove_parity(unsigned char);
extern int ps_open(char *, int, double, int, int, int);
extern int ps_SY(unsigned char);
extern void ps_close();

typedef struct _MEASURES
{
  char papername[24];
  int lines;
} MEASURES;

static MEASURES measures[]=
{ {"A4",          595},
  {"Letter",         },
  {"A3",          842},
  {"A2",         1190},
  {"A1",         1684},
  {"OVERSIZEA1", 1788},
  {"A0",         2384},
  {"OVERSIZEA0", 2567}
};

static MEASURES *measure;

#define BUFFERSIZE 1024

static void printusage()
{
  int i;
  fprintf(stderr, "Usage: flx2ps [options] <input.flx >output.ps\n\n");
  fprintf(stderr, "Options:\n\n");
  fprintf(stderr, "-8            8 lines per inch (default: 6).\n");
  fprintf(stderr, "-4            4 lines per inch (default: 6).\n");
  fprintf(stderr, "-l            letter size paper (default: A4).\n");
  fprintf(stderr, "-p <name>     Use paper size <name> (default: A4).\n");
  for(i=0; i<sizeof(measures)/sizeof(measures[0]); i++)
    fprintf(stderr, "                %s\n", measures[i].papername);
  fprintf(stderr, "-h <number>   Number of lines printed per page (default: A4: 67, Letter: 63).\n");
  fprintf(stderr, "-f <number>   Fontsize (default: 11).\n");
  fprintf(stderr, "-r            Right shift on odd, left shift on even pages\n");
  fprintf(stderr, "-u            Underlines and vertical bar advance position.\n");
}

int main(int argc, char *argv[])
{
  int i,blen;
  char buffer[BUFFERSIZE];
  int opt;
  measure=&(measures[0]);

  while((opt=getopt(argc, argv, "84lh:f:p:ur"))!= -1)
  {
    switch(opt)
    {
    case '8':
      lpi=8;
      break;
    case '4':
      lpi=4;
      break;
    case 'l':
      is_letter=1;
      measure=&(measures[1]);
      break;
    case 'p':
      for(i=0; i<sizeof(measures)/sizeof(measures[0]); i++)
      {
	if(!strcasecmp(optarg,measures[i].papername))
	{
	  measure = &(measures[i]);
	  is_letter=i;
	  break;
	}
      }
      break;
    case 'h':
      sscanf(optarg, "%d", &lines);
      break;
    case 'f':
      sscanf(optarg, "%lg", &fontsize);
      break;
    case 'u':
      uadvance=1;
      break;
    case 'r':
      userightshift=1;
      break;
    default:
      printusage();
      exit(-1);
    }
  }

  if(lines == -1)
  {
    lines = measure->lines;
  }

  ps_open(NULL, is_letter, fontsize, lpi, lines, uadvance);

  while( (blen=read(0, buffer, BUFFERSIZE))>0 )
  {
    for(i=0; i<blen; i++)
    {
      ps_SY(remove_parity(buffer[i]));
    }
  }
  ps_close();
  return 0;
}

