#!/bin/bash
dir="$1"
genre=187 #"Podcast"

if ! [ -f $dir/folder.jpg ]; then
  albumart=$(cat $dir/playlist.json | jq -r ".[].shows[].image_large" 2>/dev/null)
  curl -s "$albumart" -o $dir/folder.jpg
fi

for file in $dir/*; do
  basefile=$(basename $file)
  if echo $basefile | grep -E "mp3|m4a" > /dev/null; then
    id=${basefile%.*}
    showname=$(cat $dir/playlist.json | jq -r ".[].shows[].title" 2>/dev/null)
    showname=$(echo $showname | iconv -f utf-8 -t ascii//translit)
    #showname=$(echo $showname | anyascii)
    extension=${basefile#*.}
    timestamp=$(cat $dir/playlist.json | jq -r ".[].episodes[] | select(.id==$id).date_published" 2>/dev/null)
    echo $timestamp
    date=$(date +'%Y-%m-%d' -d @${timestamp})
    summary=$(cat $dir/playlist.json | jq -r ".[].episodes[] | select(.id==$id).description" 2>/dev/null)
    summary=$(echo $summary | tr -d '\r\n')
    summary=$(echo $summary | iconv -f utf-8 -t ascii//translit)
    #summary=$(echo $summary | anyascii)
    season=$(cat $dir/playlist.json | jq -r ".[].episodes[] | select(.id==$id).season" 2>/dev/null)
    episode=$(cat $dir/playlist.json | jq -r ".[].episodes[] | select(.id==$id).episode_number" 2>/dev/null)
    title=$(cat $dir/playlist.json | jq -r ".[].episodes[] | select(.id==$id).title" 2>/dev/null)
    title=$(echo $title| iconv -f utf-8 -t ascii//translit)
    #title=$(echo $title | anyascii)
    filetitle=${title//[^[:alnum:]]/_}
    filetitle="${date}.${filetitle}.${extension}"

    if [ "$extension" = "mp3" ]; then
      if (exiftool -if 'not defined $Picture' "$file" > /dev/null); then
        tageditor set cover= cover0="$dir/folder.jpg:front-cover" -f "$file"
      fi
      tageditor set comment= comment0="$summary" -f "$file"
      if [ "$season" = "null" ] && [ "$episode" = "null" ]
      then
        id3 --year "$date" --title "$title" --artist "$showname" --album "$showname" --genre "$genre" "$file"
      elif [ "$season" != "null" ] && [ "$episode" = "null" ]
      then
        track="$season"00
        id3 --year "$date" --title "$title" --track "$track" --artist "$showname" --album "Season ${season}" --genre "$genre" "$file"
      elif [ "$season" = "null" ] && [ "$episode" != "null" ]
      then
        track="$episode"
        id3 --year "$date" --title "$title" --track "$track" --artist "$showname" --album "$showname" --genre "$genre" "$file"
      elif [ "$season" != "null" ] && [ "$episode" != "null" ]
      then
        episode_padded=$(printf %02d $episode)
        track="$season""$episode_padded"
        id3 --year "$date" --title "$title" --track "$track" --artist "$showname" --album "Season ${season}" --genre "$genre" "$file"
      fi
    fi

    if [ "$extension" = "m4a" ]; then
      if (exiftool -if 'not defined $CoverArt' "$file" > /dev/null); then
        atomicparsley "$file" --artwork "${dir}/folder.jpg" --podcastFlag --overWrite
      fi
      if [ "$season" = "null" ] && [ "$episode" = "null" ]
      then
        atomicparsley "$file" --year "$date"T00:00:00Z --title "$title" --artist "$showname" --album "$showname" --genre "Podcast" --podcastFlag --comment "$summary" --overWrite
      elif [ "$season" != "null" ] && [ "$episode" = "null" ]
      then
        track="$season"00
        atomicparsley "$file" --year "$date"T00:00:00Z --title "$title" --tracknum "$track" --artist "$showname" --album "Season ${season}" --genre "Podcast" --podcastFlag --comment "$summary" --overWrite
      elif [ "$season" = "null" ] && [ "$episode" != "null" ]
      then
        track="$episode"
        atomicparsley "$file" --year "$date"T00:00:00Z --title "$title" --tracknum "$track" --artist "$showname" --album "$showname" --genre "Podcast" --podcastFlag --comment "$summary" --overWrite
      elif [ "$season" != "null" ] && [ "$episode" != "null" ]
      then
        episode_padded=$(printf %02d $episode)
        track="$season""$episode_padded"
        atomicparsley "$file" --year "$date"T00:00:00Z --title "$title" --tracknum "$track" --artist "$showname" --album "Season ${season}" --genre "Podcast" --podcastFlag --comment "$summary" --overWrite
      fi
    fi

    mv $file $dir/$filetitle

    touch -a -m --date=@$timestamp $dir/$filetitle

    rm -f ${dir}/*.bak
  fi
done
