#!/bin/sh -e
#
# # DASHT-DOCSETS-REMOVE 1        2020-05-16                            2.4.0
#
# ## NAME
#
# dasht-docsets-remove - removes installed [Dash] docsets
#
# ## SYNOPSIS
#
# `dasht-docsets-remove` [*OPTION*...] [*NAME*...]
#
# ### Examples
#
# `dasht-docsets-remove`
#   Removes all installed [Dash] docsets.
#
# `dasht-docsets-remove` sh
#   Removes installed [Dash] docsets whose names contain "sh".
#
# `dasht-docsets-remove` sh 'c$'
#   Removes installed [Dash] docsets whose names contain "sh" or end in "c".
#
# ## DESCRIPTION
#
# Removes installed [Dash] docsets whose names match the *NAME* regex(7)
# patterns.  If no *NAME*s are given, all installed docsets are matched.
# Unless forced, this operation prompts you to confirm it for every match.
#
# ## OPTIONS
#
# `-f`, `--force`
#   Forces the operation by overriding the interactive confirmation prompt.
#
# ## ENVIRONMENT
#
# `DASHT_DOCSETS_DIR`
#   Defines the filesystem location where your [Dash] docsets are installed.
#   If undefined, its value is assumed to be `$XDG_DATA_HOME/dasht/docsets/`
#   or, if `XDG_DATA_HOME` is undefined, `$HOME/.local/share/dasht/docsets/`.
#
# ## SEE ALSO
#
# dasht-docsets-install(1), dasht-docsets-update(1), dasht-docsets(1), [Dash]
#
# [Dash]: https://kapeli.com/dash
#
# ## AUTHOR
#
# Written in 2016 by Suraj N. Kurapati <https://github.com/sunaku/dasht>
# Distributed under the terms of the ISC license (refer to README file).

: ${DASHT_DOCSETS_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/dasht/docsets}

test "$1" = '-f' -o "$1" = '--force' && force=1 && shift || unset force

set -- $(dasht-docsets "$@")
printf "Removable docsets ($#):\n\n\t%s\n\n" "$(echo "$*" | sed 's/ /,&/g')"

trap exit INT # let Control-C abort `xargs -p` when running under bash(1)
for docset; do
  test -n "$force" || # don't ask for confirmation when it's being forced
  echo "Remove $docset docset [y/N]" | xargs -p | grep -q . || continue

  rootname="$DASHT_DOCSETS_DIR/$docset"
  rm -v -f -r "$rootname".docset \
              "$rootname".tgz
done