/**
  @file
  @brief Saves an in-memory CAS table back to persistent storage
  @details There are three versions of this macro, one per build
    target.  The interface is the same.  This version is VIYA and
    saves the named table back to its original source file.
    Note that we cannot collect metadata from the APIs (like in
    mv_castabsave) because the server info may be inacessible to
    the logged in user

    Running SAS code under a system account (like proc util) DOES
    work, so we just make the assumption that the source file and
    library matches the libds.

  @param [in] libds   library.dataset of the CAS table to save
  @param [in] mdebug= (0) Set to 1 to enable verbose logging

  <h4> SAS Macros </h4>
  @li mf_getengine.sas
  @li mfv_getcaslib.sas

  @author 4GL Apps Ltd
  @copyright 4GL Apps Ltd.  This code may only be used within Data
    Controller and may not be re-distributed or re-sold without the
    express permission of 4GL Apps Ltd.
**/

%macro dc_cassave(libds, mdebug=0);
  %local lib eng caslib ds;
  %let lib=%scan(&libds,1,.);
  %let eng=%mf_getengine(&lib);

  %if &eng ne CAS %then %return;

  %let caslib=%mfv_getcaslib(lib=&lib);
  %let ds=%scan(&libds,2,.);
  %if &mdebug=1 %then %put _local_;


  /* ---- save to disk -------------------------------------------------- */
  proc casutil;
    save casdata="&ds"
        incaslib="&caslib"
        casout="&ds"
        outcaslib="&caslib"
        replace;
  quit;
  %put NOTE: Table &caslib..&ds saved;

%mend dc_cassave;
