#include <applicat.h>
#if XVT_OS == XVT_OS_SCOUNIX

// EDIT Printer File
// Utility for printer configuration files editing
// by MB SEP 1993

#include <mask.h>
#include <utility.h>

#define __BA2300_CPP
#include "ba2300.h"

FILE* mirror;
FILE* mirror2;
FILE* current;
FILE* printerlist;
FILE* codes;
FILE* names;
FILE* defcodes;
FILE* defnames;

int   selprinter=1;


void build_list_file (FILE* inputfile, TFilename& outputfile)
{
  FILE* printerlist;
  TString junk;
  int prog=0;

  printerlist=fopen(outputfile, "w");
  fseek(inputfile, 0, SEEK_SET);
  while (fgets(__tmp_string, 256, inputfile))
  {
    junk=__tmp_string;
    if (junk.find("[")!=-1 && junk.find("[Default]")==-1 && junk.find("[End of File]")==-1)
    {
      junk.ltrim(1);
      junk.rtrim(2);
      fprintf(printerlist, "  ITEM \"%i|%s\"\n", prog++, (const char*)junk);
    }
  }
  fclose(printerlist);
}

bool seek_default_printer (FILE* inputfile, TString& newprinter, long* position, bool justafter)
{
  TString junk;

  fseek(inputfile, 0, SEEK_SET);
  fgets(__tmp_string, 256, inputfile);
  junk=__tmp_string;
  if (junk.find("[")!=-1)
  {  
    if (junk.find("Default")!=-1)
    {
      fgets(__tmp_string, 256, inputfile);
      junk=__tmp_string;
      junk.rtrim(1);
    }
    else
    {
      junk.ltrim(1);
      junk.rtrim(2);
    }
    newprinter=junk;
    return seek_printer(inputfile, newprinter, position, justafter);
  }
  return FALSE;
}


bool seek_printer (FILE* inputfile, TString& printer, long* position, bool justafter)
{
  TString junk;
  long pos;
  char* noteof;

  fseek(inputfile, 0, SEEK_SET);
  do
  {
    pos=ftell(inputfile);
    noteof=fgets(__tmp_string, 256, inputfile);
    junk=__tmp_string;
  }
  while (!((junk.find(printer)!=-1 && junk.find("[")!=-1) || !noteof));

  if (!noteof) return FALSE;
  else
    if (justafter)
    {
      *position=ftell(inputfile);
      return TRUE;
    }
    else
    {
      *position=pos;
      fseek(inputfile, pos, SEEK_SET);
      return TRUE;
    }
}
bool erase_file (TFilename& filename)
{
  FILE* junkfile;

  if ((junkfile=fopen(filename, "r"))!=0)
  {
    fclose (junkfile);
#if XVT_OS == XVT_OS_SCOUNIX
    sprintf (__tmp_string, "rm %s", (char*)filename);
    system (__tmp_string);
#else
    remove(filename);
#endif
    return TRUE;
  } else return FALSE;
}

bool is_present (FILE* inputfile, TString& printer)
{
  TFilename mirror("mirror");
  TString junk;
  FILE* printerlist;
  bool found=FALSE;

  build_list_file (inputfile, mirror);
  printerlist=fopen(mirror, "r");
  while (fgets(__tmp_string, 256, printerlist) && !found)
  {
    junk=__tmp_string;
    if (junk.find(printer)!=-1) found=TRUE;
  }
  fclose(printerlist);

  return found;
}

bool change_default_printer (TFilename& inputfilename, FILE* inputfile, TString& newdefprinter)
{
  FILE* mirror;
  TFilename mirrorname("mirror");
  TString junk;

  if (is_present(inputfile, newdefprinter))
  {
    mirror=fopen (mirrorname, "w");
    fseek (inputfile, 0, SEEK_SET);
    fgets (__tmp_string, 256, inputfile);
    junk=__tmp_string;
    if (junk.find("[Default]")!=-1)
    {
      fputs(__tmp_string, mirror);
      fgets(__tmp_string, 256, inputfile);
      fgets(__tmp_string, 256, inputfile);
    }
    else fprintf(mirror, "[Default]\n");
    fprintf(mirror, "%s\n", (const char*)newdefprinter);
    fputs(__tmp_string, mirror);
    while (fgets(__tmp_string, 256, inputfile))
      fputs (__tmp_string, mirror);
    fclose (mirror);
    fclose (inputfile);
    erase_file (inputfilename);
    rename (mirrorname, inputfilename);
    inputfile = fopen(inputfilename, "r");
    return TRUE;
  }
  else return FALSE;
}

