#!/usr/bin/env python
#
# GAMBIT: Global and Modular BSM Inference Tool
#*********************************************
# \file
#
#  Printer harvesting script.
#  Generates printer_rollcall.hpp
#
#  This script identifies all the headers that
#  define GAMBIT printers (output handlers),
#  and includes them in printer_rollcall unless
#  asked not to.
#
#  (duplicated mostly from model_harvester.py)
#
#*********************************************
#
#  Authors (add name and date if you modify):
#
#  \author <NAME>
#          (<EMAIL>)
#    \date 2015
#
#*********************************************
import os
import sys
import getopt
execfile("./Utils/scripts/harvesting_tools.py")

def main(argv):

    printer_headers=set([])  # Printer definition headers
    extra_headers=set([])    # Extra headers needed by printers
    extra_source=set([])     # Extra source files needed by printers
    exclude_printers=set([]) # -Ditch'ed printers

    # Handle command line options
    verbose = False
    try:
       opts, args = getopt.gnu_getopt(argv,"vx:",["verbose","exclude-printers="])
    except getopt.GetoptError:
        print 'Usage: printer_harvestor.py [flags]'
        print ' flags:'
        print '        -v                       : More verbose output'
        print '        -x printer1,printer2,... : Exclude printer1, printer2, etc.'
        sys.exit(2)
    for opt, arg in opts:
      if opt in ('-v','--verbose'):
        verbose = True
        print 'printer_harvester.py: verbose=True'
      elif opt in ('-x','--exclude-printers'):
        exclude_printers.update(neatsplit(",",arg))

    # Get list of printers to include in printers_rollcall.hpp
    printer_headers.update(retrieve_generic_headers(verbose,"./Printers/include/gambit/Printers/printers","printer",exclude_printers))

   # Generate a c++ header containing all the printer headers we have just harvested.
    towrite = "\
//   GAMBIT: Global and Modular BSM Inference Tool\n\
//   *********************************************\n\
///  \\file                                       \n\
///                                               \n\
///  Compile-time registration of GAMBIT printers.\n\
///                                               \n\
///  This file was automatically generated by     \n\
///  printer_harvester.py. Do not modify.         \n\
///                                               \n\
///  Do not add to this if you want to add a new  \n\
///  printer -- just add your printer header to   \n\
///  Printers/include/gambit/Printers/            \n\
///  and rest assured that printer_harvester.py   \n\
///  will make sure it ends up here.              \n\
///                                               \n\
///  *********************************************\n\
///                                               \n\
///  Authors (add name and date if you modify):   \n\
///                                               \n\
///  \\author The GAMBIT Collaboration            \n\
///  \date "+datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")+"\n\
///                                               \n\
///  *********************************************\n\
                                                  \n\
#ifndef __printer_rollcall_hpp__                  \n\
#define __printer_rollcall_hpp__                  \n\
                                                  \n\
// Automatically-generated list of printers.      \n"

    for h in printer_headers:
        towrite+='#include \"gambit/Printers/printers/{0}\"\n'.format(h)
    towrite+="\n#endif // defined __printer_rollcall_hpp__\n"

    # Don't touch any existing file unless it is actually different from what we will create
    header = "./Printers/include/gambit/Printers/printer_rollcall.hpp"
    candidate = "./scratch/printer_rollcall.hpp.candidate"
    with open(candidate,"w") as f: f.write(towrite)
    update_only_if_different(header, candidate)

    if verbose:
        print "\nGenerated printer_rollcall.hpp."

# Handle command line arguments (verbosity)
if __name__ == "__main__":
   main(sys.argv[1:])

