#!/usr/bin/bash
#
#   Updated 5-1-26 to use new vocafortune script and not require fortune/misfortune.
#
#

# argument handling
while [[ $# -gt 0 ]]; do
    case $1 in
        	-u|--update)
        		echo "Downloading tetosong updater..."
        		bash <(curl -s https://raw.githubusercontent.com/eric5949/tetosong/refs/heads/main/autoupdater/updater.sh)
        		shift
        		exit 0
        		;;
        -h|--help)
            echo "--- TETOSONG HELP ---"
            echo ""
            echo " -h | display this message"
            echo " -u | update script and fortunes"
            echo ""
            shift
            exit 0
            ;;
        *)
            break
            ;;
    esac
done

# check if the config file exists, if not download it
if [ ! -f ~/.local/share/tetosong/tetosong.config ]; then
	echo "Config file not found, downloading default..."
	curl -sLo ~/.local/share/tetosong/tetosong.config https://raw.githubusercontent.com/eric5949/tetosong/refs/heads/main/tetosong.config
fi
# check if the user wants to play audio
AUDIO="$(. ~/.local/share/tetosong/tetosong.config; echo $AUDIO)"

vocafortune >> /tmp/fortune
if [ "$AUDIO" = "YES" ]; then
    if grep -q "SV2" /tmp/fortune; then
        nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/audio/teto/sv2SOTD.wav 2>/dev/null >/dev/null &
    elif grep -q "SV" /tmp/fortune; then
        nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/audio/teto/svSOTD.wav 2>/dev/null >/dev/null &
    elif grep -q "重音テト" /tmp/fortune; then
        nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/audio/teto/utSOTD.wav 2>/dev/null >/dev/null &
    else
        nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/audio/teto/sv2SOTD.wav 2>/dev/null >/dev/null &
    fi
fi
    cat /tmp/fortune
    rm /tmp/fortune
