function [pop1, pop2, links_type, links_n, links_capacity, ...
	  group_id, group_members, date, ...
	  network_cost, router_cost, fibre_cost, line_card_cost] = read_links(filename, debug)
%
% file:      	read_links.m, (c) Matthew Roughan, Tue Mar 10 2009
% created: 	Tue Mar 10 2009 
% author:  	Matthew Roughan 
% email:   	matthew.roughan@adelaide.edu.au
% 
% This function reads a list of links from a file along with the Meta-information included in
% the comments. Please note that this program is how I will read the file you handup, so it
% this does not work for your data file, you will get zero.
%

if (nargin == 0)
  filename = 'elbonian_example_net.dat';
end
if (nargin < 2)
  debug = 0;
end

% read the link information
[pop1, pop2, links_type, links_n, links_capacity] = ...
    textread(filename, '%f %f %f %f %f', 'delimiter', '|', 'commentstyle', 'shell');

% read the Meta-information from the comments
fid = fopen(filename, 'r');
str = fgetl(fid);
while (ischar(str))
  [field, value] = strread(str, '%s %s', 'delimiter', '=');
  if (~isempty(strfind(char(field), 'group ID'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    group_id = char(value);
  elseif  (~isempty(strfind(char(field), 'group members'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    group_members = char(value);
  elseif  (~isempty(strfind(char(field), 'date'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    date = char(value);
  elseif  (~isempty(strfind(char(field), 'network cost'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    network_cost = strread(char(value), '%d');
  elseif  (~isempty(strfind(char(field), 'router cost'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    router_cost = strread(char(value), '%d');
  elseif  (~isempty(strfind(char(field), 'fibre cost'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    fibre_cost = strread(char(value), '%d');
  elseif  (~isempty(strfind(char(field), 'line card cost'))) 
    if (debug > 0)
      fprintf('found %s -> %s\n', char(field), char(value));
    end
    line_card_cost = strread(char(value), '%d');
  end
  str = fgetl(fid);
end
fclose(fid);
 
% check that all meta-fields have been entered
if (~exist('group_id', 'var'))
  error('could not find group ID');
end
if (~exist('group_members', 'var'))
  error('could not find group members');
end
if (~exist('date', 'var'))
  error('could not find date');
end
if (~exist('network_cost', 'var'))
  error('could not find network cost');
end
if (~exist('router_cost', 'var'))
  error('could not find router cost');
end
if (~exist('fibre_cost', 'var'))
  error('could not find fibre cost');
end
if (~exist('line_card_cost', 'var'))
  error('could not find line card cost');
end
