function skew_labelfile(Name,Indir,Outdir,Offset,Slope)
% skew_labelfile(Name,Srcdir,Dstdir,Offset,Slope)
%     Load the Harte-format label file from Indir/Name.lab, 
%     (i.e. lines of <start_sec> <end_sec> <label>)
%     then apply time mapping T_out = Offset + Slope*T_in; 
%     rewrite new label file to Outdir/Name.lab.
%     Passing Name as a cell array, and Offset and Slope as
%     vectors, writes a whole series of output files
% 2010-04-27 Dan Ellis dpwe@ee.columbia.edu

if iscell(Name)
  % Recurse if passed array
  nN = length(Name);
  for i = 1:nN
    skew_labelfile(Name{i}, Indir, Outdir, Offset(i), Slope(i));
  end
  return
end

if length(Name) == 0
  % silently do nothing with an empty name
  return
end


infilename = fullfile(Indir,[Name,'.lab']);

if exist(infilename,'file') == 0
  if exist(Indir)==0
    error(['Input label directory ',Indir,' does not exist']);
  else
    error(['Input label file ',infilename,' does not exist']);
  end
end

[tstt,tend,lab] = textread(infilename,'%f %f %s');

tstto = max(0,Offset + Slope*tstt);
tendo = Offset + Slope*tend;

outfilename = fullfile(Outdir,[Name,'.lab']);
mymkdir(fileparts(outfilename));
fp = fopen(outfilename,'w');
if fp == 0
  error(['Cannot create ', outfilename]);
end

for i = 1:length(tstto)
  fprintf(fp, '%f %f %s\n',tstto(i),tendo(i),lab{i});
end
fclose(fp);
disp(['wrote ',outfilename]);

