/*+headerPackage+*/
/**############################################################################**/
/*                                                                              */
/*  Copyright Bartosz Jablonski, since July 2019 onward.                        */
/*                                                                              */
/*  Code is free and open source. If you want - you can use it.                 */
/*  I tested it the best I could                                                */
/*  but it comes with absolutely no warranty whatsoever.                        */
/*  If you cause any damage or something - it will be your own fault.           */
/*  You have been warned! You are using it on your own risk.                    */
/*  However, if you decide to use it do not forget to mention author:           */
/*  Bartosz Jablonski (yabwon@gmail.com)                                        */
/*                                                                              */
/*  Here is the official version:                                               */
/*
  Copyright (c) 2019 - 2026 Bartosz Jablonski (yabwon@gmail.com)

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included 
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
                                                                                */
/**############################################################################**/

/*** HELP START ***/
/* SPF (SAS Packages Framework) is a set of macros: 
   - to install, 
   - to load, 
   - to get help, 
   - to unload, or 
   - to generate SAS packages.

  SAS Packages Framework, version 20260727.
  See examples below.

  A SAS package is a zip file containing a group of files
  with SAS code (macros, functions, data steps generating 
  data, etc.) wrapped up together and %INCLUDEed by
  a single load.sas file (also embedded inside the zip).

Contributors: 
- Stu Sztukowski 
    LinkedIn: https://www.linkedin.com/in/statsguy/ 
    GitHub: https://github.com/stu-code
- Ken Nakamatsu 
    LinkedIn: https://www.linkedin.com/in/k-nkmt 
    GitHub: https://github.com/k-nkmt

*/
/*** HELP END ***/

/*+unbundlePackages+*/
%macro unbundlePackages(
 bundleName
,path=
,pathRef=
,packagesPath=
,packagesRef=packages
,ods= /* data set for report file */
,reportOnly=0
,verify=0
,quiet=0
)/
des='Macro to extract a bundle of SAS packages, version 20260727. Run %unbundlePackages(HELP) for help info.'
secure
minoperator
;

