#!/usr/bin/env 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/eggs/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/eggs/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
      if [ "$AUDIO" = "YES" ]; then
          nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/sv2SOTD.wav 2>/dev/null >/dev/null &
      fi
    misfortune ~/.local/share/tetosong/fortunes/**/*/* # fortune doesnt care if you give it a directory, apparently misfortune does. watch this be a problem when i go to add easter eggs in a separate file...
  fi
else
    if [ "$AUDIO" = "YES" ]; then
        nohup ffplay -nodisp -autoexit -v quiet ~/.local/share/tetosong/sv2SOTD.wav 2>/dev/null >/dev/null &
    fi
    fortune ~/.local/share/tetosong/fortunes/**/*/
fi
