#!/bin/bash
source /opt/scripts_local/shared_functions.sh
API_KEY="api-key: c716cbf5-bc99-4c2b-9c12-44ae41d1344c"
X_HEADER="http://vmx-nas.swallow-grue.ts.net:22025/api"
CONTENT_HEADER="Content-Type: application/json"
DEBUG=false

exit 0

#EPISODEFILE_RELATIVEPATH=$sonarr_episodefile_relativepath
#SERIES_PATH=$sonarr_series_path
#EPISODEFILE_PATH=$sonarr_episodefile_path
#EPISODEFILE_SEASONNUMBER="$sonarr_episodefile_seasonnumber"

EPISODEFILE_RELATIVEPATH="/mnt/move/series/Percy Jackson and the Olympians (2023)/Season 1/Percy Jackson and the Olympians (2023) - S01E05 - A God Buys Us Cheeseburgers [Profile 1_10_1 Releases Source (Streaming Services)_3_4 WEBDL-2160p][HDR10][EAC3 Atmos 5.1][h265]-NTb.mkv"
SERIES_PATH="/mnt/union/series/Percy Jackson and the Olympians (2023)"
EPISODEFILE_PATH="Season 1/Percy Jackson and the Olympians (2023) - S01E05 - A God Buys Us Cheeseburgers [Profile 1_10_1 Releases Source (Streaming Services)_3_4 WEBDL-2160p][HDR10][EAC3 Atmos 5.1][h265]-NTb.mkv"
EPISODEFILE_SEASONNUMBER="Season 5"

DESTINATION_SERVER="root@vmx-nas.swallow-grue.ts.net:"
DESTINATION_PATH="$SERIES_PATH/$EPISODEFILE_SEASONNUMBER"
#PATH_DIRNAME=$(dirname "$EPISODEFILE_RELATIVEPATH") # Season 01/Pretty Little Liars (2010) - S01E22 - For Whom the Bell Tolls [WEBDL-1080p][AC3 5.1][h264]-NTb.mkv
#DESTINATION_PATH="$SERIES_PATH/$EPISODEFILE_PATH"
SOURCE_PATH=$(dirname "$EPISODEFILE_RELATIVEPATH")

echo "$DESTINATION_SERVER"
echo "$DESTINATION_PATH"
echo "$SOURCE_PATH"

echo "Creating destination dirs."
ssh -o StrictHostKeyChecking=no root@vmx-nas.swallow-grue.ts.net "bash -s" << EOF
  mkdir -p "$DESTINATION_PATH";
  echo "$DESTINATION_PATH"
EOF

echo "Uploading files to server."
rsync -avzhP -e "ssh -o StrictHostKeyChecking=no" --remove-source-files --progress "$SOURCE_PATH/" "$DESTINATION_SERVER$DESTINATION_PATH/" &&  find "$SOURCE_PATH" -type d -empty -delete

echo "Applying file permissions and ownership"
ssh -o StrictHostKeyChecking=no root@vmx-nas.swallow-grue.ts.net "bash -s" << EOF
  chmod 775 -R "$DESTINATION_PATH";
  chown dbi:fs_data -R "$DESTINATION_PATH"
EOF

TMM_PATH=$(dirname "/media/$(echo "$sonarr_episodefile_path" | cut -d'/' -f 4-)")
echo "$TMM_PATH"

#curl -d '[{"action":"update", "scope":{"name":"all"}},{"action":"scrape", "scope":{"name":"new"}}]' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/movies"
curl -d '{"action":"update", "scope":{"name":"all"}},' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/tvshow" 

curl -d '{"action":"scrape", "scope":{"name":"new"}}' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/tvshow"
#curl -d '{"action":"scrape", "scope":{"path":"$TMM_PATH"}}' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/tvshow" 
#curl -d '{"action":"downloadSubtitle", "scope":{"name":"all"}, "args":{"language":"fr"}}' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/movies"
#curl -d '{"action":"downloadSubtitle", "scope":{"name":"all"}, "args":{"language":"fr"}}' -H "$CONTENT_HEADER" -H "$API_KEY" -X POST "$X_HEADER/tvshow"

