#!/usr/bin/bash
# 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/test/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/test/tetosong.config
fi
# check if the user wants to play audio
AUDIO="$(. ~/.local/share/tetosong/tetosong.config; echo $AUDIO)"

# check which fortune command is installed if any and then run it
if ! [ -x "$(command -v fortune)" ]; then
  if ! [ -x "$(command -v misfortune)" ]; then
      echo 'No fortune commmand is installed, exiting!'
      exit 1
  else
      misfortune ~/.local/share/tetosong/fortunes/*/* >> /tmp/fortune # fortune and misfortune handle directories differently
      if [ "$AUDIO" = "YES" ]; then # i guess i could foregoe checking and just run the audio either way since i dont download it unless the user enables it, but i want them to be ableto turn it off if they choose to
          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
  fi
else
    fortune ~/.local/share/tetosong/fortunes/* >> /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
fi
