#!/bin/sh
# Copyright (c) 2026 Jiaxing Hu <gahing@gahingwoo.com>
# SPDX-License-Identifier: GPL-2.0
# `charsiu list`. Not meant to be run directly; charsiu dispatches to it.
#
# Columns, in the order ollama uses, so the output is legible to anyone who
# has used that: NAME, SIZE, then what is particular to us. The last column is
# the one that matters here and no other runner has it: whether the file can
# actually run, read out of the file rather than off its name.
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; }

CUR=$(current_model)
CK=$(find_bin charsiu_check)

printf '%-44s %8s  %-9s %s\n' NAME SIZE RUNS WHERE
all_models | while IFS= read -r f; do
	mb=$(( $(file_bytes "$f") / 1048576 ))
	runs="?"
	[ -n "$CK" ] && { "$CK" -q "$f" >/dev/null 2>&1 && runs="yes" || runs="NO"; }
	mark=""; [ "$f" = "$CUR" ] && mark=" *"
	printf '%-44s %7sM  %-9s %s%s\n' "$(basename "$f")" "$mb" "$runs" "$(dirname "$f")" "$mark"
done
[ -n "$(all_models | head -1)" ] || printf '\n  nothing yet. `charsiu pull` fetches one.\n'
exit 0
