/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  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 3 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, see <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include <QCoreApplication>
#include <QTimer>

#include "cryptrav/cryptrav.h"
#include "commander/commander.h"

#include "initloop.h"
#include "sessionloop.h"


using namespace cryptrav;


int main(int argc, char *argv[])
{
 QCoreApplication app(argc, argv);

 commander::Console::putLine("Naamari v" + QString(APPLICATION_VERSION) + " (" + QString(APPLICATION_DATE) + ")", EGA_MAGENTA);

 ContextGlob* globContextP = new ContextGlob();
 StateGlob* globStateP = new StateGlob(globContextP);


 QString initFilePath = "";
 QString opt;

 for (int i = 1; i < argc; i++) {
   opt = QString(argv[i]);

   if (opt.startsWith("-")) {
       if (opt.compare("--cr") == 0) {
           globContextP -> setPassTermChr(CR_ASCII_CODE);
       } else if (opt.compare("--lf") == 0) {
           globContextP -> setPassTermChr(LF_ASCII_CODE);
       }
// For compatibility (accessibility, perhaps?) with versions prior to 1.3.0,
// when CR (0xD) was appended to password before hashing (instead of 0).
// Mentioned in 'History', but not in manual to avoid divergency...

   } else {
     initFilePath = opt;
   }
  }


 if (initloop(globContextP, initFilePath) == 0)
  {
   sessionloop(globContextP, globStateP);
  }



 delete globStateP;
 delete globContextP;

 QTimer::singleShot(100, &app, SLOT(quit()));
 return app.exec();
}
