!******************************************************************************
! Utility macro that checks for duplicate city name/position records in
! input file (defaults to cities.prm) which should be a type 5010 file with
! POS and NAME sub-records.
!
! This will also print INFO message for cities found with same name but at 
! different location (i.e. San Jose, CA and San Jose, Costa Rica)
!
! @bug 1699 CITIES file has duplicate entries
! @author Neon Ngo
!******************************************************************************
startmacro FN:input[nxm.sys.dat.cities.prm]

  call setDatDir
  invoke input input.toString()
  invoke input input.replace("nxm.sys.dat.cities.prm",^{datDir}.cities.prm)

  ! get city "NAME" sub-record information
  status input SR(NAME)=subRecInfo
  set format subRecInfo.format
  set offset subRecInfo.offset
  
  new java.util.HashSet CityPositionSet
  new java.util.HashSet CityNameSet
  res L:rownum   0
  res L:dupcount 0

  foreach record infile input
    res acity record.atomAt(offset,format)
    invoke city acity.value.trim()

    invoke added CityPositionSet.add(record)
    invoke added2 CityNameSet.add(city)
    if added isFalse then
      warn "duplicate city (^{city}) at row ^{rownum}"
      calc L:dupcount dupcount 1 +
    elseif added2 isFalse then
      info "same city name/different position (^{city}) at row ^{rownum}"
    endif
    calc L:rownum rownum 1 +
  endfor

  if dupcount gt 0 then
    warn "Found ^dupcount duplicate cities in ^{input}."
  endif
endmacro

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
