Add session-util to create a new empty session
[ardour.git] / session_utils / copy-mixer.cc
index e95c07761883701d803470c6b603f2d65ab1d34c..2fa3c1fb14a3234324231dbb44500b5c0bf4fc80 100644 (file)
@@ -1,8 +1,11 @@
 #include <iostream>
 #include <cstdlib>
 #include <getopt.h>
+#include <glibmm.h>
 
+#include "pbd/basename.h"
 #include "pbd/stateful.h"
+#include "ardour/filename_extensions.h"
 #include "ardour/send.h"
 #include "ardour/track.h"
 
@@ -14,6 +17,11 @@ using namespace std;
 using namespace ARDOUR;
 using namespace SessionUtils;
 
+static bool opt_debug_dump = false;
+static bool opt_copy_busses = false;
+static bool opt_verbose = false;
+static bool opt_log = false;
+
 /* this is copied from  Session::new_route_from_template */
 static void
 trim_state_for_mixer_copy (Session*s, XMLNode& node)
@@ -31,37 +39,35 @@ trim_state_for_mixer_copy (Session*s, XMLNode& node)
                                 */
                                XMLProperty const * target = (*i)->property (X_("name"));
                                if (!target) {
-                                       (*i)->add_property ("type", "dangling-aux-send");
+                                       (*i)->set_property ("type", "dangling-aux-send");
                                        continue;
                                }
                                boost::shared_ptr<Route> r = s->route_by_name (target->value ());
                                if (!r || boost::dynamic_pointer_cast<Track> (r)) {
-                                       (*i)->add_property ("type", "dangling-aux-send");
+                                       (*i)->set_property ("type", "dangling-aux-send");
                                        continue;
                                }
-                               (*i)->add_property ("target", r->id ().to_s ());
+                               (*i)->set_property ("target", r->id ().to_s ());
                        }
                        if (role && role->value () == X_("Listen")) {
                                (*i)->remove_property (X_("bitslot"));
                        }
                        else if (role && (role->value () == X_("Send") || role->value () == X_("Aux"))) {
-                               char buf[32];
                                Delivery::Role xrole;
                                uint32_t bitslot = 0;
                                xrole = Delivery::Role (string_2_enum (role->value (), xrole));
                                std::string name = Send::name_and_id_new_send (*s, xrole, bitslot, false);
-                               snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
                                (*i)->remove_property (X_("bitslot"));
                                (*i)->remove_property (X_("name"));
-                               (*i)->add_property ("bitslot", buf);
-                               (*i)->add_property ("name", name);
+                               (*i)->set_property ("bitslot", bitslot);
+                               (*i)->set_property ("name", name);
                        }
                        else if (type && type->value () == X_("intreturn")) {
                                // ignore, in case bus existed in old session,
                                // tracks in old session may be connected to it.
                                // if the bus is new, new_route_from_template()
                                // will have re-created an ID.
-                               (*i)->add_property ("type", "ignore-aux-return");
+                               (*i)->set_property ("type", "ignore-aux-return");
                        }
                        else if (type && type->value () == X_("return")) {
                                // Return::set_state() generates a new one
@@ -70,7 +76,7 @@ trim_state_for_mixer_copy (Session*s, XMLNode& node)
                        else if (type && type->value () == X_("port")) {
                                // PortInsert::set_state() handles the bitslot
                                (*i)->remove_property (X_("bitslot"));
-                               (*i)->add_property ("ignore-name", "1");
+                               (*i)->set_property ("ignore-name", "1");
                        }
                }
        }
@@ -84,7 +90,9 @@ copy_mixer_settings (Session*s, boost::shared_ptr<Route> dst, XMLNode& state)
        trim_state_for_mixer_copy (s, state);
        state.remove_nodes_and_delete ("Diskstream");
        state.remove_nodes_and_delete ("Automation");
-       state.dump (cerr);
+       if (opt_debug_dump) {
+               state.dump (cout);
+       }
 
        dst->set_state (state, PBD::Stateful::loading_state_version);
 }
