chroot_version_hooks.d: clean up ~/chromiumos/.cache/ state before linking

A few devs seem to have ~/chromiumos/.cache/common/ but nothing else.
That dir is safe to nuke, so do so automatically.

BUG=b:209485932
TEST=CQ passes

Change-Id: I63fd735d4d7906734d0ca3ac491b021811e27c5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/3344012
Reviewed-by: Cindy Lin <xcl@google.com>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/chroot_version_hooks.d/197_trunk_to_chromiumos b/chroot_version_hooks.d/197_trunk_to_chromiumos
index f1039aa..edddd0a 100644
--- a/chroot_version_hooks.d/197_trunk_to_chromiumos
+++ b/chroot_version_hooks.d/197_trunk_to_chromiumos
@@ -6,6 +6,17 @@
 for old_link in /home/*/trunk; do
   new_link="${old_link%/*}/chromiumos"
   if [[ -L "${old_link}" && ! -L "${new_link}" ]]; then
+    if [[ -f "${new_link}" ]]; then
+      rm -f "${new_link}"
+    elif [[ -d "${new_link}" ]]; then
+      # Some devs have a little .cache state for unknown reason.  Delete the
+      # few known safe paths, but don't try to delete the whole tree.
+      rm -rf "${new_link}/.cache/common"
+      rmdir "${new_link}/.cache" 2>/dev/null
+      if ! rmdir "${new_link}"; then
+        echo "ERROR: ~/chromiumos exists inside your SDK when it should not."
+      fi
+    fi
     ln -s /mnt/host/source "${new_link}"
   fi
 done