#include <applicat.h>
#include <execp.h>
#include <config.h>
#include <utility.h>
#include "cgdll.h"

class TInstall_dll : public TSkeleton_application
{
private:
bool _copy_dll, _makeini, _del_dll;

  bool CopiaDll(const char * dllname);
  bool CreaExeIni(const char * exename);

protected:
  virtual void main_loop() ;
  virtual bool create(); 
  bool CopiaDlls();
  bool InstallaExeIni();
  bool RemoveDlls();
public:
  virtual ~TInstall_dll () {}
  virtual const char * extra_modules() const 
  {return "BA";}
  virtual bool check_autorization() const 
    {return FALSE;}
};

bool TInstall_dll ::CopiaDll(const char * dllname)
{
  bool ok;
  TFilename from("C:\\windows\\system"),to(".");
  from.add(dllname);
  to.add(dllname);
  if (fexist(from))
    ok = fcopy(from, to);
  else
    warning_box("Copia impossibile : %s non รจ presente in %s", dllname, from.path());

  return ok;
}

// crea il .ini in \WINDOWS per i files exe copiati nella directory di EASYCAMPO
bool TInstall_dll ::CreaExeIni(const char * exename)
{
  TFilename ininame("C:\\WINDOWS");
  ininame.add(exename);
  TFilename dllname;

  ininame.ext("ini");
  TConfig ini(ininame);

  dllname.currdir();
  dllname.add("xbs200.dll");
  ini.set_paragraph("Installable ISAMs");
  ini.set("FoxPro 2.0" ,dllname );
  ini.set("FoxPro 2.5" ,dllname );
  ini.set("dBASE III"  ,dllname );
  ini.set("dBASE IV"   ,dllname );
  
  ini.set("Deleted","On","dBase ISAM");
  
  ini.set("ParadoxNetStyle","3.x","Paradox ISAM" );
  return TRUE;
}

// 
bool TInstall_dll ::CopiaDlls()
{
  bool ok=TRUE;
  ok &= CopiaDll("MSAES110.DLL" );
  ok &= CopiaDll("MSAJT112.DLL");
  ok &= CopiaDll("MSAJT200.DLL");
  ok &= CopiaDll("VBDB300.DLL");
  ok &= CopiaDll("VBRUN300.DLL");
  ok &= CopiaDll("XBS200.DLL");

  return ok;
}
// cancella le dll dal WINDOws
bool TInstall_dll ::RemoveDlls()
{
  bool ok=TRUE;
  ok &= (remove("c:\\windows\\system\\MSAES110.DLL" ) == 0);
  ok &= (remove("c:\\windows\\system\\MSAJT112.DLL")== 0);
  ok &= (remove("c:\\windows\\system\\MSAJT200.DLL")== 0);
  ok &= (remove("c:\\windows\\system\\VBDB300.DLL")== 0);
  ok &= (remove("c:\\windows\\system\\VBRUN300.DLL")== 0);
  ok &= (remove("c:\\windows\\system\\XBS200.DLL")== 0);

  return ok;
}


bool TInstall_dll ::InstallaExeIni()
{
  bool ok;

  ok = fexist("MSAES110.DLL" );
  ok &= fexist("MSAJT112.DLL");
  ok &= fexist("MSAJT200.DLL");
  ok &= fexist("VBDB300.DLL");
  ok &= fexist("VBRUN300.DLL");
  ok &= fexist("XBS200.DLL");
  if (!ok)
    error_box("Le DLL non sono presenti nel direttorio corrente");
  else
  {
    // copia dll (ex file PRASSI.PR  inst.exe
    ok &= CreaExeIni("CB0000.EXE");
    ok &= CreaExeIni("TRRICE.EXE");
    ok &= CreaExeIni("VCOPIA.EXE");
  }
  return ok;
}

bool TInstall_dll ::create() 
{
  _copy_dll=TRUE, 
  _makeini=TRUE,
  _del_dll=TRUE;
  int c = argc();
  while (c>1) {
    c--;
    if (strcmp(argv(c),"-d")==0)
      _del_dll=FALSE;
    else if (strcmp(argv(c),"-c")==0)
      _copy_dll=FALSE; 
    else if (strcmp(argv(c),"-i")==0)
      _makeini=FALSE;
    else if (strcmp(argv(c),"-h")==0)
      warning_box("Parametri di lancio: \n  -i  non crea i .ini per i programmi di CG           \n  -c  non copia le DLL nella directory corrente          \n  -d  non cancella le DLL su windows\\system       ");
  }
  return TSkeleton_application::create();
  
}
void TInstall_dll ::main_loop() 
{
  if (_copy_dll)
    CopiaDlls();
  if (_makeini)
    InstallaExeIni();
  if (_del_dll)
    RemoveDlls();
}

int main(int argc,char** argv)
{
  TInstall_dll app;
  app.run(argc, argv, "Installazione DLL per contabilita' generale");
  return TRUE;
}