//----------------------------------------------------------------------------
///
/// \file   writerCMake.cpp
///
/// \brief  writes cmake script file with board properties
///
///         If the existing file does not differ from the new one, the file
///         is not touched at all.
///         This will prevent cmake to go crazy when the file keeps being
///         touched. So the unchanged-logic does not have to be implemented in
///         the CMake-Scripts.
///
/// \date   20210813
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Defines------------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#include <QFile>
#include <QDebug>

//---Own------------------------------

#include "writerCMakeDeps.h"
#include "board.h"
#include "app.h"

//--- Implementation ----------------------------------------------------------


CWriterCMakeDeps::CWriterCMakeDeps(CPinmux &pinmux, const QString &basePath)
   :CWriter(pinmux, basePath)
{
   /*
   QFile file( getBasePath() + "/arena_generated_deps.cmake");
   if( ! getBasePath().isEmpty() )
   {
      file.remove();
      //qWarning() << ">>> remove: " << ( getBasePath() + "/arena_generated_deps.cmake") ;
   }
   */
   return;
}

int CWriterCMakeDeps::touch()
{
   QFile file( getBasePath() + "/arena_generated_deps.cmake");
   if( ! file.exists() )
   {
      file.open( QIODevice::WriteOnly );
      file.write("set ( ARENA_PARSED_FILES \"\" )");
   }

   return(0);
}

int CWriterCMakeDeps::writeFiles() /* virtual override */
{
   return(0);
}

int CWriterCMakeDeps::writeCMake( const CConstructorPool &constructors )
{
   // Write to an seperate file. Check later in checkUpdate() if we have to
   // touch the original file.
   QFile file( getBasePath() + "/arena_generated_deps.cmake" + ".new" );
   if (!file.open(QFile::WriteOnly | QFile::Truncate))
   {
      qFatal("Could not open header file for writing: %s", qPrintable( getBasePath() + "/arena_generated_deps.cmake" + ".new" ));
   }
   QTextStream out(&file);
   //const char *category="cmake.";

   out << "#----------------------------------------------------------------------------\n";
   out << "#- File generated by arenas pinmux tool. Don't change it!\n";
   out << "#----------------------------------------------------------------------------\n";
   out << "\n";

   if( board().getLoadedFiles().count() )
   {
      out << "set ( ARENA_PARSED_FILES \n";
      for(const auto &file: board().getLoadedFiles())
      {
         if( file != board().getMainFile() )
         {
            out <<"        " << file <<"\n";
         }
      }
      for(const auto &file: constructors.getLoadedFiles())
      {
         out <<"        " << file <<"\n";
      }
      out << ")\n\n";
   }

   out << "\n";
   out << "#---fin----------------------------------------------------------------------";
   out << "\n";
   file.close();

   checkUpdate();

   return(0);
}


void CWriterCMakeDeps::checkUpdate( )
{
   QString fileName=getBasePath() + "/arena_generated_deps.cmake";
   QFile oldFile( fileName );
   QFile newFile( fileName + ".new");
   QByteArray bufOld;
   QByteArray bufNew;
   bool force=false;

   if (!oldFile.open(QFile::ReadOnly))
   {
      force=true;
   }
   else
   {
      bufOld=oldFile.readAll();
      oldFile.close();
   }

   if (!newFile.open(QFile::ReadOnly))
   {
      force=true;
   }
   else
   {
      bufNew=newFile.readAll();
      newFile.close();
   }

   if( force || ( bufNew != bufOld ) )
   {
      qWarning("### Differ; renaming");
      QFile file(fileName + ".new");
      oldFile.remove();
      bool sta=file.rename(fileName);
      if(!sta)
      {
         qCritical("Could not rename cmake file");
      }
   }
   else
   {
      // qWarning("### Not Differ; remaining");
   }

}


//---fin----------------------------------------------------------------------
