#include "handler.ih"

    // at this point it is certain that the .pdf must be written

void Handler::latexmk()
{
    if (not g_options.spoolZip())           // the pdf is stored in spoolPath
        d_pdfName = to_string(time(0));
    else
    {                                       // the pdf is stored in the zip
        d_pdfName = d_zip;
    
        size_t pos = d_pdfName.rfind('.');  // remove .zip from the zipname
        if (pos != string::npos)             
            d_pdfName.resize(pos);
    }

    d_exec.execute(
        "/usr/bin/latexmk -pdf "
        "-pdflatex=\"pdflatex -synctex=1 -interaction=nonstopmode\" "
        "-jobname=" + d_pdfName + " job.latex");

    d_pdfName += ".pdf";

    if (not Stat{d_pdfName})
        throw Exception{REPLY} << "could not construct " << d_pdfName;

    log() << "spoolPath: " << g_options.spoolPath() << 
            ", pdfName = " << d_pdfName << '\n';

    chmod(d_pdfName.c_str(), 0664);        
}