@@ -94,17 +102,17 @@ copy_session_routes (
                const std::string& src_path, const std::string& src_name,
                const std::string& dst_path, const std::string& dst_load, const std::string& dst_save)
 {
-       SessionUtils::init (false);
+       SessionUtils::init (opt_log);
        Session* s = 0;
 
        typedef std::map<std::string,XMLNode*> StateMap;
        StateMap routestate;
        StateMap buslist;
 
-       s = SessionUtils::load_session (src_path, src_name);
+       s = SessionUtils::load_session (src_path, src_name, false);
 
        if (!s) {
-               printf ("Cannot load source session %s/%s.\n", src_path.c_str (), src_name.c_str ());
+               fprintf (stderr, "Cannot load source session %s/%s.\n", src_path.c_str (), src_name.c_str ());
                SessionUtils::cleanup ();
                return -1;
        }
@@ -130,20 +138,21 @@ copy_session_routes (
        /* open target session */
        s = SessionUtils::load_session (dst_path, dst_load);
        if (!s) {
-               printf ("Cannot load target session %s/%s.\n", dst_path.c_str (), dst_load.c_str ());
+               fprintf (stderr, "Cannot load target session %s/%s.\n", dst_path.c_str (), dst_load.c_str ());
                SessionUtils::cleanup ();
                return -1;
        }
 
        /* iterate over all busses in the src session, add missing ones to target */
-       // TODO: make optional
-       rl = s->get_routes ();
-       for (StateMap::const_iterator i = buslist.begin (); i != buslist.end (); ++i) {
-               if (s->route_by_name (i->first)) {
-                       continue;
+       if (opt_copy_busses) {
+               rl = s->get_routes ();
+               for (StateMap::const_iterator i = buslist.begin (); i != buslist.end (); ++i) {
+                       if (s->route_by_name (i->first)) {
+                               continue;
+                       }
+                       XMLNode& rs (*(i->second));
+                       s->new_route_from_template (1, PresentationInfo::max_order, rs, rs.property (X_("name"))->value (), NewPlaylist);
                }
-               XMLNode& rs (*(i->second));
-               s->new_route_from_template (1, rs, rs.property (X_("name"))->value (), NewPlaylist);
        }
 
        /* iterate over all *busses* in the target session.
@@ -162,10 +171,14 @@ copy_session_routes (
                /* find matching route by name */
                std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
                if (it == routestate.end ()) {
-                       printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
+                       if (opt_verbose) {
+                               printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
+                       }
                        continue;
                }
-               printf ("-- found match '%s'\n", (*i)->name ().c_str ());
+               if (opt_verbose) {
+                       printf ("-- found match '%s'\n", (*i)->name ().c_str ());
+               }
                XMLNode *state = it->second;
                // copy state
                copy_mixer_settings (s, r, *state);
@@ -186,10 +199,14 @@ copy_session_routes (
                /* find matching route by name */
                std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
                if (it == routestate.end ()) {
-                       printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
+                       if (opt_verbose) {
+                               printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
+                       }
                        continue;
                }
-               printf ("-- found match '%s'\n", (*i)->name ().c_str ());
+               if (opt_verbose) {
+                       printf ("-- found match '%s'\n", (*i)->name ().c_str ());
+               }
                XMLNode *state = it->second;
                /* copy state */
                copy_mixer_settings (s, r, *state);
@@ -213,14 +230,24 @@ copy_session_routes (
 
 static void usage (int status) {
        // help2man compatible format (standard GNU help-text)
-       printf ("copy-mixer - copy mixer settings from one session to another.\n\n");
-       printf ("Usage: copy-mixer [ OPTIONS ] <src-path> <name> <dst-path> <name> [name]\n\n");
+       printf (UTILNAME " - copy mixer settings from one session to another.\n\n");
+       printf ("Usage: " UTILNAME " [ OPTIONS ] <src> <dst>\n\n");
+
        printf ("Options:\n\
   -h, --help                 display this help and exit\n\
+  -b, --bus-copy             add busses present in src to dst\n\
+  -d, --debug                print pre-processed XML for each route\n\
+  -l, --log-messages         display libardour log messages\n\
+  -s, --snapshot <name>      create a new snapshot in dst\n\
+  -v, --verbose              show perfomed copy opeations\n\
   -V, --version              print version information and exit\n\
 \n");
+
        printf ("\n\
-.. not yet documented..\n\
+This utility copies mixer-settings from the src-session to the dst-session.\n\
+Both <src> and <dst> are paths to .ardour session files.\n\
+If --snapshot is not given, the <dst> session file is overwritten.\n\
+When --snapshot is set, a new snaphot in the <dst> session is created.\n\
 \n");
 
        printf ("Report bugs to <http://tracker.ardour.org/>\n"
@@ -228,32 +255,60 @@ static void usage (int status) {
        ::exit (status);
 }
 
+static bool ends_with (std::string const& value, std::string const& ending)
+{
+       if (ending.size() > value.size()) return false;
+       return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
+}
 
 int main (int argc, char* argv[])
 {
-       const char *optstring = "hV";
-
-       // TODO add some arguments. (save snapshot, copy busses...)
+       const char *optstring = "bhls:Vv";
 
        const struct option longopts[] = {
-               { "help",       0, 0, 'h' },
-               { "version",    0, 0, 'V' },
+               { "bus-copy",     required_argument, 0, 'b' },
+               { "debug",        no_argument,       0, 'd' },
+               { "help",         no_argument,       0, 'h' },
+               { "log-messages", no_argument,       0, 'l' },
+               { "snapshot",     no_argument,       0, 's' },
+               { "version",      no_argument,       0, 'V' },
+               { "vebose",       no_argument,       0, 'v' },
        };
 
        int c = 0;
+       std::string dst_snapshot_name = "";
 
        while (EOF != (c = getopt_long (argc, argv,
                                        optstring, longopts, (int *) 0))) {
                switch (c) {
+                       case 'b':
+                               opt_copy_busses = true;
+                               break;
+
+                       case 'd':
+                               opt_debug_dump = true;
+                               break;
+
+                       case 'h':
+                               usage (0);
+                               break;
+
+                       case 'l':
+                               opt_log = true;
+                               break;
+
+                       case 's':
+                               dst_snapshot_name = optarg;
+                               break;
 
                        case 'V':
                                printf ("ardour-utils version %s\n\n", VERSIONSTRING);
-                               printf ("Copyright (C) GPL 2015 Robin Gareus <robin@gareus.org>\n");
+                               printf ("Copyright (C) GPL 2016 Robin Gareus <robin@gareus.org>\n");
                                exit (0);
                                break;
 
-                       case 'h':
-                               usage (0);
+                       case 'v':
+                               opt_verbose = true;
                                break;
 
                        default:
@@ -264,12 +319,40 @@ int main (int argc, char* argv[])
 
        // TODO parse path/name  from a single argument.
 
-       if (optind + 4 > argc) {
+       if (optind + 2 > argc) {
                usage (EXIT_FAILURE);
        }
 
+       std::string src = argv[optind];
+       std::string dst = argv[optind + 1];
+
+       // statefile_suffix
+
+       if (!ends_with (src, statefile_suffix)) {
+               fprintf (stderr, "source is not a .ardour session file.\n");
+               exit (1);
+       }
+       if (!ends_with (dst, statefile_suffix)) {
+               fprintf (stderr, "target is not a .ardour session file.\n");
+               exit (1);
+       }
+       if (!Glib::file_test (src, Glib::FILE_TEST_IS_REGULAR)) {
+               fprintf (stderr, "source is not a regular file.\n");
+               exit (1);
+       }
+       if (!Glib::file_test (dst, Glib::FILE_TEST_IS_REGULAR)) {
+               fprintf (stderr, "target is not a regular file.\n");
+               exit (1);
+       }
+
+       std::string src_path = Glib::path_get_dirname (src);
+       std::string src_name = PBD::basename_nosuffix (src);
+       std::string dst_path = Glib::path_get_dirname (dst);
+       std::string dst_name = PBD::basename_nosuffix (dst);
+
+       // TODO check if src != dst ..
        return copy_session_routes (
-                       argv[optind], argv[optind + 1],
-                       argv[optind + 2], argv[optind + 3],
-                       (optind + 4 < argc) ? argv[optind + 4] : "");
+                       src_path, src_name,
+                       dst_path, dst_name,
+                       dst_snapshot_name);
 }