function writeJob(uid, class, binary, programOptions, sleepMinutes, maxMine)

%%% Input validation
if ~ischar(uid)
    error('UID must be string');
end
if ~ischar(class)
    error('class must be string');
end
if ~ischar(binary)
    error('binary must be string');
end
if ~isstruct(programOptions)
    error('programOptions must be a struct array');
end
if numel(programOptions) ~= length(programOptions)
    error('programOptions must be linear');
end
if ~all(isfield(programOptions,{'key' 'value'}))
    error('programOptions must contain fields key and value');
end

if nargin < 5, sleepMinutes = 9; end
sleepMinutes = round(sleepMinutes);
if nargin < 6, maxMine = 2; end
maxMine = round(maxMine);

%%%variables
nodes = [];
for i = 1:length(programOptions)
    if strcmp(programOptions(i).key, 'nodes')
        nodes = programOptions(i).value;
        programOptions(i) = [];
        break;
    end
end
if ~ischar(nodes)
    error('"nodes" not defined in options');
end
mydate = datestr(now,'mmdd');
mycwd = [ mydate '/' uid ];
dirname = [ mydate '/jobs' ];
filename = [ mydate '.' uid '.job'];
filepath = fullfile(dirname, filename);
scriptpath = fullfile(dirname, 'submit.sh');
starPrefix = [ '/home/vandef1/loadleveler/' mydate '/jobs/' ];

%%%add UID to options
programOptions = [ struct('key','uid','value',uid) programOptions ];

%%%prepare directory structure
[mkdirsuccess,dummy,dummy] = mkdir(mydate); %#ok<NASGU>
if ~mkdirsuccess
    error(['Cannot create directory :: ' mydate]);
end
[mkdirsuccess,dummy,dummy] = mkdir( mycwd ); %#ok<NASGU>
if ~mkdirsuccess
    error(['Cannot create directory :: ' mycwd]);
end
[mkdirsuccess,dummy,dummy] = mkdir(dirname); %#ok<NASGU>
if ~mkdirsuccess
    error(['Cannot create directory :: ' dirname]);
end
%%%open file
fid = fopen(filepath, 'wb');
%%%open script file
fsh = fopen(scriptpath, 'ab');
if (ftell(fsh) == 0)
  fprintf(fsh, '#!/bin/bash\n\n# ==== submit script for %s ====\n\n', mydate);
end
%%%write to script file
fprintf(fsh, 'if ssh vandef1@star.felk.cvut.cz ls -l %s%s\n', starPrefix, filename);
fprintf(fsh, ' then\n');
fprintf(fsh, '  while [ `ssh vandef1@star.felk.cvut.cz llq -u vandef1 | grep node | wc -l` -gt %g ]\n', maxMine);
fprintf(fsh, '   do\n');
fprintf(fsh, '    sleep %gm\n', sleepMinutes);
fprintf(fsh, '  done\n');
fprintf(fsh, '  ssh vandef1@star.felk.cvut.cz llsubmit %s%s\n', starPrefix, filename);
fprintf(fsh, '  sleep %gm\n', sleepMinutes);
fprintf(fsh, 'fi\n\n');
%%%close script file
fclose(fsh);

%%%preamble & class
fprintf(fid, '#!/bin/bash\n# ==== Which queue? ====\n# | fast     :: 8  PE''s, 2 nodes, 1 minute\n# | short    :: 16 PE''s, 4 nodes, 20 minutes\n# | standard :: 8  PE''s, 2 nodes, 30 minutes\n# | long     :: 24 PE''s, 6 nodes, 40 minutes\n\n');
fprintf(fid, '# @ class = %s\n\n\n', class);

%%%nodes
fprintf(fid, '# ==== How many nodes? ====\n');
fprintf(fid, '# @ node = %s\n', nodes);
fprintf(fid, '# @ tasks_per_node = 4\n\n\n');

%%%UID
fprintf(fid, '# === What''s it called? ====\n');
fprintf(fid, '# @ job_name = %s\n\n\n', uid);

%%%output & misc
fprintf(fid, '# ==== Where to place output? ===\n');
fprintf(fid, '# @ initialdir = /home/vandef1/loadleveler/%s\n', mycwd);
fprintf(fid, '# @ error    = psg.$(job_name).$(Host).$(Cluster).$(Process).err\n# @ output   = psg.$(job_name).$(Host).$(Cluster).$(Process).out\n\n');
fprintf(fid, '# @ job_type = parallel\n# @ node_usage = not_shared\n\n\n');

%%%executable and parameters
strBinary = sprintf('/home/vandef1/work/binaries/%s', binary);
strParams = '';
for opt = programOptions
    if ~ischar(opt.key)
        warning('option key must be string');
    end
    if ~ischar(opt.value)
        warning('option value must be string');
    end
    strParams = sprintf('%s --%s %s', strParams, opt.key, opt.value);
end

fprintf(fid, '# ==== What to run? ====\n');
fprintf(fid, '# @ executable = /home/openmpi-1.2.5/bin/mpirun\n');
fprintf(fid, '# @ arguments = --mca mpi_paffinity_alone 1 --mca btl openib,self %s%s\n', strBinary, strParams);
fprintf(fid, '\n\n');

%%%misc & epilogue
fprintf(fid, '# ==== misc. U Can''t Touch This ====\n');
fprintf(fid, '# @ queue\n\n');
fprintf(fid, '# --mca mpi_paffinity_alone 1\n\n');
fprintf(fid, '# --mca btl tcp,self\n');
fprintf(fid, '# --mca btl openib,self\n\n');

%%%close file
fclose(fid);

disp(filename);

end
