#include "handler.ih"

void Handler::rotate()
{
                            // e.g., /var/lib/cpp/etc/rater
    string const &rotatePath = g_options.rotate();

                            // no rotation if not requested or if a rater
                            // was already specified or if only pdf was 
                            // requested
    if (d_pdf2from or rotatePath.empty() or d_rater.length() > 0)
        return;

    MapStrCCpair::const_iterator iter;

    error_code ec;
    if (not exists(rotatePath, ec))
        iter = d_ccInfo.cbegin();
    else
    {
        ifstream in{ rotatePath };
        string key;
        in >> key;                          // e.g., FB

                                            // e.g., /var/lib/cpp/etc/ccinfo
        iter = d_ccInfo.find(key);          // fm FB: go to the next rater
        if (iter == d_ccInfo.cend())        // key not found
        {
            string msg{ "rater `" + key +"' not available" };
            log() << "Internal error: " << msg << '\n';
            throw Exception{ PANIC } << msg;
        }
                                            // ccinfo entry following FB
        if (++iter == d_ccInfo.cend())      // no next? then use the 1st
            iter = d_ccInfo.cbegin();
    }

    setRaterInfo(iter);

    ofstream out{ rotatePath };
    out << iter->first << '\n';

    log() << "Initial submission assigned to " << d_rater << '\n';
}
