#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.7"
CMDSTR="$CMD v$CMDVER (2026-01-23)"

set -e -u

export PATH="$PATH:/usr/sbin"
. /usr/sbin/kusa-functions.sh


usage()
{
	DESC=${DESC:-$(kusa-conf machine.desc 2>/dev/null)}
	DESC=${DESC:-none}

	echo "
== $CMDSTR == searches machine icon / splash image from kusa class ==

usage: $CMD [options] ...

options:
  --search		searches image and returns full path if any (default
  			is print image name only)
  --splash		works on boot splash images instead of machine icon
  --class class		overrides machine class (now: $CLASS)
  --desc desc		overrides machine description
  			(now: $DESC)
  --res resname		override image resolution (now: $res); valid resnames
 			are low or hi, see directories /usr/share/images/ku-*

other options:
  -v|--verbose		be verbose
  -q|--quiet		be quiet
  -h|--help		show help
  -D[n]|--debug[=n]	set debug, optionally with level
  --			stop processing options
" >&2
	[ $# != 0 ] && echo -e "\n$@\n" >&2
	exit 127
}


cleanup()
{
	:
}

show_help()
{
	usage
}






# (MAIN)

trap 'echo -e "\n*INTR*\n"; exit 255' 1 2 3
trap 'echo -e "\nunexpected error $? at $LINENO\n"' ERR
trap 'cleanup' EXIT
set -e
set -u

VERBOSE=${VERBOSE:-true}
F_DEBUG=${F_DEBUG:-false}
DBGLEVEL=${DBGLEVEL:-0}
DESC=
do_search=false
search_splash=false
res="low"
imgext="png"

CLASS=$(kusa-conf class)
CLASS=$(echo "$CLASS" | sed -e 's/ .*//')
KUSA_MODEL=$(jtconf model) || exit $?

while [ $# != 0 ]
do
  case $1 in
    --search)	do_search=true ;;
    --splash)	search_splash=true ;;

    --class)	shift
    		[ $# = 0 ] && usage "option --class needs an argument"
    		[ "X$1" = "X" ] && usage "option --class needs an argument"
		CLASS=$1
		;;
    --desc)	shift
    		[ $# = 0 ] && usage "option --desc needs an argument"
    		[ "X$1" = "X" ] && usage "option --desc needs an argument"
		DESC=$1
		;;
    --res)	shift
    		[ $# = 0 ] && usage "option --res needs an argument"
    		[ "X$1" = "X" ] && usage "option --res needs an argument"
		res=$1
		;;

    -h|--help)		show_help; exit 0 ;;
    -v|--verbose)	VERBOSE=true ;;
    -q|--quiet)		VERBOSE=false ;;
    -D|--debug)		F_DEBUG=true ;;
    -D[0-9]|--debug=[0-9])
    			F_DEBUG=true; DBGLEVEL=$(echo "X$1" | sed -e 's/^X-D//' -e 's/^X--debug=//')
			;;
    --)			break ;;
    -*|"")		usage "unknown parm: '$1'" ;;
  esac
  shift
done

[ "X$CLASS" = "X" ] && {
	CLASS=$(kusa-conf class)
	CLASS=$(echo "$CLASS" | sed -e 's/ .*//')
}

case $res in
  low)	imgext="png"; res="-$res" ;;
  hi)	imgext="jpg"; res="-$res" ;;
  *)	echo "error: image resolution must be 'hi' or 'low'" >&2
  	exit 1
	;;
esac

image=

decho "searching image for CLASS='$CLASS'"

[ "X$DESC" = "X" ] && {
	DESC=$(kusa-conf machine.desc 2>/dev/null) || :
}
DESC=$(echo "$DESC" | tr '[A-Z]' '[a-z]')


case $CLASS in
  none)				image="noclass" ;;
  office*)			image="server-office" ;;
  media*)			image="server-media" ;;
  ws*|virt-ws*)			image="desktop" ;;
  *mobile*)			image="mobile" ;;
  *laptop*)			image="mobile" ;;
  server.mail*)
	case $DESC in
	  *archive*|*archivio*)	image="server-mail-archive" ;;
	  *)			image="server-mail" ;;
	esac
	;;
  server.backup)		image="server-backup" ;;
  server.lamp*|server.web*)	image="server-web" ;;
  server.build)			image="server-build" ;;
  server.glpi)			image="server-glpi" ;;
  server*)
  	case $DESC in
	  *zabbix*)		image="server-audit" ;;
	  *nextcloud*)		image="server-nextcloud" ;;
	  *)			image="server" ;;
	esac
esac

[ "X$image" != "X" ] && {
	if $search_splash
	then
		image="ku-splash-$image.jpg"
		SEARCH_PATH_ADD="/usr/share/images/ku-splash"
	else
		image="ku-machine-$image.$imgext"
		SEARCH_PATH_ADD="/usr/share/images/ku$res"
	fi
}

$do_search || {
	if [ "X$image" != "X" ]
	then
		echo "$image"
		exit 0
	else
		exit 1
	fi
}

[ "X$image" = "X" ] && {
	$VERBOSE && echo "$CMD: cannot find suitable image for machine class $CLASS" >&2
	exit 1
}

SEARCH_PATH=$(kusa-conf search_path)
MODNAME=
MODRUNDIR=$(pwd)

imgpath=$(filepath $image) || :

[ "X$imgpath" = "X" ] && {
	$VERBOSE && echo -e "$CMD: image '$image' not found in\n  $SEARCH_PATH $SEARCH_PATH_ADD" >&2
	exit 1
}

echo "$imgpath"
exit 0
