/*
*  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 "initloop.h"

using namespace cryptrav;
using namespace commander;


int            initloop(ContextGlob* pContext, QString initFilePath)
{
 QString       input;
 QStringList   argList;
 QString       command;


 if (initFilePath.isEmpty())
  do
   {
    Console::putLine("");
    Console::putLine("INIT...");
    Console::putLine("Hallway: \"" + pContext -> getHallwayPath() + "\"");
    Console::putLine("Nominis: \"" + pContext -> getNominisPath() + "\"");
    Console::putLine("Harbour: \"" + pContext -> getHarbourPath() + "\"");
    Console::putLine("Keys: " + ((pContext -> areKeysSet())?QString("set"):QString("not set")));


    input = Console::getLine();

    CommandLineParser::parse(input, &argList);


    if (argList.length() > 0)
     {
      command = argList.at(0);

      if (command == "help")
       Console::putHelp("Available commands: help, init, quit");
      else if (command == "init")
       commander::init(pContext, argList.mid(1));
      else if (command == "quit")
       return (-1);
      else
       Console::putFailure("Unknown command (try 'help')");
     }


   } while ( !(pContext -> isReady()) );

  else

   {
    Console::putLine("Reading \"" + initFilePath + "\"...");

    JSON::InitData initData;
    if ( JSON::readInit(initFilePath, &initData) == CTERR_OK )
     {
      QString path;

      path = initData.pathHallway;
      Console::putLine("Setting Hallway to \"" + path + "\"...");
      bool hallwayPathSet = pContext -> setHallwayPath(path);
      Console::putOutcome(hallwayPathSet);

      path = initData.pathNominis;
      Console::putLine("Setting Nominis to \"" + path + "\"...");
      bool nominisPathSet = pContext -> setNominisPath(path);
      Console::putOutcome(nominisPathSet);

      path = initData.pathHarbour;
      Console::putLine("Setting Harbour to \"" + path + "\"...");
      bool harbourPathSet = pContext -> setHarbourPath(path);
      Console::putOutcome(harbourPathSet);

      if (hallwayPathSet && nominisPathSet && harbourPathSet)
       {
        path = initData.filepathKeys;
        Console::putLine("Loading Protokeys from \"" + path + "\"...");

        quint8 passHash[HASH_SIZE];
        Console::putLine("Enter passphrase (hidden):");
        ScanPassHash::scan(pContext, passHash);

        Console::putLine("Setting Keys...");

        Console::putOutcome( (pContext -> loadKeys(path, passHash) == 0) );
        memset(passHash, 0, HASH_SIZE);
       }

     }

    if ( !(pContext -> isReady()) )
     return (-3);

   }


 return 0;
}