%if (%superq(bundleName) = ) OR (%qupcase(&bundleName.) = HELP) %then
  %do;
    %local options_tmp ;
    %let options_tmp = ls=%sysfunc(getoption(ls)) ps=%sysfunc(getoption(ps))
     %sysfunc(getoption(notes)) %sysfunc(getoption(source))
     msglevel=%sysfunc(getoption(msglevel))
    ;
    options NOnotes NOsource ls=MAX ps=MAX msglevel=N;
    %put ;
    %put #################################################################################;
    %put ###     This is short help information for the `unbundlePackages` macro         #;
    %put #-------------------------------------------------------------------------------#;
    %put #                                                                               #;
    %put # Macro to *extract* SAS packages from a bundle, version `20260727`             #;
    %put #                                                                               #;
    %put # A SAS package is a zip file containing a group                                #;
    %put # of SAS codes (macros, functions, data steps generating                        #;
    %put # data, etc.) wrapped up together and included by                               #;
    %put # a single `load.sas` file (also embedded inside the zip).                      #;
    %put #                                                                               #;
    %put # The `%nrstr(%%unbundlePackages())` macro allows to extract SAS packages from           #;
    %put # a bundle into a single directory.                                             #;
    %put #                                                                               #;
    %put #-------------------------------------------------------------------------------#;
    %put #### Parameters:                                                                #;
    %put #                                                                               #;
    %put # 1. `bundleName`       *Required.* Name of a bundle, e.g. myBundle,            #;
    %put #                       extension `.bundle.zip` is automatically added.         #;
    %put #                       For empty value or `HELP` this help information         #;
    %put #                       is displayed.                                           #;
    %put #                                                                               #;
    %put # - `path=`             *Required.* Location of the bundle. Must be a valid     #;
    %put #                       directory. Takes precedence over `pathRef` parameter.   #;
    %put #                       Either `path=` or `pathRef=` must be non-empty.         #;
    %put #                                                                               #;
    %put # - `pathRef=`          *Optional.* Fileref to location of the bundle.          #;
    %put #                       Either `path=` or `pathRef=` must be non-empty.         #;
    %put #                                                                               #;
    %put # - `packagesPath=`     *Optional.* Location for packages extracted from        #;
    %put #                       the bundle. Takes precedence over `packagesRef`.        #;
    %put #                       When non-empty, must be a valid directory.              #;
    %put #                                                                               #;
    %put # - `packagesRef=`      *Optional.* Fileref to location where packages will     #;
    %put #                       be extracted. Default value is `packages`.              #;
    %put #                                                                               #;
    %put # - `ods=`              *Optional.* V7 style name of SAS data set for report.   #;
    %put #                                                                               #;
    %put # - `verify=`           *Optional.* Indicates if verification code should       #;
    %put #                       be executed after bundle extraction.                    #;
    %put #                       Value `1` means yes, Value `0` (default) means no.      #;
    %put #                                                                               #;
    %put # - `reportOnly=`       *Optional.* Indicates if packages unbundling should     #;
    %put #                       be suspended and only a report with bundle content      #;
    %put #                       be produced and printed.                                #;
    %put #                       Value `1` means yes, Value `0` (default) means no.      #;
    %put #                                                                               #;
    %put # - `quiet=`           *Optional.* Indicates if printout of the summary         #;
    %put #                       report should be suspended.                             #;
    %put #                       Value `1` means yes, Value `0` (default) means no.      #;
    %put #                                                                               #;
    %put #-------------------------------------------------------------------------------#;
    %put #                                                                               #;
    %put # Visit: `https://github.com/yabwon/SAS_PACKAGES/tree/main/SPF/Documentation`   #;
    %put # to learn more.                                                                #;
    %put # Tutorials available at: `https://github.com/yabwon/HoW-SASPackages`           #;
    %put #                                                                               #;
    %put ### Example 1 ###################################################################;
    %put #                                                                               #;
    %put #   Enabling the SAS Package Framework                                          #;
    %put #   from the local directory and extract a bundle of                            #;
    %put #   packages from user home directory to packages.                              #;
    %put #                                                                               #;
    %put #   Assume that the `SPFinit.sas` file                                          #;
    %put #   is located in the "/sas/PACKAGES/" folder.                                  #;
    %put #                                                                               #;
    %put #   Run the following code in your SAS session:                                 #;
    %put ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sas;
    %put  %nrstr( filename packages "/sas/PACKAGES/";  %%* setup a directory for packages;);
    %put  %nrstr( %%include packages(SPFinit.sas);      %%* enable the framework;         );
    %put  ;
    %put  %nrstr( %%unbundlePackages%(myLittleBundle                                      );
    %put  %nrstr(                  ,path=/home/user/bundles                               );
    %put  %nrstr(                  ,verify=1                                              );
    %put  %nrstr(                  ,packagesRef=PACKAGES%)                                );
    %put ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
    %put #                                                                               #;
    %put #################################################################################;
    %put ;
    options &options_tmp.;
    %GOTO ENDofunbundlePackages;
  %end;
  /* local variables for options */
  %local ls_tmp ps_tmp notes_tmp source_tmp stimer_tmp fullstimer_tmp msglevel_tmp mautocomploc_tmp;
  %let ls_tmp         = %sysfunc(getoption(ls));
  %let ps_tmp         = %sysfunc(getoption(ps));
  %let notes_tmp      = %sysfunc(getoption(notes));
  %let source_tmp     = %sysfunc(getoption(source));
  %let stimer_tmp     = %sysfunc(getoption(stimer));
  %let fullstimer_tmp = %sysfunc(getoption(fullstimer));
  %let msglevel_tmp   = %sysfunc(getoption(msglevel));
  %let mautocomploc_tmp = %sysfunc(getoption(mautocomploc));

  options NOnotes NOsource ls=128 ps=MAX NOfullstimer NOstimer msglevel=N NOmautocomploc;
/*===================================================================================================*/

%if NOT(%superq(verify) in (0 1)) %then %let verify=0;
%if NOT(%superq(quiet) in (0 1))  %then %let quiet=0;

%if NOT(%superq(reportOnly) in (0 1)) %then %let reportOnly=0;
/* no verification when reporting */
%if 1=&reportOnly. %then %let verify=0;

%local HASHING_FILE_exist;
%let HASHING_FILE_exist = 0;

%if %sysfunc(exist(sashelp.vfunc, VIEW)) %then
  %do;
    data _null_;
      set sashelp.vfunc(keep=fncname);
      where fncname = "HASHING_FILE";
      call symputX('HASHING_FILE_exist', 1, "L");
      stop;
    run;
  %end;

%local reportFile datetime;
%let datetime = %sysfunc(datetime());
%let reportFile = WORK.tmpbundlefile%sysfunc(int(&datetime.), b8601dt15.)_;

%if NOT %sysevalf(%superq(ods)=,BOOLEAN) %then %do;
  data _null_; /* verify ods= value */
    %SPFinit_intrnl_forceV7DSname(ods);
    call symputX("ods",ods,"L");
  run;
%end;

data _null_;
datetime=symgetn('datetime');

length packagesList $ 32767 bundleName $ 128;
bundleName = compress(symget('bundleName'),"_.","KAD"); /* bundle name is letters, digits, and underscore, up to 128 symbols */

if bundleName NE symget('bundleName') then /* warn about illegal characters */
  do;
    put "ERROR: Bundle name contains illegal characters. Exiting";
    stop; 
  end;
