#!/bin/bash

###
# Sonarr/Radarr/Lidarr run the script with injected environment variables.
# Sonarr reference here: https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts
# Radarr reference here: https://github.com/Radarr/Radarr/wiki/Custom-Post-Processing-Scripts
# Lidarr reference here: https://github.com/Lidarr/Lidarr/wiki/Custom-Post-Processing-Scripts
###
# TGRAM_CHAT_ID=-1001683027861 # Desbreit Media
# TGRAM_CHAT_ID=-1001730456573 # Desbreit Sysadmin
TGRAM_BOT_TOKEN=6643122498:AAHkAoXUL3t5oMcQtqsaXsEeojdoKB90eZg #Desbreit_arr_bot
TGRAM_SILENT=no
POVER_USER_KEY="ucz8kryp1v73dizqo7t9krq4312ewy"
POVER_PRIO="0" # -2 is low, -1 is moderate, 0 is normal, 1 is high and 2 is emergency
SONARR_POVER="aw5e5mny7b254adqfkiqiqgydodixe"
ARR_ERR_WARN_POVER="a4ieptv2oaddbjpumjswn64mp9j9y3"
STOP_CONDITIONS=("Ok" "Notice")
CONTINUE_CONDITIONS=("Warning" "Error")
STOP_ISSUE_TYPE_CONDITION=("IndexerLongTermStatusCheck" "IndexerStatusCheck")

# Get Variables from Radarr injected environment variables
# https://wiki.servarr.com/en/radarr/custom-scripts
RADARR_EVENTTYPE="$radarr_eventtype"
RADARR_HEALTH_ISSUE_LEVEL="$radarr_health_issue_level"
RADARR_HEALTH_ISSUE_MESSAGE="$radarr_health_issue_message"	# Message from the health issue
RADARR_HEALTH_ISSUE_TYPE="$radarr_health_issue_type"	    # Area that failed and triggered the health issue
RADARR_HEALTH_ISSUE_WIKI="$radarr_health_issue_wiki"	    # Wiki URL (empty if does not exist)
RADARR_ISUPGRADE="$radarr_isupgrade"
RADARR_MOVIE_ID="$radarr_movie_id"
RADARR_MOVIE_IMDBID="$radarr_movie_imdbid"
RADARR_MOVIE_IMDBLINK="https://www.imdb.com/title/$radarr_movie_imdbid"
RADARR_MOVIE_MDBLINK="https://www.mdblist.com/movie/$radarr_movie_imdbid"
RADARR_MOVIE_IN_CINEMAS_DATE="$radarr_movie_in_cinemas_date"
RADARR_MOVIE_TITLE="$radarr_movie_title"
RADARR_MOVIE_YEAR="$radarr_movie_year"
RADARR_HEALTH_ISSUE_TO_SKIP=("IndexerLongTermStatusCheck" "IndexerStatusCheck" "RemovedMovieCheck")
RADARR_HEALTH_ISSUE_TO_SKIP_STRING=" ${RADARR_HEALTH_ISSUE_TO_SKIP[*]} "


# Get Variables from Sonarr injected environment variables
# https://wiki.servarr.com/en/sonarr/custom-scripts
SONARR_EVENTTYPE="$sonarr_eventtype"
SONARR_HEALTH_ISSUE_LEVEL="$sonarr_health_issue_level"
SONARR_HEALTH_ISSUE_MESSAGE="$sonarr_health_issue_message"
SONARR_HEALTH_ISSUE_TYPE="$sonarr_health_issue_type"
SONARR_HEALTH_ISSUE_WIKI="$sonarr_health_issue_wiki"
SONARR_ISUPGRADE="$sonarr_isupgrade"
SONARR_SERIES_ID="$sonarr_series_id"
SONARR_SERIES_IMDBID="$sonarr_series_imdbid"
SONARR_SERIES_IMDBLINK="https://www.imdb.com/title/$sonarr_series_imdbid"
SONARR_SERIES_MDBLINK="https://www.mdblist.com/show/$sonarr_series_imdbid"
SONARR_SERIES_TITLE="$sonarr_series_title"
SONARR_SERIES_YEAR="$sonarr_series_year"
SONARR_HEALTH_ISSUE_TO_SKIP=("IndexerLongTermStatusCheck" "IndexerStatusCheck" "RemovedSeriesCheck")
SONARR_HEALTH_ISSUE_TO_SKIP_STRING=" ${SONARR_HEALTH_ISSUE_TO_SKIP[*]} "

# Detect who (RADARR, SONARR OR LIDARR) is running the app
SCOUNT=$(printenv | grep sonarr | wc -l)
RCOUNT=$(printenv | grep radarr | wc -l)
LCOUNT=$(printenv | grep lidarr | wc -l)