bool build_code_files (FILE* input, TFilename& outcdfile, TFilename& outnmfile, TString& printer)
{
  TString junk;
  TToken_string j2;
  long    pos;
  int     i;
  FILE    *codes, *names;

  if (is_present(input, printer))
  {
    codes=fopen(outcdfile, "w");
    names=fopen(outnmfile, "w");
    seek_printer (input, printer, &pos, TRUE);
    for (i=0; i<7; i++)
      fgets(__tmp_string, 256, input);
    fgets(__tmp_string, 256, input);
    junk=__tmp_string;
    i=0;
    while (junk.find("[")==-1)
    {
      junk.ltrim(14);
      j2=junk;
      j2.restart();
      fprintf(names, "  ITEM \"%i|%s\"\n", i++, (char*)j2.get());
      fputs((char*)j2.get(), codes);
      fgets(__tmp_string, 256, input);
      junk=__tmp_string;
    }
    fclose (names);
    fclose (codes);
    return TRUE;
  }
  else return FALSE;
}

void build_list_box (FILE* names, TMask& mask, short field)
{
  TString replace1, replace2, junk;

  replace1.cut(0);
  replace2.cut(0);
  while(fgets(__tmp_string, 256, names))
  {
    if (__tmp_string[0]!='\n')
    {
      if (replace1.not_empty()) replace1 << '|';
      if (replace2.not_empty()) replace2 << '|';
      junk=__tmp_string;
      junk.ltrim(8);
      while (junk[0] && junk[0]!='|')
      {
        replace1 << junk[0];
        junk.ltrim(1);
      }
      junk.ltrim(1);
      junk.rtrim(2);
      replace2 << junk;
    }
  }
  ((TList_field&)mask.field(field)).replace_items(replace1, replace2);
}

void place_codes_on_file (FILE* names, FILE* codes, FILE* output)
{
  TString name;
  TString junk;

  while (fgets(__tmp_string, 256, names))
  {
    name=__tmp_string;
    item2prname(name, junk);
    name.cut(0);
    name << "Code        = " << junk << "|";
    if (fgets(__tmp_string, 256, codes))
      if (!(__tmp_string=="\n"||__tmp_string==""))
        fprintf(output, "%s%s", (const char*)name, __tmp_string);
  }
}

void item2prname (TString& item, TString& name)
{
  name=item;
  name.ltrim(8);
  name.rtrim(2);
  if (name.not_empty())
    while (name[0]!='|') name.ltrim(1);
  name.ltrim(1);
}

bool seek_first_printer(FILE* input, TString& printer, long* pos, bool justafter)
{
  return seek_printer_no(input, 1, printer, pos, justafter);
}

bool seek_printer_no(FILE* input, int number, TString& printer, long* pos, bool justafter)
{
  FILE* listfile;
  TString junk;
  char* noteof=0;

  if (number==0) return FALSE;
  build_list_file(input, (TFilename)"mirror");
  listfile=fopen("mirror", "r");
  for (int i=0; i<number; i++) noteof=fgets(__tmp_string, 256, listfile);
  if (!noteof) return FALSE;
  junk=__tmp_string;
  item2prname(junk, printer);
  fclose(listfile);
  return seek_printer(input, printer, pos, justafter);
}


class EPF_Application : public TApplication
{
  bool _isnamed;
  bool _haschanged;
  bool _isopened;

  TFilename _mirrorname;
  TFilename _mir2name;
  TFilename _currentname;
  TFilename _plname;
  TFilename _cdname;
  TFilename _dcdname;
  TFilename _nmname;
  TFilename _dnmname;

  DIRECTORY _workdir;

  TString _currentprinter;

public:
  // Constructor
  EPF_Application ();

