#!/bin/bash
###
# This script utilizes apprise (https://github.com/caronc/apprise) to send notifications
# You need to have apprise available for the user sonarr operates under.
# I installed it via `sudo pip install --system apprise` although the python
# community really dislikes it when you do that. Recommended installation would be
# something along the lines of: 
#
#   sudo su sonarr -s /bin/bash
#   pip install --user apprise
#
# Repeat that for each user that would be running the script. Please make sure you have 
# pip installed already for that to work.
###
# 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
###

# Connection Details
##
# Set up a webhook integration on your Rocket.Chat server. This is found in 
# Administration -> Integrations -> New Integration -> Webhook
# Set up the destination channel and which user the webhook will masquerade as.
# Then fill in the details below for your server.
##
# Server Host
RCHOST="rocketchat.host"
# Single Webhook Token (either set this or set one for each service below).
# Do not try to set both or this one will take presidence.
#RCWEBHOOK="base64token/moretoken"
# Per Service Webhook Token
SONARRWEBHOOK="base64token/moretoken"
RADARRWEBHOOK="base64token/moretoken"
LIDARRWEBHOOK="base64token/moretoken"

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

# Message Content
##
# Here we set up the conent of the message. Mostly just form fill based on what information we get from Sonarr.
##
if [[ $SCOUNT -gt 0 ]]; then
# Detected Sonarr
if [ -z $RCWEBHOOK ]; then RCWEBHOOK=$SONARRWEBHOOK; fi
# Determine which event happened
case $sonarr_eventtype in "Grab")
  # We are getting a notification on grabbing a new episode, act accordingly.
  TITLE="Episode Grabbed"
  SIZEGB=$(bc <<< "scale=2; $sonarr_release_size / 1000000000")
  BODY="Grabbed $sonarr_series_title Season $sonarr_release_seasonnumber, E$sonarr_release_episodenumbers
  $sonarr_release_quality ($SIZEGB GB)
  From $sonarr_release_indexer"
  ;; "Download")
  # Check if we upgraded an exisiting file or downloaded a new one
  if [[ $sonarr_isupgrade = "True" ]]; then
    # We are upgrading an exisitng file
    TITLE="Episode Upgraded"
    BODY="Upgraded $sonarr_series_title
    Season $sonarr_episodefile_seasonnumber, E$sonarr_episodefile_episodenumbers - 
$sonarr_episodefile_edpisodetitles
    $sonarr_episodefile_quality"
  else
    # We are getting a notification on downloading a new episode, act accordingly.
    TITLE="Episode Downloaded"
    BODY="$sonarr_series_title
    Season $sonarr_episodefile_seasonnumber, E$sonarr_episodefile_episodenumbers 
$sonarr_episodefile_episodetitles
    $sonarr_episodefile_quality"
  fi
  ;; "Rename")
  # Sonarr just renamed episodes for a series.
  TITLE="Files renamed"
  BODY="Renamed files for $sonarr_series_title"
  ;; "Test")
  # You either clicked test or saved the connection
  TITLE="Success!"
  BODY="The test connection from Sonarr was successful!"
  ;; *)
  TITLE="Sonarr Event"
  BODY="This is an unhandled eventtype.
  $(printenv | grep sonarr)"
  ;; esac
  ## END SONARR
elif [[ $RCOUNT -gt 0 ]]; then
# Detected Radarr
if [ -z $RCWEBHOOK ]; then RCWEBHOOK=$RADARRWEBHOOK; fi
# Set the IMDB Link
IMDBLINK="https://www.imdb.com/title/$radarr_movie_imdbid"
# Determine which event happened
case $radarr_eventtype in
"Grab")
  # We are getting a notification on grabbing a new episode, act accordingly.
  TITLE="Episode Grabbed"
  SIZEGB=$(bc <<< "scale=2; $radarr_release_size / 1000000000")
  BODY="$radarr_movie_title $radarr_release_quality ($SIZEGB GB)
  $radarr_release_title from $radarr_release_indexer
  Sent to $radarr_download_client
  $IMDBLINK"
  ;;
"Download")
  # Check if we upgraded an exisiting file or downloaded a new one
  if [[ $sonarr_isupgrade = "True" ]]; then
    # We are upgrading an exisitng file
    TITLE="Movie Upgraded"
    BODY="Upgraded $radarr_movie_title
    $radarr_Moviefile_quality
    $IMDBLINK"
  else
    # We are getting a notification on downloading a new Movie, act accordingly.
    TITLE="Movie Downloaded"
    BODY="$radarr_movie_title
    $radarr_moviefile_quality
    $IMDBLINK"
  fi
  ;;
"Rename")
  # radarr just renamed episodes for a series.
  TITLE="Files renamed"
  BODY="Renamed files for $radarr_movie_title"
  ;;
"Test")
  # You either clicked test or saved the connection
  TITLE="Success!"
  BODY="The test connection from radarr was successful!"
  ;;
esac
elif [[ $LCOUNT -gt 0 ]]; then
# Detected Lidarr
if [ -z $RCWEBHOOK ]; then RCWEBHOOK=$LIDARRWEBHOOK; fi
# Determine which event happened
case $lidarr_eventtype in
"Grab")
  # We are getting a notification on grabbing a new episode, act accordingly.
  TITLE="Music Grabbed"
  BODY="$lidarr_release_title"
;;
"Download")
  # We are getting a notification on downloading a new episode, act accordingly.
  TITLE="Music Downloaded"
  BODY="upgrade $lidarr_isupgrade for $lidarr_artist_name
$lidarr_album_title"
  ;;
"Rename")
  # Sonarr just renamed episodes for a series.
  TITLE="Files renamed"
  BODY="Renamed files for $lidarr_artist_name"
  ;;
"Test")
  # You either clicked test or saved the connection
  TITLE="Success!"
  BODY="The test connection from Lidarr was successful!"
  ;;
esac
else
  BODY="Nothing detected:
  sonarr $SCOUNT, radarr $RCOUNT, lidarr $LCOUNT"
fi

# Build connection string, rockets://webhook@host/
CONNECTION="rockets://$RCWEBHOOK@$RCHOST/?avatar=No"

# Send Message
##
# Send the notification by calling the apprise CLI
##
apprise -v -t "$TITLE" -b "$BODY" "$CONNECTION"