bundleName=lowcase(bundleName); 
lbn = length(bundleName); /* to cover lengths < 7 & 11 */
/* if there is ".bundle.zip" extension added, remove it */
if substr(strip(reverse(bundleName)),1,min(11,lbn)) = 'piz.eldnub.' then bundleName=scan(bundleName,-3,".");
else /* if there is ".bundle" extension added, remove it */
if substr(strip(reverse(bundleName)),1,min(7,lbn))  = 'eldnub.'     then bundleName=scan(bundleName,-2,".");

put / "INFO: Bundle name is: " bundleName / ;

length packagesPath $ 32767 packagesRef $ 8;
packagesPath = dequote(symget('packagesPath'));
packagesRef = upcase(strip(symget('packagesRef')));

if 0 then set SASHELP.VEXTFL(keep=level xpath xengine fileref exists);

/* ignore target check when only report is requested */
%if 0=&reportOnly. %then
  %do;
    /* organize target path (location for packages) */
    if " "=packagesPath then
      do;
        DECLARE HASH sH(dataset:'SASHELP.VEXTFL(where=(fileref=' !! quote(packagesRef) !! '))', ordered: "A");
        sH.DefineKey("level");
        sH.DefineData("xpath","xengine","exists");
        sH.DefineDone();

        if sH.NUM_ITEMS=0 then
          do;
            put "ERROR: Fileref in packagesRef= does NOT exist. Exiting!";
            stop;
          end;

        packagesPath=" ";

        rc = sH.FIND(key:NOT(1=sH.NUM_ITEMS)); /* if only 1 element select level 0, if more than 1 select level 1 */
        if xengine = "DISK" AND exists='yes' then
          packagesPath=quote(strip(xpath)); /* add quotes to the packagesPath */
        else
          do;
            put "ERROR: Path: " xpath "in packagesRef= is invalid! Exiting!";
            stop; 
          end;
      end;
    else
      do;
        rcPckPath = fileexist(strip(packagesPath));
        if 0=rcPckPath then
          do;
            put "ERROR: Path in packagesPath= does NOT exist. Exiting!";
            stop;
          end;
        else packagesPath=quote(strip(packagesPath)); /* add quotes to the packagesPath */
      end;
  %end;

length path $ 32767 pathRef $ 8;
path = dequote(symget('path'));
pathRef = upcase(strip(symget('pathRef')));

if " "=path and " "=pathRef then
  do;
    put "ERROR: Path= and pathRef= are empty! Exiting!";
    stop;
  end;

/* verify source path (location of the bundle) */
if " "=path then
  do;
    DECLARE HASH tH(dataset:'SASHELP.VEXTFL(where=(fileref=' !! quote(pathRef) !! '))', ordered: "A");
    tH.DefineKey("level");
    tH.DefineData("xpath","xengine","exists");
    tH.DefineDone();
    DECLARE HITER tI("tH");

    do while (tI.next()=0);
      put "Checking in: " xpath;
      if fileexist(cats(xpath,"/",bundleName,'.bundle.zip')) then 
        do;
          path=strip(xpath);
          put "INFO: Bundle file " bundleName +(-1) ".bundle.zip found under: " xpath;
          leave;
        end;
    end;

    if " "=path then
      do;
        put "ERROR: Bundle: " bundleName "does NOT exist in any directory in pathRef=. Exiting!";
        stop;
      end;
  end;
else
  do;
    rcPath = fileexist(strip(path));
    if 0=rcPath then
      do;
        put "ERROR: Path in Path= does NOT exist. Exiting!";
        stop;
      end;
  end;

/* get the list of packages to unbundle from bundlecontent.sas */
length bundlecontentFR $ 8;
rc1 = filename(bundlecontentFR, cats(path,"/",bundleName,'.bundle.zip'));
rcE = fexist(bundlecontentFR); 
rc2 = filename(bundlecontentFR);

if 0=rcE then
  do;
    put "ERROR: The " bundleName "file does NOT exist!. Exiting!";
    stop;
  end;

length bundlecontentFR $ 8;
rc1 = filename(bundlecontentFR, cats(path,"/",bundleName,'.bundle.zip'), 'zip', 'member="bundlecontent.sas"');
rcE = fexist(bundlecontentFR); 
rc2 = filename(bundlecontentFR);

if 0=rcE then
  do;
    put "ERROR: The bundlecontent.sas file does NOT exist inside bundle. Exiting!";
    stop;
  end;

length bundlecontentfile $ 1024;
bundlecontentfile = cats(path,"/",bundleName,'.bundle.zip');

infile _DUMMY_ ZIP FILEVAR=bundlecontentfile member="bundlecontent.sas" end=EOF dlm=",";

DECLARE HASH Q(ordered:"A");
Q.defineKey("package");
Q.defineData("package",'pckVer','pckDtm','hash');
Q.defineDone();
DECLARE HITER IQ("Q");

