#include "handler.ih"

void Handler::storeSpoolPath()
{
    if (g_options.spoolPath().empty())
        return;

    d_spoolZip = g_options.spoolPath();

    bool hasRater; 
    if (hasRater = not d_raterDir.empty(); hasRater)
         d_spoolZip += '/' + d_raterDir;        // add the rater to the
                                                // spoolpath
    error_code ec;

    log() << "spool path: " << d_spoolZip << '\n';

    if (not create_directories(d_spoolZip, ec) and ec)
        throw Exception{PANIC} << 
                "can't create spool path `" << d_spoolZip << '\'';

    if (g_options.spoolZip())
    {
        if (g_options.pdf())
            d_exec.execute("/usr/bin/zip -P C++ " + d_zip + ' ' + d_pdfName);

        d_spoolZip += '/' + to_string(time(0)) + '.' + d_zip;
 
        rename(d_zip, d_spoolZip, ec);
        if (ec)
            throw Exception{PANIC} << "cannot mv " << d_zip << " to " << 
                                                                d_spoolZip;
    }
    else // if (d_pdf)  (currently implied: it's either d_spoolZip or d_pdf)
    {
        d_spoolZip += '/' + to_string(time(0)) + ".zip";
        d_exec.execute("/usr/bin/zip -P C++ " + d_spoolZip + ' ' + d_pdfName);
                
//        d_spoolZip += '/' + to_string(time(0)) + ".pdf";
//        rename(d_pdfName, d_spoolZip, ec);
//        if (ec)
//            throw Exception{PANIC} << "cannot mv " << d_pdfName << " to " << 
//                                                                d_spoolZip;
    }

    log() << "spooled: " << d_spoolZip << '\n';
    if (hasRater)                           // zips in a rater-subdir
        chmod(&d_spoolZip.front(), 0664);   // can be removed by the rater
}



