; docformat = 'rst'

;+
; Compares two version numbers for the more updated number. Returns 0 for 
; equal versions, 1 if version1 is later than version2, and -1 if version1 is 
; less than version2. Strings such as 'alpha' and 'beta' may be tacked on to 
; the end of a version, but are compared alphabetically.
;     
;
; :Examples:
;    For example, 1.2 is later than 1.1.2::
;
;       IDL> print, mg_cmp_version('1.2', '1.1.2')
;              1
;
; :Returns: 
;    -1, 0, or 1
;
; :Params:    
;    version1 : in, required, type=string
;       first version number
;    version2 : in, required, type=string
;       second version number
;- 
function mg_cmp_version, version1, version2
  compile_opt strictarr
  
  v1parts = strsplit(version1, '.', /extract, count=v1len)
  v2parts = strsplit(version2, '.', /extract, count=v2len)
  
  nparts = v1len > v2len
  
  v1partsValues = lonarr(nparts)
  v2partsValues = lonarr(nparts)
  
  v1partsValues[0] = long(v1parts)
  v2partsValues[0] = long(v2parts)
  
  for i = 0L, nparts - 1L do begin
    if (v1partsValues[i] gt v2partsValues[i]) then return, 1
    if (v1partsValues[i] lt v2partsValues[i]) then return, -1
    
    if (i eq nparts - 1L) then begin
      nondigitpos1 = stregex(v1parts[i], '[^[:digit:].]')
      nondigitpos2 = stregex(v2parts[i], '[^[:digit:].]') 

      if (nondigitpos1 eq -1L && nondigitpos2 eq -1L) then return, 0
      if (nondigitpos1 eq -1L) then return, 1
      if (nondigitpos2 eq -1L) then return, -1    
      
      case 1 of
        v1parts[i] lt v2parts[i]: return, -1
        v1parts[i] gt v2parts[i]: return, 1
        else : return, 0
      endcase
    endif
  endfor
  
  return, 0
end


; main-level example

v = ['1.2', '1.1.2', '1.1', '1.1alpha', '1.1beta']
colwidth = max(strlen(v)) + 2

print, '', format='(A13, $)'
print, v, format='(5A10)'

for v1 = 0L, n_elements(v) - 1L do begin
  print, v[v1] + ' > ', format='(A13, $)'
  for v2 = 0L, n_elements(v) - 1L do begin
    print, mg_cmp_version(v[v1], v[v2]), $
           format='(I10, $)'
  endfor
  print
endfor

end