function check = checksum ( vin )

%*****************************************************************************80
%
%% checksum() computes the checksum of a VIN.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    09 September 2022
%
%  Input:
%
%    char vin[17]: a VIN.  
%    This is a MATLAB character array.
%
%  Output:
%
%    character check: the VIN checksum.
%
  if ( length ( vin ) ~= 17 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'checksum(): Fatal error!\n' );
    fprintf ( 1, '  Input VIN must be exactly 17 characters long.\n' );
    fprintf ( 1, '  VIN = "%s"\n', vin );
    error ( 'checksum(): Fatal error!\n' );
  end

  w = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ];

  t = 0;
  for i = 1 : 17
    s = ch_to_num ( vin(i) );
    t = t + s * w(i);
  end
  t = mod ( t, 11 );

  if ( t == 10 )
    check = 'X';
  else
    check = char ( '0' + t );
  end

  return
end

