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

namespace commander
{


int            init(ContextGlob* pContext, QStringList argList)
{
 if (pContext == NULL)
  return COMERR_NULL_POINTER;

 QStringList comList;
 QStringList optList;
 CommandLineParser::separate(&argList, &comList, &optList);


 if ( optList.contains("--help") )
  {
   Console::putHelp("Available 'init' subcommands:");
   Console::putHelp("  hallway <path>");
   Console::putHelp("  nominis <path>");
   Console::putHelp("  harbour <path>");
   Console::putHelp("  keys <filepath>");
   Console::putHelp("Available 'init' options:");
   Console::putHelp("  --help");

   return COMERR_OK;
  }



 QString       paramact = "";
 QString       value = "";


 if (comList.count() < 1)
  {
   Console::putFailure("No subcommand (try 'init --help')");
   return COMERR_NO_SUBCOMMAND;
  }

 paramact = comList.at(0);


 if (comList.count() < 2)
  {
   Console::putFailure("No path (try 'init --help')");
   return COMERR_EMPTY_OBJECT_NAME;
  }

 value = comList.at(1);


 if (paramact == "hallway")
  {
   Console::putOutcome( pContext -> setHallwayPath(value) );
  }
 else if (paramact == "nominis")
  {
   Console::putOutcome( pContext -> setNominisPath(value) );
  }
 else if (paramact == "harbour")
  {
   Console::putOutcome( pContext -> setHarbourPath(value) );
  }
 else if (paramact == "keys")
  {
   quint8 passHash[HASH_SIZE];

   Console::putLine("Enter passphrase (hidden):");

   ScanPassHash::scan(pContext, passHash);

   Console::putOutcome( (pContext -> loadKeys(value, passHash) == 0) );
   memset(passHash, 0, HASH_SIZE);
  }
 else
  {
   Console::putFailure("Unknown subcommand (try 'init --help')");
   return COMERR_UNKNOWN_SUBCOMMAND;
  }


 return COMERR_OK;
}


} // namespace "commander"