sudo find /mnt/union/series ! -user dbi -exec chown dbi:fs_data {} \;

# sonarr_eventtype="$sonarr_eventtype"	    #Download
# sonarr_isupgrade="$sonarr_isupgrade"  #True when an existing file is upgraded, False otherwise
# sonarr_series_id="$sonarr_series_id"	#Internal ID of the series
# sonarr_series_title="$sonarr_series_title"	#Title of the series
# sonarr_series_path="$sonarr_series_path"			#Full path to the series
# sonarr_series_tvdbid="$sonarr_series_tvdbid"			#TVDB ID for the series
# sonarr_series_tvmazeid="$sonarr_series_tvmazeid"			#TVMaze ID for the series
# sonarr_series_imdbid="$sonarr_series_imdbid"			#IMDB ID for the series (empty if unknown)
# sonarr_series_type="$sonarr_series_type"			#Type of the series (Anime, Daily, or Standard)
# sonarr_episodefile_id="$sonarr_episodefile_id"			#Internal ID of the episode file
# sonarr_episodefile_episodecount="$sonarr_episodefile_episodecount"			#Number of episodes in the file
# sonarr_episodefile_relativepath="$sonarr_episodefile_relativepath"			#Path to the episode file, relative to the series path
# sonarr_episodefile_path="$sonarr_episodefile_path"			#Full path to the episode file
# sonarr_episodefile_episodeids="$sonarr_episodefile_episodeids"			#Internal ID(s) of the episode file
# sonarr_episodefile_seasonnumber="$sonarr_episodefile_seasonnumber"			#Season number of episode file
# sonarr_episodefile_episodenumbers="$sonarr_episodefile_episodenumbers"			#Comma-delimited list of episode numbers
# sonarr_episodefile_episodeairdates="$sonarr_episodefile_episodeairdates"			#Comma-delimited list of air dates from original network
# sonarr_episodefile_episodeairdatesutc="$sonarr_episodefile_episodeairdatesutc"			#Comma-delimited list of air dates in UTC
# sonarr_episodefile_episodetitles="$sonarr_episodefile_episodetitles"			#\|-delimited list of episode titles
# sonarr_episodefile_quality="$sonarr_episodefile_quality"			#Quality name of the episode file, as detected by Sonarr
# sonarr_episodefile_qualityversion="$sonarr_episodefile_qualityversion"			#1 is the default, 2 is for proper, and 3+ could be used for anime versions
# sonarr_episodefile_releasegroup="$sonarr_episodefile_releasegroup"			#Release group (empty if unknown)
# sonarr_episodefile_scenename="$sonarr_episodefile_scenename"			#Original release name (empty if unknown)
# sonarr_episodefile_sourcepath="$sonarr_episodefile_sourcepath"			#Full path to the imported episode file
# sonarr_episodefile_sourcefolder="$sonarr_episodefile_sourcefolder"			#Full path to the folder the episode file was imported from
# sonarr_download_client="$sonarr_download_client"			#Download client
# sonarr_download_id="$sonarr_download_id"			#Hash of the torrent/NZB file (used to uniquely identify the download in the download client)
# sonarr_deletedrelativepaths="$sonarr_deletedrelativepaths"			#\|-delimited list of files that were deleted to import this file
# sonarr_deletedpaths="$sonarr_deletedpaths"			#\|-delimited list of full paths of files that were deleted to import this file

