function subj = datainfo_subject(subject, workshoppath)

% The code, raw data and derived data should be organized as follows:
%
% practicalmeeg2025/
% ├── README
% ├── code
% │   ├── README.md
% │   ├── atlas_subparc374_8k.mat
% │   ├── datainfo_subject.m
% │   └── ...
% └── ds000117-pruned
% │   ├── CHANGES
% │   ├── README
% │   ├── dataset_description.json
% │   ├── derivatives
% │   ├── participants.tsv
% │   ├── stimuli
% │   ├── sub-01
% │   └── ...
% └── derivatives
%     ├── anatomy
%     ├── groupanalysis
%     ├── raw2erp
%     ├── sensoranalysis
%     └── sourceanalysis

if nargin<1 || isempty(subject)
  % this is the default subject
  subject = 1;
end

subj_name = sprintf('sub-%02d', subject);

%% specify the root location of all files
if nargin<2 || isempty(workshoppath)
  f = mfilename('fullpath');
  codepath = fileparts(f);
  workshoppath = fileparts(codepath);
end

%% specify the location of the input and output files

if isfolder(fullfile(workshoppath, 'ds000117-pruned/ds000117-pruned'))
  rawdata = 'ds000117-pruned/ds000117-pruned';
elseif isfolder(fullfile(workshoppath, 'ds000117-pruned'))
  rawdata = 'ds000117-pruned';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned/ds000117_pruned'))
  rawdata = 'ds000117_pruned/ds000117_pruned';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned'))
  rawdata = 'ds000117_pruned';

elseif isfolder(fullfile(workshoppath, 'ds000117-pruned_sub-15/ds000117-pruned_sub-15'))
  rawdata = 'ds000117-pruned_sub-15/ds000117-pruned_sub-15';
elseif isfolder(fullfile(workshoppath, 'ds000117-pruned_sub-15'))
  rawdata = 'ds000117-pruned_sub-15';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned_sub-15/ds000117_pruned_sub-15'))
  rawdata = 'ds000117_pruned_sub-15/ds000117_pruned_sub-15';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned_sub-15'))
  rawdata = 'ds000117_pruned_sub-15';

elseif isfolder(fullfile(workshoppath, 'ds000117-pruned_sub-01/ds000117-pruned_sub-01'))
  rawdata = 'ds000117-pruned_sub-01/ds000117-pruned_sub-01';
elseif isfolder(fullfile(workshoppath, 'ds000117-pruned_sub-01'))
  rawdata = 'ds000117-pruned_sub-01';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned_sub-01/ds000117_pruned_sub-01'))
  rawdata = 'ds000117_pruned_sub-01/ds000117_pruned_sub-01';
elseif isfolder(fullfile(workshoppath, 'ds000117_pruned_sub-01'))
  rawdata = 'ds000117_pruned_sub-01';

else
  error('cannot find the raw data, please organize your files as specified on https://www.fieldtriptoolbox.org/workshop/practicalmeeg2025/#download-and-organize-the-scripts-and-data')
end

% with the data organized according to BIDS, the sss files are in the derivatives folder.
megpath    = fullfile(workshoppath, rawdata, 'derivatives', 'meg_derivatives', subj_name, 'ses-meg', 'meg');
mripath    = fullfile(workshoppath, rawdata, subj_name, 'ses-mri', 'anat');
eventspath = fullfile(workshoppath, rawdata, subj_name, 'ses-meg', 'meg');

outputpath = fullfile(workshoppath, 'derivatives');
if ~exist(fullfile(outputpath), 'dir')
  mkdir(fullfile(outputpath));
end

subdirs = {'raw2erp' 'sensoranalysis' 'anatomy' 'sourceanalysis' 'groupanalysis'};
for m = 1:numel(subdirs)
  if ~exist(fullfile(outputpath, subdirs{m}), 'dir')
    mkdir(fullfile(outputpath, subdirs{m}));
  end
  if ~exist(fullfile(outputpath, subdirs{m}, subj_name), 'dir')
    mkdir(fullfile(outputpath, subdirs{m}, subj_name));
  end

end

%% specify the names of the MEG datasets
megfile = cell(6,1);
eventsfile = cell(6,1);
for run_nr = 1:6
  megfile{run_nr}    = fullfile(megpath,    sprintf('%s_ses-meg_task-facerecognition_run-%02d_proc-sss_meg.fif', subj_name, run_nr));
  eventsfile{run_nr} = fullfile(eventspath, sprintf('%s_ses-meg_task-facerecognition_run-%02d_events.tsv', subj_name, run_nr));
end

%% specify the name of the anatomical MRI -> check whether this works on windows
mrifile = fullfile(mripath, sprintf('%s_ses-mri_acq-mprage_T1w.nii.gz', subj_name));
fidfile = strrep(mrifile, 'nii.gz', 'json');

subj = struct('id',subject,'name',subj_name,'mrifile',mrifile,'fidfile',fidfile,'outputpath',outputpath);
subj.megfile    = megfile;
subj.eventsfile = eventsfile;

%% other subject-specific information could also go here, especially if
% it follows from a manual assesment or analysis. Examples are
%  - bad channels
%  - bad data segments
%  - deviations from trigger codes
%  - anatomical information for coregistration