  // Destructor
  ~EPF_Application ();

protected:
  int  accept_current_name  (bool mode);
  bool create_box           ();
  void delete_printer       ();
  void insert_new_info      (TMask& mask);
  void make_mirror          ();
  void reset_mirror         ();
  void save_box             (bool saveas=FALSE);
  void save_file            (bool saveas=FALSE);
  bool select_printer       ();
  void set_field            (TMask& mask, int fldid, char* str, bool inrun);
  void set_files_to_default ();
  void update_mirror        (TMask& mask);

public:
  void         fill_mask           (TMask& mask, bool inrun=FALSE);
  void         fill_print_codes    (TMask& mask);
  virtual bool menu                (MENU_TAG);
  void         set_current_printer (TString newprinter) { _currentprinter=newprinter; }
  void         update_codes_file   (TString str, int pos);
  void         update_names_file   (TString& str, int items);
};

bool codes_on_key  (TMask_field& f, KEY key);
bool add_on_key    (TMask_field& f, KEY key);
bool names_on_key  (TMask_field& f, KEY key);
bool prev_on_key   (TMask_field& f, KEY key);
bool next_on_key   (TMask_field& f, KEY key);

// global application
EPF_Application epf;

// Constructor
EPF_Application::EPF_Application ():
_plname(FILE_PRINTERS),
_cdname(FILE_CODES),
_nmname(FILE_NAMES),
_dcdname(FILE_DEF_CODES),
_dnmname(FILE_DEF_NAMES),
_isnamed(FALSE),
_isopened(FALSE),
_haschanged(FALSE),
_currentname("printer.ini")
{
  _mirrorname=tmpnam(NULL);
  _mir2name=tmpnam(NULL);
  reset_mirror ();
  set_files_to_default ();
  reset_mirror();
  make_mirror();
  _isopened=TRUE;
}

// Destructor
EPF_Application::~EPF_Application ()
{
  erase_file(_cdname);
  erase_file(_nmname);
  erase_file(_plname);
}

// Handlers for masks
bool names_on_key (TMask_field& f, KEY key)
{
  if (key==K_SPACE)
    epf.fill_print_codes (f.mask());
  return TRUE;
}

bool add_on_key (TMask_field& f, KEY key)
{
  if (key==' ')
  {
    TMask addmask(MSK_4);
    TString junk;
    int items;

    addmask.run();
    f.mask().set_focus();
    junk.cut(0);
    items = ((TList_field&)((TMask&)f.mask()).field(MSK_23_NAMES)).items();
    junk << items << "|" << addmask.get(MSK_4_CODE);
    ((TList_field&)((TMask&)f.mask()).field(MSK_23_NAMES)).add_item(junk);
    junk = addmask.get(MSK_4_CODE);
    epf.update_names_file (junk, items);
    epf.fill_print_codes (f.mask());
    
  }
  return TRUE;
}

bool codes_on_key (TMask_field& f, KEY key)
{
  TString junk;
  int intjunk;

  if (key==K_TAB)
  {
    if (f.focusdirty())
    {  
      junk = f.mask().get(MSK_23_NAMES);
      intjunk = atoi(junk);
      epf.update_codes_file (f.mask().get(MSK_23_CODES), intjunk);
    }
  }
  return TRUE;
}

bool prev_on_key (TMask_field& f, KEY key)
{
  TString printer;
  long pos;

  if (key==' ')
    if (seek_printer_no(mirror, selprinter-1, printer, &pos))
    {
      selprinter--;
      build_code_files (mirror, (TFilename)FILE_CODES, (TFilename)FILE_NAMES, printer);
      names=fopen(FILE_NAMES, "r");
      build_list_box (names, f.mask(), MSK_23_NAMES);
      fclose(names);
      epf.set_current_printer(printer);
      epf.fill_mask(f.mask(), TRUE);
      epf.fill_print_codes(f.mask());
    }
  f.mask().show();
  return TRUE;
}

bool next_on_key (TMask_field& f, KEY key)
{
  TString printer;
  long pos;

  if (key==' ')
    if (seek_printer_no(mirror, selprinter+1, printer, &pos))
    {
      selprinter++;
      build_code_files (mirror, (TFilename)FILE_CODES, (TFilename)FILE_NAMES, printer);
      names=fopen(FILE_NAMES, "r");
      build_list_box (names, f.mask(), MSK_23_NAMES);
      fclose(names);
      epf.set_current_printer(printer);
      epf.fill_mask(f.mask(), TRUE);
      epf.fill_print_codes(f.mask());
    }
  f.mask().show();
  return TRUE;
}


