#!/bin/bash
################################################################################
# Copyright 2025 ModalAI Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# 4. The Software is used solely in conjunction with devices provided by
#    ModalAI Inc.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
################################################################################

source /home/root/.profile.d/modalai_sku_definitions.sh

USER=$(whoami)

RESET_ALL="\e[0m"
RED="\e[91m"
YLW="\e[33m"
GRN="\e[32m"
SET_BOLD="\e[1m"

PRINT_ERROR (){
	echo -e "${RED}[ERROR] $@${RESET_ALL}"
}

PRINT_WARN (){
	echo -e "${YLW}[WARN] $@${RESET_ALL}"
}

## behavior modes
QUIET=""
JSON_MODE=""

print_usage () {
	clear -x
	echo -e "${GRN}${SET_BOLD}------------------------------------------------------------------${RESET_ALL}"
	echo -e "${GRN}${SET_BOLD}voxl-inspect-sku${RESET_ALL}"
	echo -e ""
	echo -e "This tool parses the SKU saved at $SKU_FILENAME"
	echo -e "and exports the following bash environment variables for things"
	echo -e "like voxl-configure-mpa to read."
	echo -e ""
	echo -e "You may also give it a SKU as an argument for testing"
	echo -e "or run it in quiet mode for scripting."
	echo -e ""
	echo -e "${GRN}${SET_BOLD}Arguments:${RESET_ALL}"
	echo -e "-h, --help     show this help text"
	echo -e "-q, --quiet    quiet mode, only write to disk"
	echo -e "-s, --sku      optionally provide sku as an argument"
	echo -e "-j, --json     output SKU information in JSON format"
	echo -e ""
	echo -e "${GRN}${SET_BOLD}------------------------------------------------------------------${RESET_ALL}"
	exit 0
}

_main(){

	while (( "$#" )); do
		case "$1" in
		"-h"|"--help")
			print_usage
			exit 0
			;;
		"-q"|"--quiet")
			QUIET=true
			;;
		"-j"|"--json")
			JSON_MODE=true
			;;
		"-s"|"-S"|"--sku"|"--SKU") # family version number
			if [ -z "$VOXL_SKU" ]; then
				VOXL_SKU="$2"
				shift
			else
				PRINT_ERROR "Received multiple attempts to set SKU, exiting"
				exit 1
			fi
			if [ -z "$VOXL_SKU" ]; then
				PRINT_ERROR "Received empty SKU argument"
				exit 1
			fi
			;;
		*)
			PRINT_ERROR "Invalid arg: $1, exiting"
			exit 1
			;;
		esac
		shift
	done

	# No manual SKU provided, read from disk
	if [ -z "$VOXL_SKU" ]; then
		## handle the old file name
		if [ -f "$FACTORY_FILENAME" ]; then
			if [ -f "$SKU_FILENAME" ]; then
				PRINT_WARN "Detected both the old SKU file $FACTORY_FILENAME"
				PRINT_WARN "and the new one: $SKU_FILENAME"
				PRINT_WARN "Deleting the old one"
				rm -rf "$FACTORY_FILENAME"
			else
				## most of the time, just migrate old to new
				mv "$FACTORY_FILENAME" "$SKU_FILENAME"
				PRINT_WARN "migrating old SKU file to $SKU_FILENAME"
			fi
		fi

		## read from file
		if ! [ -f "$SKU_FILENAME" ]; then
			PRINT_ERROR "missing $SKU_FILENAME"
			PRINT_ERROR "please run voxl-configure-sku to make a new one"
			exit 1
		fi
		VOXL_SKU=$( cat "$SKU_FILENAME" )
	fi

	if ! voxl-parse-and-export-sku-variables "$VOXL_SKU"; then
		PRINT_ERROR "failed to parse SKU: $VOXL_SKU"
		PRINT_ERROR "please run voxl-configure-sku to make a new one"
		exit 1
	fi

	# JSON mode output
	if [ "$JSON_MODE" == "true" ]; then
		voxl-print-sku-variables-json
	else
		# when not in quiet mode, print results
		if [ -z "$QUIET" ]; then
			echo -e ""
			voxl-print-sku-variables
			echo ""
		fi
	fi

	

	exit 0
}

_main "$@"
