function prep_data_dir(varargin)
% Prepare interface functions for the catalogs in the data directory
% Package: VO.search
% Description: Prepare interface functions for the catalogs in the data directory
% Input  : * Arbitrary number of pairs if ...,keyword,value,...
%            The possible keywords are possible:
%            'Dir' - Directory in which the data catalogs resides.
%                    The directory tree should be stored like a package.
%                    each directory start with "+".
%                    Default is '~/matlab/data/+datacats/';
%            'Exten' - Cell array of file extensions to map.
%                    Default is {'mat'}.
% Output : null
% License: GNU general public license version 3
%     By : Eran O. Ofek                    Feb 2017
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: VO.search.prep_data_dir
% Reliable: 2
%--------------------------------------------------------------------------

DefV.Dir             = '~/matlab/data/+cats/';
DefV.Exten           = {'mat','fits'};
InPar = InArg.populate_keyval(DefV,varargin,mfilename);


Next  = numel(InPar.Exten);

PWD = pwd;

for Iext=1:1:Next
    cd(InPar.Dir);
    List = Util.IO.rdir(sprintf('*.%s',InPar.Exten{Iext}));

    for If=1:1:numel(List)
        cd(List(If).folder);

        %--- Generate the interface file ---
        ProgName = regexprep(List(If).name,InPar.Exten{Iext},'m');
        FunName  = regexprep(ProgName,'.m','');
        % print status to screen
        fprintf('Generating function %s\n',FunName);
        
        switch lower(InPar.Exten{Iext})
            case 'mat'
                % create and write function
                FID=fopen(ProgName,'w');
                fprintf(FID,'function varargout=%s(varargin)\n',FunName);
                fprintf(FID,'%% Interface function for catalog: %s\n',FunName);
                fprintf(FID,'%% Auto generated by %s\n',mfilename);
                fprintf(FID,'\n');
                fprintf(FID,'Nout = nargout;\n');
                fprintf(FID,'[varargout{1:Nout}]=VO.search.catalog_interface(''%s'',''%s'',varargin{:});\n',List(If).name,List(If).folder);
                fclose(FID);
            case 'fits'
                % create and write FITS read function
                FID=fopen(ProgName,'w');
                fprintf(FID,'function varargout=%s(varargin)\n',FunName);
                fprintf(FID,'%% Interface function for FITS file: %s\n',FunName);
                fprintf(FID,'%% Auto generated by %s\n',mfilename);
                fprintf(FID,'\n');
                fprintf(FID,'Nout = nargout;\n');
                fprintf(FID,'[varargout{1:Nout}]=FITS.fitsread(''%s%s%s'',varargin{:});\n',List(If).folder,filesep,List(If).name);
               
                fclose(FID);
                
            otherwise
                error('Unknown Extension option');
        end
        
    end
end
cd(PWD);