function sync(varargin)

if nargin == 2
   myMg = varargin{1};
   yourMg = varargin{2};
elseif nargin == 1
   myMg = varargin{1};
   yourMg = input('opponent''s monogram: ','s');
else
   myMg = input('your monogram: ','s');
   yourMy = input('opponent''s monogram: ','s');
end
myMg = [myMg '.txt'];
yourMg = [yourMg '.txt'];

% ---------------- Exchange Hello's --------------------
[saidHello, yourLastTStamp] = hellos(myMg, yourMg);
if ~saidHello
   return
end

% ---------------- Exchange Opening Board Info ---------
board = setUpMyHalf(myMg);
[board, yourLastTStamp] = setUpYourHalf(board, yourMg, yourLastTStamp);

% ---------------- Play the Game -----------------------
it = whoFirst(myMg, yourMg);
anotherTurn = true;
while anotherTurn
   it = 3 - it;
   if it == 1
      play = getMyPlay(board, myMg);
   else
      play = getYourPlay(board, yourMg);
   end
   board = makePlay(board, play, it);
   anotherTurn = morePlay(board);
end

declareWinner(it)
end

% ---------------------------------------------------
function [saidHello, yourTimeStamp] = hellos(myMg, yourMg)
% delete (if exists) and recreate file myMG with message hello
% wait for opponent to create his/her yourMG file with hello
% allows user to cancel if wait becomes long
% return with success or failure to detect hello

if exist(myMg) == 2
   delete(myMg);
end
myFid = fopen(myMg, 'w');
fprintf(myFid, 'hello');
fclose(myFid);

stale = 5;
tic;
timeout = false;
while (exist(yourMg) ~= 2 || aged(yourMg, stale)) && ~timeout
   if exist(yourMg) ~= 2
      fprintf('waiting for opponent to say hello\n')
   else
      fprintf('stale hello from opponent over %d minutes old\n', stale)
   end
   pause(5)
   if toc > 60
      timeout = input('want to time out (1 yes, 0 no)? ');
      tic
   end
end

if toc <= 60
   yourTimeStamp = getTimeStamp(yourMg);
   fprintf('hello received!\n')
   saidHello = true;
else
   yourTimeStamp = [0 0 0 0 0 0];
   fprintf('timed out\n')
   saidHello = false;
end
end

% ---------------------------------------------------------
function age = aged(f, stale)
   % checks if fid file is over 'stale' minutes old
   % Not valid if ages span a month or year or midnight
   lst = dir(f);
   date = datevec(lst.date);
   now = int32(clock);
   age = any(date(1:3) ~= now(1:3)) || now(4)*60+now(5) > ...
         date(4)*60+date(5) + stale;
end

% ---------------------------------------------------------
function meOlder = cmpAge(myMg, yourMg)
   % responds 1 if first file is older, -1 is second is, else 0
   myDir = dir(myMG);
   yourDir = dir(yourMG);
   myAge = datevec(myDir.date);
   yourAge = datevec(yourDir.date);
   diff = yourAge - myAge;
   diffPos = find(diff ~= 0, 1);
   if length(diff) > 0
      meOlder = sign(diff(diffPos));
   else
      meOlder = 0;
   end
end

% ----------------------------------------------------------
function board = setUpMyHalf(myMg)
board.critters = repmat(' ',8,8);
board.clouded = repmat(true,8,8);
board.owners = repmat('1',8,8);
board.critters(1:3,:) = input('Enter my board half (3x8 array wvz):');
board.clounded(4:5,:) = false;
board.owners(4:5,:) = 0;
board.owners(6:8,:) = 2;

myFid = fopen(myMg, 'w');
fprintf(myFid, '%s\n%s\n%s\n', board.critters(1,:), ...
               board.critters(2,:), ...
               board.critters(3,:));
fclose(myFid);
disp('set up my half')
end

% ----------------------------------------------------------
function [board, yourLastTStamp] = ...
           setUpYourHalf(board, yourMg, yourLastTStamp)
disp('set up your half')
updateTime = getTimeStamp(yourMg);
while all(updateTime == yourLastTStamp)
   fprintf('waiting for opponent to set up board\n')
   pause(5)
   updateTime = getTimeStamp(yourMg);
end
yourFid = fopen(yourMg);
row = fgetl(yourFid);
board.critters(8,:) = fliplr(row);
row = fgetl(yourFid);
board.critters(7,:) = fliplr(row);
row = fgetl(yourFid);
board.critters(6,:) = fliplr(row);
fclose(yourFid);
end

% ----------------------------------------------------------
function it = whoFirst(myMg, yourMg);
it = 1;
disp('who goes first')
end

% ----------------------------------------------------------
function play = getMyPlay(boardInfo, myMG)
play = 1;
disp('get my play')
end

% ----------------------------------------------------------
function play = getYourPlay(boardInfo, myMG)
play = 1;
disp('get your play')
end

% ----------------------------------------------------------
function boardInfo = makePlay(boardInfo, play, it)
boardInfo = 1;
disp('make the play')
end

% ----------------------------------------------------------
function anotherTurn = morePlay(boardInfo)
anotherTurn = input('another turn? ');
end

% ----------------------------------------------------------
function declareWinner(it)
disp('declare winner')
end

% ----------------------------------------------------------
function timeStamp = getTimeStamp(f)
d = dir(f);
timeStamp = datevec(d.date);
end

