#!/bin/bash
###################################################################
#                                                                 #
#  Get song info and download lyrics from database.               #
#  Depends on: wget, jq                                           #
#                                                                 #
#  Scripted by Koentje   (remon@cobrasoft.nl)                     #
#                                                    version 1.3  #
###################################################################

# Files and paths
chkfile="./tempfiles/lyrics.chk"
txtfile="./tempfiles/lyrics.txt"
jqfile="./tempfiles/lyrics.jq"
urlfile="./tempfiles/lyrics.url"
outfile="./tempfiles/lyrics.out"


# Send message
sendmsg () {
   echo -e "\n\${color 888888}Get-lyrics: \${color FF0000} $msg" > "$outfile"
   echo -e "Get-lyrics: $msg" > /dev/stderr
}


# Check if getting data from active-player script
cmdline="$@"; arg1="$1"
#arg1="justin timberlake|like i love you|justified"
if [ "$cmdline" = "||" ]; then
  msg="No data from active-player.sh!  (flac?)"; sendmsg
  echo "" > "$chkfile"
  exit
elif [ "$arg1" = "" ]; then
  echo -e "\n This script is used by 'active-player.sh' !\n"
  exit
fi


# Get artist and title from control file lyrics.chk
if [ -f "$chkfile" ]; then
  check=$(cat "$chkfile")
else
  check="none"
fi

# Get song info from commandline
artist=$(echo "$arg1" | awk -F'|' '{print $1}')
title=$(echo "$arg1" | awk -F'|' '{print $2}')
album=$(echo "$arg1" | awk -F'|' '{print $3}')

# Check if we are already showing correct lyrics. If so then exit,
# else store new artist and title into controle file and download new lyrics.
if [ "$artist$title$album" != "$check" ]; then
  if [ "$artist$title$album" != "" ]; then
    echo "$artist$title$album" > "$chkfile"
  fi
else
  echo -e "Already showing \"$artist - $title\"  from album \"$album\"" > /dev/stderr
  exit
fi


### GET URL

# Replaces special characters with + for urls
urlartist=$(echo "$artist" | sed 's/[][{}() _~,]/+/g') #'
urltitle=$(echo "$title" | sed 's/[][{}() _~,]/+/g') #'
urlalbum=$(echo "$album" | sed 's/[][{}() _~,]/+/g') #'

# Create url for search
url_template='https://lrclib.net/api/get?artist_name=<artist>&track_name=<title>&album_name=<album>'
url="${url_template//"<artist>"/$urlartist}"
url="${url//"<title>"/$urltitle}"
url="${url//"<album>"/$urlalbum}"
echo "$url" > "$urlfile"

# Retreive lyrics
wget --no-verbose $url -O "$txtfile" -o "./tempfiles/wget.log"
jq .syncedLyrics "$txtfile" > "$jqfile"
lyrics=$(cat "$jqfile")


if [ "$lyrics" = "null" ] ; then
  # Put code here for plain text
  msg="No timed lyrics found!  (null)"; sendmsg
  exit
elif [ "$lyrics" = "" ]; then
  msg="No timed lyrics found!  (404)"; sendmsg
  exit
else
  lyrics=${lyrics:1:-1}
fi


# Store message from database to lyrics.txt
if [ "$lyrics" = "ul" ]; then
  msg="Sorry, no lyrics found!"; sendmsg
else
  echo -e "Artist: $artist\nTitle : $title\nAlbum : $album\n" > "$txtfile"
  echo -e "$lyrics" >> "$txtfile"
  echo -e "Lyrics found for \"$artist - $title\"" > /dev/stderr
  echo "" > "./tempfiles/first.time"
fi