# if [ "$DEBUG" = true ]; then
#   echo "$sonarr_eventtype" >>                      /4me/scripts/sonarr.txt    # Download
#   echo "$sonarr_isupgrade" >>                      /4me/scripts/sonarr.txt    # False
#   echo "$sonarr_series_id" >>                      /4me/scripts/sonarr.txt    # 1880
#   echo "$sonarr_series_title"	>>                   /4me/scripts/sonarr.txt    # Pretty Little Liars
#   echo "$sonarr_series_path" >>                    /4me/scripts/sonarr.txt    # /mnt/union/series/Pretty Little Liars (2010)
#   echo "$sonarr_series_tvdbid" >>                  /4me/scripts/sonarr.txt    # 146711
#   echo "$sonarr_series_tvmazeid" >>                /4me/scripts/sonarr.txt    # 177
#   echo "$sonarr_series_imdbid" >>                  /4me/scripts/sonarr.txt    # tt1578873
#   echo "$sonarr_series_type" >>                    /4me/scripts/sonarr.txt    # Standard
#   echo "$sonarr_episodefile_id" >>                 /4me/scripts/sonarr.txt    # 434248
#   echo "$sonarr_episodefile_episodecount" >>       /4me/scripts/sonarr.txt    # 1
#   echo "$sonarr_episodefile_relativepath" >>       /4me/scripts/sonarr.txt    # Season 01/Pretty Little Liars (2010) - S01E22 - For Whom the Bell Tolls [WEBDL-1080p][AC3 5.1][h264]-NTb.mkv
#   echo "$sonarr_episodefile_path" >>               /4me/scripts/sonarr.txt    # /mnt/union/series/Pretty Little Liars (2010)/Season 01/Pretty Little Liars (2010) - S01E22 - For Whom the Bell Tolls [WEBDL-1080p][AC3 5.1][h264]-NTb.mkv
#   echo "$sonarr_episodefile_episodeids" >>         /4me/scripts/sonarr.txt    # 90146
#   echo "$sonarr_episodefile_seasonnumber" >>       /4me/scripts/sonarr.txt    # 1
#   echo "$sonarr_episodefile_episodenumbers" >>     /4me/scripts/sonarr.txt    # 22
#   echo "$sonarr_episodefile_episodeairdates" >>    /4me/scripts/sonarr.txt    # 2011-03-21
#   echo "$sonarr_episodefile_episodeairdatesutc" >> /4me/scripts/sonarr.txt    # 03/22/2011 00:00:00
#   echo "$sonarr_episodefile_episodetitles" >>      /4me/scripts/sonarr.txt    # For Whom the Bell Tolls 
#   echo "$sonarr_episodefile_quality" >>            /4me/scripts/sonarr.txt    # WEBDL-1080p
#   echo "$sonarr_episodefile_qualityversion" >>     /4me/scripts/sonarr.txt    # 1
#   echo "$sonarr_episodefile_releasegroup" >>       /4me/scripts/sonarr.txt    # NTb
#   echo "$sonarr_episodefile_scenename" >>          /4me/scripts/sonarr.txt    # Pretty.Little.Liars.S01E22.For.Whom.the.Bell.Tolls.1080p.WEB-DL.DD5.1.H.264-NTb
#   echo "$sonarr_episodefile_sourcepath" >>         /4me/scripts/sonarr.txt    # /mnt/downloads/nzb/completed/series/Pretty.Little.Liars.S01E22.For.Whom.the.Bell.Tolls.1080p.WEB-DL.DD5.1.H.264-NTb/Pretty.Little.Liars.S01E22.For.Whom.the.Bell.Tolls.1080p.WEB-DL.DD5.1.H.264-NTb.mkv
#   echo "$sonarr_episodefile_sourcefolder" >>       /4me/scripts/sonarr.txt    # /mnt/downloads/nzb/completed/series/Pretty.Little.Liars.S01E22.For.Whom.the.Bell.Tolls.1080p.WEB-DL.DD5.1.H.264-NTb
#   echo "$sonarr_download_client" >>                /4me/scripts/sonarr.txt    # Sabnzbd
#   echo "$sonarr_download_id" >>                    /4me/scripts/sonarr.txt    # SABnzbd_nzo_qa05jefs
#   echo "$sonarr_deletedrelativepaths" >>           /4me/scripts/sonarr.txt
#   echo "$sonarr_deletedpaths" >>                   /4me/scripts/sonarr.txt
# fi