# --------------------------------------------
# -------------- SONARR ----------------------
# --------------------------------------------
if [[ $SCOUNT -gt 0 ]]; then
  # Determine which event happened
  case "$SONARR_EVENTTYPE" in 
    "Download")
      if [[ "$SONARR_ISUPGRADE" = "False" ]]; then
        POVER_PRIO=-2
        TITLE="New Series Downloaded"
        BODY="$SONARR_SERIES_TITLE $SONARR_SERIES_IMDBLINK $SONARR_SERIES_MDBLINK"
        TGRAM_CHAT_ID=-1001683027861 # Desbreit Media
        TGRAM_SILENT=yes
      else
        exit 0
      fi
      ;;
    "HealthIssue")
      if [[ " $STOP_CONDITIONS[*] " != *" $SONARR_HEALTH_ISSUE_LEVEL "* ]]; then
        if [[ " $SONARR_HEALTH_ISSUE_TO_SKIP_STRING " != *" $SONARR_HEALTH_ISSUE_TYPE "* ]]; then 
        if [[ "$SONARR_HEALTH_ISSUE_LEVEL" == "warning" ]]; then
          POVER_PRIO="0"
        else
          POVER_PRIO="1"
        fi
          TITLE="Sonarr '$SONARR_HEALTH_ISSUE_LEVEL' Health Issue"
          BODY="A '$SONARR_HEALTH_ISSUE_LEVEL' has been raised in '$SONARR_HEALTH_ISSUE_TYPE' 
          with the following message: '$SONARR_HEALTH_ISSUE_MESSAGE'. 
          Wiki issue if available: '$SONARR_HEALTH_ISSUE_WIKI'.
          ARRAY=$SONARR_HEALTH_ISSUE_TO_SKIP_STRING & ISSUE_RAISED=$SONARR_HEALTH_ISSUE_TYPE"  
          TGRAM_CHAT_ID=-1001730456573 # Desbreit Sysadmin
          TGRAM_SILENT=no
        else
          exit 0
        fi
      else
        exit 0
      fi
      ;; 
    "Test")
      TITLE="Sonarr Health Issue Test"
      BODY="The test connection from Sonarr was successful!"
      TGRAM_CHAT_ID=-1001730456573 # Desbreit Sysadmin
      TGRAM_SILENT=no
      ;; 
  esac
# --------------------------------------------
# -------------- RADARR ----------------------
# --------------------------------------------
elif [[ $RCOUNT -gt 0 ]]; then
  case "$RADARR_EVENTTYPE" in
  "Download")
    if [[ "$RADARR_ISUPGRADE" = "False" ]]; then
      TITLE="New Movie Downloaded"
      BODY="$RADARR_MOVIE_TITLE $RADARR_MOVIE_IMDBLINK $RADARR_MOVIE_MDBLINK"
      POVER_PRIO=-2
      TGRAM_CHAT_ID=-1001683027861 # Desbreit Media
      TGRAM_SILENT=yes
    else
      exit 0
    fi
    ;;
  "HealthIssue")
    if [[ " $STOP_CONDITIONS[*] " != *" $RADARR_HEALTH_ISSUE_LEVEL "* ]]; then
      if [[ " $RADARR_HEALTH_ISSUE_TO_SKIP_STRING " != *" $RADARR_HEALTH_ISSUE_TYPE "* ]]; then 
        if [[ "$RADARR_HEALTH_ISSUE_LEVEL" == "warning" ]]; then
          POVER_PRIO="0"
        else
          POVER_PRIO="1"
        fi
        TITLE="Radarr '$RADARR_HEALTH_ISSUE_LEVEL' Health Issue"
        BODY="A '$RADARR_HEALTH_ISSUE_LEVEL' has been raised in '$RADARR_HEALTH_ISSUE_TYPE' 
        with the following message: '$RADARR_HEALTH_ISSUE_MESSAGE'. 
        Wiki issue if available: '$RADARR_HEALTH_ISSUE_WIKI'.
        ARRAY=$RADARR_HEALTH_ISSUE_TO_SKIP_STRING & ISSUE_RAISED=$RADARR_HEALTH_ISSUE_TYPE"
        TGRAM_CHAT_ID=-1001730456573 # Desbreit Sysadmin 
        TGRAM_SILENT=no
      else
        exit 0
      fi
    else
      exit 0
    fi
    ;; 
  "Test")
    TITLE="Test Successful!"
    BODY="The test connection from radarr was successful!"
    TGRAM_CHAT_ID=-1001730456573 # Desbreit Sysadmin
    TGRAM_SILENT=no
    ;;
  esac
else
  BODY="Nothing detected:
  sonarr $SCOUNT, radarr $RCOUNT, lidarr $LCOUNT"
fi

# Build connection string, rockets://webhook@host/
# CONNECTION="pover://$POVER_USER_KEY@$SONARR_POVER/?priority=$POVER_PRIO"

CONNECTION="tgram://$TGRAM_BOT_TOKEN/$TGRAM_CHAT_ID/?silent=$TGRAM_SILENT"

apprise -v -t "$TITLE" -b "$BODY" "$CONNECTION"
