#!/bin/sh
#
# TazHelper - Guided testing of SliTaz cooking ISOs.
#
# Runs automatic checks, walks the tester through a short checklist and
# sends an anonymous report to the SliTaz team. All the logic lives in
# libtazhelper.sh: this script is the CLI, and the backend the GTK
# client (gui/tazhelper-gtk) runs for every action.
#
# Copyright (C) 2026 SliTaz GNU/Linux - BSD License
#

# Run from a source checkout: use its lib, tests and config.
here="$(cd "$(dirname "$0")" && pwd)"
if [ -f "$here/lib/libtazhelper.sh" ]; then
	TAZHELPER_LIB="${TAZHELPER_LIB:-$here/lib/libtazhelper.sh}"
	TESTS_DIR="${TESTS_DIR:-$here/tests}"
	[ -f /etc/slitaz/tazhelper.conf ] ||
		TAZHELPER_CONF="${TAZHELPER_CONF:-$here/etc/tazhelper.conf}"
fi
SELF="$here/$(basename "$0")"
. "${TAZHELPER_LIB:-/usr/lib/tazhelper/libtazhelper.sh}" || exit 1

# The GTK client runs this script again: keep the same paths.
export TAZHELPER_LIB TESTS_DIR TAZHELPER_CONF TAZHELPER_STATE="$STATE"

usage() {
	cat << EOT
Usage: tazhelper [command] [args]

Guided testing of SliTaz cooking ISOs: automatic checks, a short
checklist and an anonymous report sent to the SliTaz team.

Commands:
  (none)        Open the GUI, or the interactive CLI without X
  cli           Interactive CLI: auto tests, checklist, report, send
  auto          Run the automatic tests and print the results
  list          List the available tests
  tests         List the tests for the GUI: id, type, title, desc (TAB)
  results       Print the results stored in this session
  mark          Store a manual result: mark <id> <ok|partial|fail|none> [text]
  comment       Set the general comment of the report
  report        Print the report exactly as it will be sent
  send          Send the report, or a saved report: send [file]
  save          Save the report to a file: save [file]
  reset         Forget all results of this session
  launch        Start the application of a manual test: launch <id>

Examples:
  tazhelper auto
  tazhelper mark browser partial "Fonts are blurry"
  tazhelper report

About:
  Version $TAZHELPER_VERSION. Server: ${URL:-none}
  Results: $STATE
EOT
}

#
# Helpers shared by the CLI and the GUI
#

manual_ids() {
	list_tests | awk '$3 == "manual" { print $2 }'
}

launch() {
	file="$(test_file "$1")"
	cmd="$(test_var "$file" LAUNCH)"
	[ -n "$cmd" ] || { echo "No application for: $1"; return 1; }
	sh -c "$cmd" > /dev/null 2>&1 &
}

#
# CLI
#

ask() {
	printf '%s' "$1"
	read -r answer || answer=""
}

cli_checklist() {
	for id in $(manual_ids); do
		file="$(test_file $id)"
		echo
		echo "* $(test_var "$file" TITLE)"
		echo "  $(test_var "$file" DESC)"
		echo "  Command: $(test_var "$file" LAUNCH)"
		while true; do
			ask "  [l]aunch, [o]k, [p]artial, [f]ailed, Enter to skip: "
			case "$answer" in
				l) launch $id ;;
				o) status="ok"; break ;;
				p) status="partial"; break ;;
				f) status="fail"; break ;;
				*) status=""; break ;;
			esac
		done
		[ -n "$status" ] || continue
		ask "  Comment (optional): "
		set_result "$id" "$status" "$answer"
	done
}

cli() {
	echo "TazHelper $TAZHELPER_VERSION - automatic tests"
	echo
	run_auto | cli_results
	echo
	echo "Manual checklist"
	cli_checklist
	echo
	echo "Report, exactly as it will be sent:"
	echo
	report
	echo
	ask "General comment (optional): "
	[ -n "$answer" ] && set_comment "$answer"
	ask "Send the report to $URL? [y/N] "
	if [ "$answer" = "y" ]; then
		send_report && { echo "Report sent, thank you!"; return 0; }
		echo "Sending failed."
	fi
	ask "Save the report to a file? [Y/n] "
	[ "$answer" = "n" ] || echo "Saved: $(save_report)"
}

cli_results() {
	while read status id info; do
		printf '%-8s %-10s %s\n' "$status" "$id" "$info"
	done
}

#
# Commands
#

case "$1" in
	"")
		# The GTK client drives this script: tell it where we are.
		gtk="$here/gui/tazhelper-gtk"
		[ -x "$gtk" ] || gtk="$(which tazhelper-gtk 2>/dev/null)"
		if [ -n "$DISPLAY" ] && [ -n "$gtk" ]; then
			TAZHELPER="$SELF" exec "$gtk"
		fi
		cli ;;
	cli) cli ;;
	auto) run_auto | cli_results ;;
	list)
		list_tests | while read prio id type file; do
			printf '%-3s %-10s %-7s %s\n' "$prio" "$id" "$type" \
				"$(test_var "$file" TITLE)"
		done ;;
	tests)
		# For the GUI: id, type, title, desc (TAB separated).
		tab="$(printf '\t')"
		list_tests | while read prio id type file; do
			echo "$id$tab$type$tab$(test_var "$file" TITLE | tr '\t' ' ')$tab$(test_var "$file" DESC | tr '\t' ' ')"
		done ;;
	results) results ;;
	mark) shift; set_result "$@" ;;
	comment) shift; set_comment "$*" ;;
	report) report ;;
	send)
		send_report "$2" || {
			echo "Sending failed: save the report with 'tazhelper save'."
			exit 1
		} ;;
	save) save_report "$2" ;;
	reset) reset_results ;;
	launch) launch "$2" ;;
	*) usage ;;
esac