// Methods
void EPF_Application::save_box(bool saveas)
{
  if (yesno_box("Il file corrente e' stato modificato: vuoi salvarlo?"))
    save_file(saveas);
}

int EPF_Application::accept_current_name(bool mode)
{
  FILE_SPEC filetoopen;
  FL_STATUS junk;
  TString   typespec(EXTENSION);
  char      dirjunk[50];

  // ritorna 0 se il file esiste
  //         1 se il file non esiste
  //         2 se l'operazioe e' stata interrotta



  get_dir(&_workdir);
  strcpy(filetoopen.type, ( const char*)typespec);
  filetoopen.dir=_workdir;
  if (mode==OPEN) junk=open_file_dlg(&filetoopen, "Scegli un file di configurazione stampanti");
  else junk=save_file_dlg(&filetoopen, "Digita il nome del nuovo file");
  if (junk==FL_BAD || junk==FL_CANCEL) return 2;
  if (mode==OPEN) _isnamed=TRUE;
  dir_to_str(&(filetoopen.dir), dirjunk, 50);
  _currentname.cut(0);
  _currentname << dirjunk << '/' << filetoopen.name;
  if (mode==OPEN) if ((current =fopen(_currentname, "r"))==NULL) return 1;
  else
  {
    fclose(current );
    return 0;
  }
  else return 0;
}

void EPF_Application::reset_mirror()
{
  erase_file(_mirrorname);
  mirror =fopen(_mirrorname, "a+");
}

bool EPF_Application::create_box()
{
  return yesno_box("Il file specificato non esiste, vuoi crearlo?");
}

void EPF_Application::make_mirror()
{
  fseek(mirror , 0, SEEK_SET);
  current =fopen(_currentname, "r");
  while (fgets(__tmp_string, 256, current ))
    fputs(__tmp_string, mirror );
  fclose(current );
}

