#!/bin/sh
# Copyright (c) 2026 Jiaxing Hu <gahing@gahingwoo.com>
# SPDX-License-Identifier: GPL-2.0
# `charsiu rm NAME...`
#
# It will not delete the model the config is pointing at without being told
# twice. Removing the thing the next `charsiu` was going to run, silently, is
# not a convenience.
set -eu
CHARSIU_HERE=$(dirname "$0")
for _l in ${CHARSIU_LIB:+"$CHARSIU_LIB/charsiu-lib.sh"} \
	  "$CHARSIU_HERE/charsiu-lib.sh" \
	  "$CHARSIU_HERE/../scripts/charsiu-lib.sh" \
	  "$CHARSIU_HERE/../../opt/charsiu/charsiu-lib.sh" \
	  /opt/charsiu/charsiu-lib.sh; do
	[ -r "$_l" ] && { . "$_l"; break; }
done
command -v ini_get >/dev/null 2>&1 \
	|| { echo "$(basename "$0"): charsiu-lib.sh not found" >&2; exit 1; }

FORCE=0
[ "${1:-}" = "-f" ] && { FORCE=1; shift; }
[ $# -ge 1 ] || { echo "charsiu rm: which model? (charsiu list)" >&2; exit 2; }
CUR=$(current_model)
rc=0
for a in "$@"; do
	t=$(resolve_model "$a")
	[ -n "$t" ] || { echo "charsiu rm: no such model: $a" >&2; rc=1; continue; }
	if [ "$t" = "$CUR" ] && [ "$FORCE" = 0 ]; then
		echo "charsiu rm: $a is the model in use. -f to remove it anyway." >&2
		rc=1; continue
	fi
	rm -f "$t" && echo "removed $t"
done
exit $rc
