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


using namespace cryptrav;


namespace commander
{


int            put(ContextGlob* pContext, StateGlob* pState, QStringList argList)
{
 if ( (pContext == NULL) || (pState == NULL) )
  return COMERR_NULL_POINTER;


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


 if ( optList.contains("--help") )
  {
   Console::putHelp("Available 'put' arguments:");
   Console::putHelp("  <dirname> or <filename>");
   Console::putHelp("Available 'put' options:");
   Console::putHelp("  --help, --ignore-time, --push-new");

   return COMERR_OK;
  }


 QString       objName = "";


 if (comList.count() < 1)
  {
   Console::putFailure("No object name (try 'put --help')");
   return COMERR_NO_OBJECT_NAME;
  }

 objName = comList.at(0);

 if (objName.isEmpty())
  {
   Console::putFailure("Empty object name (try 'put --help')");
   return COMERR_EMPTY_OBJECT_NAME;
  }


 bool isIgnoreTime = optList.contains("--ignore-time");
 bool isPushNew = optList.contains("--push-new");


 Mover         mover(pContext, pState);

 Console::putOutcome( (mover.put(pState -> getPathInHallway(), pState -> getPathInNominis(), objName, isIgnoreTime, isPushNew) == CTERR_OK) );


 return COMERR_OK;
}


int            get(ContextGlob* pContext, StateGlob* pState, QStringList argList)
{
 if ( (pContext == NULL) || (pState == NULL) )
  return COMERR_NULL_POINTER;


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


 if ( optList.contains("--help") )
  {
   Console::putHelp("Available 'get' arguments:");
   Console::putHelp("  <dirname> or <filename>");
   Console::putHelp("Available 'get' options:");
   Console::putHelp("  --help, --ignore-time, --pull-absent, --one-pass");

   return COMERR_OK;
  }


 QString       objName = "";

 if (comList.length() < 1)
  {
   Console::putFailure("No object name (try 'get --help')");
   return COMERR_NO_OBJECT_NAME;
  }

 objName = comList.at(0);

 if (objName.isEmpty())
  {
   Console::putFailure("Empty object name (try 'get --help')");
   return COMERR_EMPTY_OBJECT_NAME;
  }


 bool isIgnoreTime = optList.contains("--ignore-time");
 bool isPullAbsent = optList.contains("--pull-absent");
 bool isOnePass = optList.contains("--one-pass");


 Mover         mover(pContext, pState);

 Console::putOutcome( (mover.get(pState -> getPathInHallway(), pState -> getPathInNominis(), objName, isIgnoreTime, isPullAbsent, isOnePass) == CTERR_OK) );


 return COMERR_OK;
}


} // namespace "commander"
