#!/bin/bash

ACTION="$1"

shift
PACKAGE_LIST=$@

FW_MODEL="https://huggingface.co/mukowaty/faster-whisper-int8/resolve/15d956df6a221df330485e243b2a5ed41f0e2728/faster-whisper-small-int8/model.bin"
FW_CONFIG="https://huggingface.co/mukowaty/faster-whisper-int8/resolve/15d956df6a221df330485e243b2a5ed41f0e2728/faster-whisper-small-int8/config.json"
FW_TOKENIZER="https://huggingface.co/mukowaty/faster-whisper-int8/resolve/15d956df6a221df330485e243b2a5ed41f0e2728/faster-whisper-small-int8/tokenizer.json"
FW_VOCABULARY="https://huggingface.co/mukowaty/faster-whisper-int8/resolve/15d956df6a221df330485e243b2a5ed41f0e2728/faster-whisper-small-int8/vocabulary.json"

DEST_DOWNLOAD="/opt/zero-lliurex-speech-note/models/multilang_fasterwhisper_small"
ORIG_BIN="/usr/share/zero-lliurex-speech-note/bin/lliurex-speech-note"
DEST_BIN="/usr/bin/lliurex-speech-note"
ORIG_DESKTOP="/usr/share/zero-lliurex-speech-note/desktop/net.mkiol.SpeechNote.desktop"
DEST_DESKTOP="/var/lib/flatpak/exports/share/applications/net.mkiol.SpeechNote.desktop"


case $ACTION in

	getStatus)
		
		if [ ! -s ${DEST_DOWNLOAD}"/model.bin" ]
		then
			echo 1	
		fi		

		if [ ! -s ${DEST_DOWNLOAD}"/config.json" ]
		then	
			echo 1	
		fi

		if [ ! -s ${DEST_DOWNLOAD}"/tokenizer.json" ]
		then	
			echo 1	
		fi

		if [ ! -s ${DEST_DOWNLOAD}"/vocabulary.json" ]
		then	
			echo 1	
		fi

		echo 0
		
	
	;;
	
	download)

		if ! [ -d ${DEST_DOWNLOAD} ]
		then
			mkdir -p ${DEST_DOWNLOAD}
		fi

		cd ${DEST_DOWNLOAD}
		
		if [ -e "model.bin" ]; then
			rm -rf  "model.bin"
		fi
		
		if [ -e "config.json" ]; then
			rm -rf  "config.json"
		fi
		
		if [ -e "tokenizer.json" ]; then
			rm -rf  "tokenizer.json"
		fi
		
		if [ -e "vocabulary.json" ]; then
			rm -rf  "vocabulary.json"
		fi
		
		wget ${FW_MODEL}
		wget ${FW_CONFIG}
		wget ${FW_TOKENIZER}
		wget ${FW_VOCABULARY}
		
	;;	

	installPackage)

		flatpak --system -y install net.mkiol.SpeechNote
		
		if [ ! -L ${DEST_BIN} ]; then\
			rm -rf ${DEST_BIN}
			ln -s ${ORIG_BIN} ${DEST_BIN}
		fi
		
		cp ${ORIG_DESKTOP} ${DEST_DESKTOP}
		

	;;

	remove)
		count=0
		for ix in $PACKAGE_LIST
		do
			if [[ "$ix" == "net.mkiol.SpeechNote" ]];then
				flatpak uninstall $ix -y

				TEST=$(flatpak list | grep $ix | cut -d " " -f 1)

				if [[ ! -z "$TEST" ]];then
					count+=1
				fi
			fi
		done
		
		if [ -d ${DEST_DOWNLOAD} ]; then
			rm -rf ${DEST_DOWNLOAD}
		fi
		
		if [ -e ${DEST_BIN} ]; then
			rm -rf ${DEST_BIN}
		fi
		
		if [ -e ${DEST_DESKTOP} ]; then
			rm -rf ${DEST_DESKTOP}
		fi

		if [ $count -gt 0 ];then
			exit 1
		fi
	;;

esac
exit 0	
