subroutine ensure_registry_exists_in_dir(config_dir)
character(len=*), intent(in) :: config_dir
character(len=512) :: registry_path
logical :: registry_exists, success
integer :: unit
! Get registry path in custom config directory
registry_path = trim(config_dir)//'/registry.toml'
! Ensure config directory exists
call ensure_config_dir(config_dir, success)
if (.not. success) then
print *, 'Error: Cannot create config directory: ', trim(config_dir)
return
end if
! Check if registry already exists
inquire (file=registry_path, exist=registry_exists)
if (registry_exists) return
! Create default registry
open (newunit=unit, file=registry_path, status='replace')
write (unit, '(a)') '# Fortran Package Registry'
write(unit, '(a)') '# Maps module names to their packages for automatic dependency resolution'
write (unit, '(a)') '#'
write (unit, '(a)') '# Module resolution rules:'
write (unit, '(a)') '# 1. Check explicit modules list'
write (unit, '(a)') '# 2. Check if module starts with a custom prefix'
write(unit, '(a)') '# 3. Use default behavior: package name = module name before first underscore'
write (unit, '(a)') '# 4. If no underscore, package name = module name itself'
write (unit, '(a)') ''
write (unit, '(a)') '[packages]'
write (unit, '(a)') ''
write (unit, '(a)') '[packages.fortplot]'
write (unit, '(a)') 'git = "https://github.com/krystophny/fortplot"'
write(unit, '(a)') 'prefix = "fortplot" # Any module starting with "fortplot" belongs to this package'
write (unit, '(a)') ''
write (unit, '(a)') '[packages.pyplot-fortran]'
write (unit, '(a)') 'git = "https://github.com/jacobwilliams/pyplot-fortran"'
write (unit, '(a)') '# No prefix specified, will use default behavior:'
write (unit, '(a)') '# pyplot_module -> package name = "pyplot" (before underscore)'
close (unit)
print *, 'Created default registry at: ', trim(registry_path)
end subroutine ensure_registry_exists_in_dir