void EPF_Application::update_mirror (TMask& mask)
{
  TString   linetoadd;
  TFilename junk;


  mirror2 =fopen(_mir2name, "w");
  fseek(mirror , 0, SEEK_SET);

  fgets(__tmp_string, 256, mirror );
  linetoadd=__tmp_string;
  while (linetoadd.find(_currentprinter)==-1 || linetoadd.find("[")==-1)
  {
    fputs (__tmp_string, mirror2 );
    fgets (__tmp_string, 256, mirror );
    linetoadd=__tmp_string;
  }
  linetoadd.cut(0);
  linetoadd << "[" << (char*)mask.get(MSK_23_LOGIC) << ']' << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Printer type= " << (const char*)mask.get(MSK_23_TYPE) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Device name = " << (const char*)mask.get(MSK_23_PHYSIC) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Filter name = " << (const char*)mask.get(MSK_23_FILTER) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Normal      = " << (const char*)mask.get(MSK_23_NORMAL) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Bold        = " << (const char*)mask.get(MSK_23_BOLD) << '\n';
  fprintf(mirror2 , "%s", const (char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Italic      = " << (const char*)mask.get(MSK_23_ITALIC) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);
  linetoadd.cut(0);
  linetoadd << "Underlined  = " << (const char*)mask.get(MSK_23_UNDERLINED) << '\n';
  fprintf(mirror2 , "%s", (const char*)linetoadd);

  names=fopen(FILE_NAMES, "r");
  codes=fopen(FILE_CODES, "r");
  place_codes_on_file (names, codes, mirror2 );
  fgets(__tmp_string, 256, mirror );
  linetoadd=__tmp_string;
  while(linetoadd.find("[")==-1)
  {
    fgets (__tmp_string, 256, mirror );
    linetoadd=__tmp_string;
  }
  fputs (__tmp_string, mirror2 );
  while (fgets(__tmp_string, 256, mirror ))
  {
    fputs (__tmp_string, mirror2 );
  }

#if XVT_OS != XVT_OS_SCOUNIX
  fflush(mirror2 );
#endif
  fclose(mirror2 );
  fclose(mirror );
  fclose(names);
  fclose(codes);
  junk=_mirrorname;
  _mirrorname=_mir2name;
  _mir2name=junk;
  mirror =fopen(_mirrorname, "a+");
}

void EPF_Application::delete_printer ()
{
  TString linetoadd;
  TFilename junk;

  mirror2 =fopen(_mir2name, "w");
  fseek(mirror , 0, SEEK_SET);

  fgets(__tmp_string, 256, mirror );
  linetoadd=__tmp_string;
  while (linetoadd.find(_currentprinter)==-1 || linetoadd.find("[")==-1)
  {
    fputs (__tmp_string, mirror2 );
    fgets (__tmp_string, 256, mirror );
    linetoadd=__tmp_string;
  }
  fgets(__tmp_string, 256, mirror );
  linetoadd=__tmp_string;
  while(linetoadd.find("[")==-1)
  {
    fgets (__tmp_string, 256, mirror );
    linetoadd=__tmp_string;
  }
  fputs (__tmp_string, mirror2 );
  while (fgets(__tmp_string, 256, mirror ))
  {
    fputs (__tmp_string, mirror2 );
  }

#if XVT_OS != XVT_OS_SCOUNIX
  fflush(mirror2 );
#endif
  fclose(mirror2 );
  fclose(mirror );
  junk=_mirrorname;
  _mirrorname=_mir2name;
  _mir2name=junk;
  mirror =fopen(_mirrorname, "a+");
}

void EPF_Application::save_file (bool saveas)
{
  int junk=0;
  TString currents;

  if (!saveas)
  {
    if (!_haschanged) return;
    if (!_isnamed) saveas=TRUE;
  }
  if (saveas) junk=accept_current_name (SAVE);
  if (junk!=2)
  {
    fclose (current);
    current = fopen (_currentname, "w");
    fseek(mirror , 0, SEEK_SET);
    fgets(__tmp_string, 256, mirror );
    currents = __tmp_string;
    if (currents.find("Default")==-1)
    {  
      currents.ltrim(1);
      currents.rtrim(2);
      fprintf(current , "[Default]\n");
      fprintf(current , "%s\n", (char*)currents);
    }
    fseek(mirror , 0, SEEK_SET);
    while (fgets(__tmp_string, 256, mirror ))
      fputs(__tmp_string, current );
    fclose(current );
    fseek(mirror , 0, SEEK_END);
  }
}

bool EPF_Application::select_printer ()
{
  TMask mask2(MSK_1);
  int numb;
  long pos;

  build_list_file(mirror, _plname);
  printerlist=fopen(_plname, "r");
  build_list_box(printerlist, mask2, MSK_1_SELECTED);
  mask2.run();
  _currentprinter=mask2.get(MSK_1_SELECTED);
  numb=atoi(_currentprinter)+1;
  seek_printer_no(mirror, numb, _currentprinter, &pos);
  fclose(printerlist);
  return (mask2.last_key()==K_ENTER ? TRUE : FALSE);
}

void EPF_Application::set_field (TMask& mask, int fldid, char* str, bool inrun)
{
  mask.set(fldid, str);
}

void EPF_Application::fill_mask (TMask& mask, bool inrun) // a ba2300
{
  TString junk;
  bool found=FALSE;

  fseek(mirror , 0, SEEK_SET);
  while (fgets(__tmp_string, 256, mirror ) && !found)
  {
    junk=__tmp_string;
    if (junk.find(_currentprinter)!=-1 && junk.find("[")!=-1)
    {
      found=TRUE;
      set_field(mask, MSK_23_LOGIC, (const char *) _currentprinter, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_TYPE, junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_PHYSIC, (const char*)junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_FILTER, (const char *)junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_NORMAL, (const char *) junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_BOLD, (const char *) junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_ITALIC, (const char *) junk, inrun);
      fgets(__tmp_string, 256, mirror );
      junk=__tmp_string;
      junk.ltrim(14);
      junk.rtrim(1);
      set_field(mask, MSK_23_UNDERLINED, (const char *) junk, inrun);
    }
  }
}

void EPF_Application::fill_print_codes (TMask& mask)
{
  TString junk;
  int intjunk;
  char* ok;

  junk=mask.get(MSK_23_NAMES);
  intjunk=atoi(junk);

  codes=fopen(_cdname, "r");
  for (int i=0; i<intjunk; i++)
    fgets(__tmp_string, 256, codes);
  ok = fgets(__tmp_string, 256, codes);
  if (ok) mask.set(MSK_23_CODES, __tmp_string);
  else mask.set(MSK_23_CODES, "");

  fclose(codes);
}


void EPF_Application::insert_new_info (TMask& mask)
{
  TString linetoadd;
  TFilename junk;
  char* k;

  mirror2 =fopen(_mir2name, "w");
  fseek(mirror , 0, SEEK_SET);

  k = fgets(__tmp_string, 256, mirror );
  while ((strcmp(__tmp_string, "[End of File]\n")) && k)
  {
    fputs (__tmp_string, mirror2 );
    k = fgets(__tmp_string, 256, mirror );
  }

  linetoadd.cut(0);
  linetoadd << "[" << (const char *) mask.get(MSK_23_LOGIC) << ']' << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Printer type= " << (const char *) mask.get(MSK_23_TYPE) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Device name = " << (const char *) mask.get(MSK_23_PHYSIC) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Filter name = " << (const char *) mask.get(MSK_23_FILTER) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Normal      = " << (const char *) mask.get(MSK_23_NORMAL) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Bold        = " << (const char *) mask.get(MSK_23_BOLD) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Italic      = " << (const char *) mask.get(MSK_23_ITALIC) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  linetoadd.cut(0);
  linetoadd << "Underlined  = " << (const char *) mask.get(MSK_23_UNDERLINED) << '\n';
  fprintf(mirror2 , "%s", (const char *) linetoadd);
  names=fopen(FILE_NAMES, "r");
  codes=fopen(FILE_CODES, "r");
  place_codes_on_file (names, codes, mirror2 );
  linetoadd.cut(0);
  linetoadd << "[End of File]\n";
  fprintf(mirror2, "%s", (const char *) linetoadd);

  fclose(mirror2 );
  fclose(mirror );
  fclose(names);
  fclose(codes);
  junk=_mirrorname;
  _mirrorname=_mir2name;
  _mir2name=junk;
  mirror =fopen(_mirrorname, "a+");
}

void EPF_Application::set_files_to_default ()
{
  codes=fopen(_cdname, "w");
  defcodes=fopen(_dcdname, "r");

  if (defcodes != NULL)
  {
    while (fgets(__tmp_string, 256, defcodes))
      fputs(__tmp_string, codes);

    fclose(defcodes);
  }
  fclose(codes);

  names=fopen(_nmname, "w");
  defnames=fopen(_dnmname, "r");
  if (defnames != NULL)
  {
    while (fgets(__tmp_string, 256, defnames))
      fputs (__tmp_string, names);

    fclose(defnames);
  }
  fclose(names);
}

void EPF_Application::update_names_file (TString& str, int items)
{
  names = fopen (_nmname, "a");
  fprintf(names, "  ITEM \"%i|%s\"\n", items, (char*)str);
  fclose (names);
}

void EPF_Application::update_codes_file (TString str, int pos)
{
  int currentpos;
  TString dirstr(30);

  codes = fopen(_cdname, "r");
  mirror2  = fopen(_mir2name, "w");
  for (currentpos=0; currentpos<pos && fgets(__tmp_string, 256, codes); currentpos++)
    fputs(__tmp_string, mirror2 );
  if (currentpos==pos)
  {
    fgets(__tmp_string, 256, codes);
    fprintf(mirror2 , "%s\n", (char*)str);
    while (fgets(__tmp_string, 256, codes))
      fputs(__tmp_string, mirror2 );
  }
  else
  {
    for(;currentpos<pos; currentpos++)
      fprintf(mirror2 , "\n");
    fprintf(mirror2 , "%s\n", (char*)str);
  }
  fclose(codes);
  fclose(mirror2 );
#if XVT_OS == XVT_OS_SCOUNIX
  sprintf(__tmp_string, "rm %s", (char*)_cdname);
  system(__tmp_string);
  dir_to_str(&_workdir, (char*)dirstr, 30);
  sprintf(__tmp_string, "mv %s %s/%s", (char*)_mir2name, (char*)dirstr, (char*)_cdname);
  system(__tmp_string);
#else
  remove(_cdname);
  rename(_mir2name, _cdname);
#endif
}


// Menu

bool EPF_Application::menu(MENU_TAG m)
{
  int junk;

  switch (m)
  {
  case NEW_FILE:
    if (_haschanged) save_box();
    _isnamed=FALSE;
    junk=accept_current_name(OPEN);   // da modificare in base a XVTILITY
    if (junk<2)
    {
      reset_mirror();
      _isopened=TRUE;
      _currentprinter="** new file **";
    }
    break;
  case OPEN_FILE:
  {
    // 0=file aperto esistente
    // 1=file aperto nuovo
    // 2=non aprire nessun file
    int open_status=0;
    long pos=0;

    if (_haschanged) save_box();
    open_status=accept_current_name(OPEN);  // in base a XVTILITY
    if (open_status==1) open_status=(create_box()?1:2);
    if (open_status<2) reset_mirror();
    if (open_status==0) make_mirror();
    if (open_status<2) _isopened=TRUE;
    current=fopen(_currentname, "r");
    seek_default_printer(current, _currentprinter, &pos);
    fclose(current);
  }      
    break;
  case SAVE_FILE:
    if (_haschanged) save_file();
    _haschanged=FALSE;
    break;
  case SAVE_FILE_AS:
    save_file(TRUE);
    _haschanged=FALSE;
    break;
  case QUIT_EPF:
    if (_haschanged) save_box();
    xvt_terminate();
    break;
  case INSERT_PRINTER:
  {
    TMask mask(MSK_2);

    set_files_to_default ();
    get_dir(&_workdir);
    names=fopen(_nmname, "r");
    build_list_box (names, mask, MSK_23_NAMES);
    fclose(names);
    _haschanged=TRUE;
    fill_print_codes (mask);
    mask.set_handler(MSK_23_NAMES, names_on_key);
    mask.set_handler(MSK_2_ADD, add_on_key);
    mask.set_handler(MSK_23_CODES, codes_on_key);
    mask.run();
    if (mask.last_key()==K_ENTER) insert_new_info(mask);
  }
    break;
  case MODIFY_PRINTER:
    if (_isopened && _currentprinter.find("** new file **")==-1)
    {
      _haschanged=TRUE;
      build_list_file(mirror, _plname);
      if (select_printer())
      {
        TMask mask(MSK_2);

        build_code_files(mirror, _cdname, _nmname, _currentprinter);
        names=fopen(_nmname, "r");
        build_list_box (names, mask, MSK_23_NAMES);
        fclose(names);
        fill_print_codes (mask);
        fill_mask(mask);
        mask.set_handler (MSK_23_NAMES, names_on_key);
        mask.set_handler (MSK_2_ADD, add_on_key);
        mask.set_handler (MSK_23_CODES, codes_on_key);
        mask.run();
        if (mask.last_key()==K_ENTER) update_mirror(mask);
      }
    } else warning_box ("Nessun file aperto");
    break;
  case SHOW_PRINTER:
  {
    long pos;

    if (_isopened && _currentprinter.find("** new file **")==-1)
    {
      TMask mask(MSK_3);

      selprinter=1;
      seek_first_printer(mirror, _currentprinter, &pos);
      build_code_files(mirror, _cdname, _nmname, _currentprinter);
      names=fopen(_nmname, "r");
      build_list_box (names, mask, MSK_23_NAMES);
      fclose(names);
      build_list_file(mirror, _plname);
      fill_print_codes (mask);
      mask.set_handler(MSK_23_NAMES, names_on_key);
      mask.set_handler(MSK_3_PREVIOUS, prev_on_key);
      mask.set_handler(MSK_3_NEXT, next_on_key);
      fill_mask(mask, TRUE);
      mask.deactivate();
      mask.enable(MSK_23_NAMES);
      mask.enable(MSK_3_PREVIOUS);
      mask.enable(MSK_3_NEXT);

      mask.run();
    } else warning_box ("Nessun file aperto");
  }
    break;
  case DELETE_PRINTER:
    if (_isopened)
    {
      _haschanged=TRUE;
      build_list_file(mirror, _plname);
      if (select_printer())
        delete_printer();
    } else warning_box ("Nessun file aperto");
    break;
  default:
    break;
  }

  return TRUE;
}

#endif

int ba2300(int argc, char* argv[])
{
#if XVT_OS == XVT_OS_SCOUNIX
  epf.run(argc, argv, "Manutenzione stampanti");
#endif  
  return 0;
}

