I'm hoping to find a method for parsing all files that were left behind in "/usr/local/bin" when I updated Homebrew for my Silicon Mac so that I can repair broken symbolic links there. The new (active) versions appear mostly to exist in "/opt/homebrew/bin".
I don't think that the problems resulting from the broken symbolic links are solvable for every instance by using information from Shell Path Not Right.
The problem is that many of the symbolic links (also executables) in "/usr/local/bin" are still valid (presumably because not all Homebrew packages have been updated for the new version of Homebrew that is appropriate for use on Silicon Macs), but most have been left as broken symbolic links, so I don't think it would be very beneficial just to delete the entire folder.
Is there a method available which will leave valid links and executables in "/usr/local/bin" but attempt to fix the broken symbolic links there so that they either point to the same targets as those in "/opt/homebrew/bin" or are listed as orphaned symbolic links that can be deleted? I'd like to be able to perform such an action on a schedule or on demand as long as there is a need for "/usr/local/bin" to exist. If I could do that, then I think that correcting path configurations (which seems significantly more difficult to me) might be less necessary. In other words, I think it's more appropriate to repair than to implement workarounds regarding these orphaned links. I consider manipulation of the path configuration a workaround. Thanks.
I finally created a method of sorts. This AppleScript may not be the ideal answer, but I've included enough dialogs to make it fairly safe, I think. You can always comment or delete the lines that prompt you if you want to let it work unhindered.
tell application "Finder"
set this_folder to POSIX file "/usr/local/bin"
set the_items to list folder this_folder without invisibles
end tell
reLink(the_items)
on reLink(the_items)
repeat with the_item in the_items
set startF to "/usr/local/bin"
set this_filepath to (the_item as string)
set the_source_file to POSIX path of this_filepath
set JustTheName to (characters 2 thru -1 of the_source_file as text)
if (my isSymlink(startF & the_source_file)) = false then
set linkTarget to (do shell script "find /opt/homebrew/bin -name" & space & JustTheName)
if the result is "" then
display dialog "Finding a suitable target for the broken symlink" & space & "\"" & JustTheName & "\"..."
set linkTarget to (do shell script "find /opt/homebrew/cellar -name" & space & JustTheName & space & "-and -type f -print -quit")
if the result is "" then # (still no match)
display dialog "No target could be found for the broken symlink located at \"/usr/local/bin" & the_source_file & "\"" & ". How would you like to proceed?" buttons {"Stop", "Leave it and continue", "Delete it and continue"} default button 3
set the user_choice to the button returned of the result
if the user_choice is "Stop" then
error number -128
else if the user_choice is "Delete it and continue" then
do shell script "rm -r" & space & startF & the_source_file
end if
else
do shell script "rm -r" & space & startF & the_source_file & ";ln -s" & space & linkTarget & space & startF & the_source_file
end if
else
display dialog "The target of the newer symlink at" & space & "\"" & the result & "\"" & space & "will become the new target for the broken symlink at \"/usr/local/bin" & the_source_file & "\"."
do shell script "rm -r" & space & startF & the_source_file & ";ln -s" & space & linkTarget & space & startF & the_source_file
display dialog "The broken" & space & "\"" & JustTheName & "\"" & space & "broken symlink in \"/usr/local/bin\" has been repaired!" buttons {"Stop", "Continue repairing"} default button 2
end if
else
display dialog "Either" & space & "\"" & JustTheName & "\"" & space & "isn't a symlink, or it already has a valid target." --giving up after 1
end if
end repeat
end reLink
on isSymlink(posixPath)
try
do shell script "[[ -e " & quoted form of posixPath & " ]]"
return true
end try
return false
end isSymlink