#!/bin/bash

# omarchy:summary=Retire a mise wrapper an older Omarchy release left in ~/.local/bin
# omarchy:args=<package> [command-name [bin-name]]
# omarchy:hidden=true

# This once wrote a ~/.local/bin wrapper that installed a tool through mise the
# first time it ran. Omarchy installs nothing that way anymore, but migrations
# from then still call this to create or rewrite such wrappers. So it removes
# the wrapper it would have written, and writes nothing.

if [[ -z $1 ]]; then
  echo "Usage: omarchy-mise-install <package> [command-name [bin-name]]"
  exit 1
fi

package=$1
command=${2:-$1}
bin=${3:-$command}

# The command name is a file name under ~/.local/bin, so a slash, a leading dot
# or a leading dash would reach somewhere else.
case "$command" in
  */* | .* | -* | *[[:cntrl:]]*)
    echo "omarchy-mise-install: '$command' is not usable as a command name" >&2
    exit 1
    ;;
esac

# Only an exact copy of the wrapper goes. Anything else at the path, including
# one the user changed, is theirs.
wrapper="$HOME/.local/bin/$command"
if [[ -f $wrapper && ! -L $wrapper ]] && cmp -s "$wrapper" <(
  printf '#!/bin/bash\nexport MISE_MINIMUM_RELEASE_AGE=0\nmise use -g --quiet "%s" || exit 1\nexec mise x "%s" -- "%s" "$@"\n' "$package" "$package" "$bin"
); then
  rm -f "$wrapper"
fi
