#/bin/bash

# java-info - spits out reminders about the zillion different locations
#             where Java-relevant things are stashed on macOS.

dump_java_info() {
  dir=$1
  test -d "$dir/Contents/Home" && dir="$dir/Contents/Home"
  echo
  stat -f "%Sm%t%N" "$dir"
  (set -x && "$dir/bin/java" -version)
  du -hs "$dir"
}

echo '=== Which versions of Java are installed? ==='
brew_prefix=$(brew --prefix 2>/dev/null || echo "HOMEBREW-NOT-INSTALLED")

jdks=$(find \
  /System/Library/Java/JavaVirtualMachines \
  /Library/Java/JavaVirtualMachines \
  "$brew_prefix"/Cellar/openjdk \
  -mindepth 1 -maxdepth 1 -type d 2> /dev/null)
for jdk in $jdks
do
  dump_java_info "$jdk"
done

echo
echo '=== Where are the shared libraries? ==='
for jdk in $jdks
do
  echo
  (set -x && find "$jdk" -name '*libjvm*' && find "$jdk" -name '*libjli*')
done

echo
echo '=== Which version of Java are my browsers using? ==='
echo
browser_path='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin'
if [ -d "$browser_path" ]
then
  (set -x && "$browser_path/Contents/Home/bin/java" -version)
  du -hs "$browser_path"
else
  echo "No Java browser plugin found."
fi

echo
echo '=== Where is the Java framework stuff? ==='
echo
(set -x && ls -l /System/Library/Frameworks/JavaVM.framework/Versions/)

echo
echo '=== Which java executables are in my path? ==='
echo
(set -x && which java javac)
(which_java=$(which java) && set -x && ls -l $which_java*)

echo
echo '=== Tell me more about my Javas! ==='
echo
(set -x && /usr/libexec/java_home -V)