#!/usr/bin/env bash
#
# Check vim plugins dir for unused plugins
# Pass -a to also clean all plugins from start/ folder.
#
# Note: does not perform deletion at this time, pipe output to bash.

set -e

pluginhome=$HOME/.vim/pack/git-plugins

cd $pluginhome/opt
find . -mindepth 1 -maxdepth 1 -type d -print0 | while read -d $'\0' pluginpath
do
  if ! grep -q "$pluginpath" $HOME/.config/nvim/init.vim ; then
    basename=$(basename $pluginpath)
    echo "rm -rf $pluginhome/opt/$basename"
  fi
done

if [ "$1" = "-a" ]; then
  echo "rm -rf $pluginhome/start/*"
fi