#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.4"
CMDSTRING="$CMD v$CMDVER (2023-11-29)"

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 "
== $CMD $CMDSTRING == 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()
{
	:
}






# (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/ .*//')

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'"

case $CLASS in
  none)				image="noclass" ;;
  office*)			image="officecore" ;;
  media*)			image="mediacore" ;;
  ws*|virt-ws*)			image="station" ;;
  *mobile*)			image="mobile" ;;
  *laptop*)			image="mobile" ;;
  server.mail*)
  	DESC=${DESC:-$(kusa-conf machine.desc 2>/dev/null) || :}
	case $(echo "$DESC" | tr '[A-Z]' '[a-z]') in
	  *archive*|*archivio*)	image="mailarchive" ;;
	  *)			image="mailserver" ;;
	esac
	;;
  server.backup)		image="backupserver" ;;
  server.lamp*|server.web*)	image="server-web" ;;
  server.build)			image="server-build" ;;
  server.glpi)			image="server-glpi" ;;
  server*)			image="server" ;;
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