/*--------------------------------------------------*/
do until(EOF);
  input package :$32. pckVer :$16. pckDtm :$24. hash :$128.;
  if " " NE package then rc = Q.ADD();
end;
label package="Package name" 
      pckVer="Version" 
      pckDtm="Generation timestamp" 
      hash="SHA256 for the Package"; 
/*--------------------------------------------------*/

if 0=Q.NUM_ITEMS then /* ... if empty then exit */
  do;
    put "WARNING: No packages to unbundle. Exiting!";
    stop;
  end;
else
  do while(iQ.next()=0);
    packagesList = catx(" ", packagesList, package);
  end;


rc = Q.output(dataset: /* create proper tag */
  %if %superq(ods) NE %then %do; "&ods.(label='Bundle: " !! strip(bundleName) !! "')" %end; 
                      %else %do; "&reportFile.1" %end;
);


/* code executed for unbundling */
length code1 code2 $ 32767;
%if 0=&reportOnly. %then
  %do;
    code1= 
    'options ps=min nofullstimer nostimer msglevel=N; filename PACKAGES ' !! strip(packagesPath) !! ';' !!
    '%relocatePackage(' !! strip(packagesList) !! ',source=' !! catx("/", path, bundleName) !! 
    '.bundle.zip, sDevice=ZIP,psMAX=MIN)'; 

    /*put code=;*/

    put / "INFO: The " bundleName "bundle extraction in progress...";

    rc = doSubL(code1);

    put / "INFO: The " bundleName "bundle extraction ended.";
  %end;


/* code executed for verification */
%if 1=&verify. %then
  %do;  
    put / "INFO: The " bundleName "bundle verification in progress...";
    code2= 
    'options ps=min nofullstimer nostimer msglevel=N; filename PACKAGES ' !! strip(packagesPath) !! ';' !!
    'filename _ ZIP ' !! quote(cats(path, "/", bundleName, ".bundle.zip")) !! ';' !! 
    '%include _(verification.sas);%listPackages()';
    rc = doSubL(code2);
    put / "INFO: The " bundleName "bundle verification ended.";
  %end;
put " ";
rc=sleep(1,1); 

rc = doSubL(
%if 0=&quiet. %then /* do not print report */
  %do;
    "title 'Summary of the bundle file: " !! strip(bundleName) !! "';" !! 
    "proc print label data=" !! 
    %if %superq(ods) NE %then %do; "&ods." %end; 
                        %else %do; "&reportFile.1" %end; !! 
    "; var package pckVer pckDtm hash; run;" !!
  %end;
%if %superq(ods) NE %then 
  %do; %put INFO: Report file: &ods.; %end; 
%else 
  %do; "proc delete data=&reportFile.1; run;" !! %end;
"title;");

stop;
run;
/*===================================================================================================*/
  /* restore options */
  options ls = &ls_tmp. ps = &ps_tmp. 
          &notes_tmp. &source_tmp. 
          &stimer_tmp. &fullstimer_tmp.
          msglevel=&msglevel_tmp. &mautocomploc_tmp.;

%ENDofunbundlePackages:
%mend unbundlePackages;

/*
options mprint;
%unbundlePackages(
 bundlenametest123
,path=R:\
,packagesPath=R:\check
,verify=1
)

%unbundlePackages(
 bundlenametest124
,path=R:\
,packagesPath=R:\check2
,verify=1
,ods=WORK.bundlenametest124_reportDS
)

%unbundlePackages(
 bundlenametest124.bundle.zip
,path=R:\
,packagesPath=R:\check3
)

%unbundlePackages(
 bundlenametest124.bundle.zip
,path=R:\
,packagesPath=R:\check4
)

%unbundlePackages()

%unbundlePackages(
 nobundlenametest123
,path=R:\
,packagesPath=R:\check
,verify=1
)
*/

/* reportOnly & quiet 

%bundlePackages(myLittleBundle
              ,path=R:\
              ,packagesList=basePlus SQLinDS macroarray
              ,packagesRef=PACKAGES)

option dlcreatedir;
libname _ "R:\check5";

%unbundlePackages(myLittleBundle         
                 ,path=R:\
                 ,reportOnly=1                 
                 ,ods=work.bundleReport1) 

%unbundlePackages(myLittleBundle         
                 ,path=R:\
                 ,reportOnly=1                 
                 ,ods=work.bundleReport2
                 ,quiet=1) 

%unbundlePackages(myLittleBundle         
                 ,path=R:\
                 ,packagesPath=R:\check5
                 ,ods=work.bundleReport3
                 ,quiet=1, verify=1) 

%unbundlePackages(myLittleBundle         
                 ,path=R:\
                 ,packagesPath=R:\check5
                 ,ods=work.bundleReport4
                 ,quiet=0, verify=1) 


*/


