Index: trunk/FACT++/src/rootifysql.cc
===================================================================
--- trunk/FACT++/src/rootifysql.cc	(revision 19234)
+++ trunk/FACT++/src/rootifysql.cc	(revision 19235)
@@ -28,6 +28,4 @@
         ("ignore-null,i", po_switch(),                 "Do not skip rows containing any NULL field")
         ("display,d",     po_switch(),                 "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
-        ("write,w",       var<string>(""),             "Write output to an ascii file")
-        ("delimiter",     var<string>(),               "The delimiter used if contents are displayed with --display (default=\\t)")
         ("explain",       po_switch(),                 "Requests an EXPLAIN from the server (shows the server optimized query)\nsee also https://dev.mysql.com/doc/refman/explain-output.html")
         ("profiling",     po_switch(),                 "Turn on profiling and print profile")
@@ -37,4 +35,15 @@
         ("print-connection", po_switch(),              "Print database connection information")
         ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
+        ;
+
+    po::options_description ascii("ASCII output");
+    ascii.add_options()
+        ("write,w",       var<string>(""),             "Write output to an ascii file")
+        ("delimiter",     var<string>(),               "The delimiter used if contents are displayed with --display (default=\\t)")
+        ("copy-shabang",  po_switch(),                 "Copy the sha-bang line if exists to the output file")
+        ("copy-header",   po_switch(),                 "Copy the header (all line starting with '#'  up to the first non-comment line to the output file")
+        ("copy-query",    po_switch(),                 "Copy the query to the ascii output file")
+        ("copy-comments", po_switch(),                 "Copy all lines starting with '#' to the output file which are not part of header")
+        ("copy-all",      po_switch(),                 "An alias for --copy-header --copy-query --copy-comments")
         ;
 
@@ -56,4 +65,5 @@
 
     conf.AddOptions(control);
+    conf.AddOptions(ascii);
     conf.AddOptions(root);
     conf.SetArgumentPositions(p);
@@ -481,4 +491,11 @@
     const uint16_t compression = conf.Get<uint16_t>("compression");
     const string   delimiter   = conf.Has("delimiter") ? conf.Get<string>("delimiter") : "\t";
+
+    const bool copy_all        = conf.Get<bool>("copy-all");
+    const bool copy_shabang    = conf.Get<bool>("copy-shabang");
+    const bool copy_header     = copy_all || conf.Get<bool>("copy-header");
+    const bool copy_query      = copy_all || conf.Get<bool>("copy-query");
+    const bool copy_comments   = copy_all || conf.Get<bool>("copy-comments");
+
     const vector<string> _ignore = conf.Vec<string>("ignore");
     const bool  print_connection = conf.Get<bool>("print-connection");
@@ -513,4 +530,5 @@
             return 4;
         }
+
         getline(fin, query, (char)fin.eof());
     }
@@ -861,4 +879,33 @@
     }
 
+    // -------------------------------------------------------------------------
+
+    string contents;
+
+    istringstream istr(query);
+    size_t line = 0;
+    bool header = true;
+    while (istr)
+    {
+        string ibuf;
+        getline(istr, ibuf);
+        const string sbuf = Tools::Trim(ibuf);
+
+        const bool shabang = line==0 && ibuf[0]=='#' && ibuf[1]=='!';
+        const bool comment = sbuf[0]=='#' && !shabang;
+        const bool isquery = !shabang && !comment;
+        if (isquery)
+            header = false;
+
+        line++;
+
+        if ((copy_shabang  && shabang) ||
+            (copy_header   && comment && header)  ||
+            (copy_query    && isquery) ||
+            (copy_comments && comment && !header))
+            contents += '#' + ibuf + '\n';
+
+    }
+
     ofstream fout(write);
     if (!write.empty() && !fout)
@@ -868,9 +915,13 @@
     {
         cout << endl;
+        cout << contents << endl;
         cout << "# " << row.field_list(delimiter.c_str()) << endl;
     }
 
     if (!write.empty())
+    {
+        fout << contents;
         fout << "# " << row.field_list(delimiter.c_str()) << endl;
+    }
 
     // ---------------------- Fill TTree with DB data --------------------------
