#!/bin/bash
######################################################
#                                                    #
#   Conky output                                     #
#                                                    #
#   Depends on: playerctl, audtool, qmmp             #
#                                                    #
#   Scripted by Koentje  (remon@cobrasoft.nl)        #
#                                                    #
#                                      version 2.6   #
######################################################

if [ "$1" != "goplayer" ]; then
   echo -e "\n\e[31mERROR:\e[m Player can not start outside of conky!\n"
   exit 1
fi

source "./player.ini"
source "./themes/$theme.ini"

# Check if all independecies are installed
  if [ "$(which playerctl)" = "" ]; then echo -e "\e[31mERROR\e[m 'playerctl' not installed!\n" >/dev/stderr; exit 1; fi
  if [ "${player,,}" = "audacious" ]; then
    if [ "$(which audtool)" = "" ]; then echo -e "\e[31mERROR\e[m 'audtool' not installed!  (audacious package)\n" >/dev/stderr; exit 1; fi
  fi
  if [ "${player,,}" = "qmmp" ]; then
    if [ "$(which qmmp)" = "" ]; then echo -e "\e[31mERROR\e[m 'qmmp' not installed!\n" >/dev/stderr; exit 1; fi
  fi


# Check if player is running
playerctl -s -p ${player,,} status >/dev/null ; err=$?

if [ "$err" = "0" ]; then
     if [ "${player,,}" = "audacious" ]; then
        # Get artist, title & album from player
          artist=$(audtool current-song-tuple-data artist)
          title=$(audtool current-song-tuple-data title)
          album=$(audtool current-song-tuple-data album)
        # Save filename of mediafile to extract coverart (cover.sh)
          audtool current-song-filename > "./tempfiles/mediafilename"
        # Get current & total time of song
          stotal=$(audtool current-song-length)
          scurrent=$(audtool current-song-output-length)
          pstat="yes"
        # Save status of player to tempfile for buttons.sh
          playerctl -p $player status &>"./tempfiles/playerstatus"
        # Save volume of player to tempfile for volume bar
          playerctl -p $player volume &>"./tempfiles/playervolume"

     elif [ "${player,,}" = "qmmp" ]; then
        # Get artist, title & album from player
          artist=$(qmmp --no-start --nowplaying %p)
          title=$(qmmp --no-start --nowplaying %t)
          album=$(qmmp --no-start --nowplaying %a)
        # If album tag is empty, then use rootfolder of mediafiles
          if [ "$album" = "" ]; then
             album=$(qmmp --no-start --nowplaying %dir | awk -F'/' '{print $NF}')
          fi
        # Save filename of mediafile to extract coverart (cover.sh)
          qmmp --nowplaying %F > "./tempfiles/mediafilename"
        # Get current & total time of song
          stotal=$(qmmp --no-start --status | head -n1 | awk '{print $3}' | awk -F'/' '{print $2}')
          scurrent=$(qmmp --no-start --status | head -n1 | awk '{print $3}' | awk -F'/' '{print $1}')
          pstat="yes"
        # Save status of player and volume to tempfile for buttons.sh and volume bar
          playerctl -p $player status &>"./tempfiles/playerstatus"
        # Save volume of player to tempfile for volume bar
          playerctl -p $player volume &>"./tempfiles/playervolume"

     else  ###### If faulty player name in ini file
        echo -e "\e[31mERROR\e[m [player.ini] Faulty player name"
        exit 1
     fi
elif [ "$err" = "1" ]; then
  # Save volume of player to tempfile for volume bar
    echo 0 > "./tempfiles/playervolume"
    pstat="no"
fi


# Calculate percentage of songtime played for bar
if [ "$stotal" = "" ]; then
   # When song is stopped
   stotal="0:00"
   scurrent="0:00"
   echo "0" > "./tempfiles/bartime"
else
   # When song is playing or paused
   btotal=$(echo "$stotal" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') #'
   bcurrent=$(echo "$scurrent" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') #'
   expr 100 \* $bcurrent \/ $btotal > "./tempfiles/bartime"
fi


# Save artist & title for coverart internet search (cover.sh)
  artist=$(echo "$artist" | sed s'/`//'g)
  title=$(echo "$title" | sed s'/`//'g)
  echo "artist=\"$artist\"" > "./tempfiles/nowplaying"
  echo "title=\"$title\"" >> "./tempfiles/nowplaying"


# Show song artist, title, album and time or if player is not active
  if [ "$pstat" = "yes" ]; then
     echo "\${font $artistfontname:size=$artistfontsize}\${color $artistfontcolor}\${voffset $artistvoffset}\${goto 158}${artist:0:artistcutoff}\${font}"
     echo "\${font $titlefontname:size=$titlefontsize}  \${color $titlefontcolor} \${voffset $titlevoffset} \${goto 158}${title:0:titlecutoff}  \${font}"
     echo "\${font $albumfontname:size=$albumfontsize}  \${color $albumfontcolor} \${voffset $albumvoffset} \${goto 158}${album:0:albumcutoff}  \${font}"
     echo "\${font $timefontname:size=$timefontsize}    \${voffset $timevoffset}\${goto 159}\${color $timecurrentfontcolor}$scurrent  \${goto 354}\${color $timetotalfontcolor}$stotal"
  elif [ "$pstat" = "no" ]; then
     echo "\${font $artistfontname:size=$artistfontsize}\${voffset $((artistvoffset+11))}\${goto 158}\${color ff0000}${player^} not running\${font}"
     echo ""
     echo ""
  fi


# Show coverart
  echo "\${lua fDrawImage ./coverart/coverart.png 21 19 123 122}"


# Icon theme  (player.ini and theme.ini files in themes)
  echo "\${lua fDrawImage ./images/.cases/$case.png 0 0 410 200}"
  echo "\${lua fDrawImage ./images/.textbackgrounds/$textbackground.png 146 8 251 96}"
  ./buttons gobuttons
  cat "./tempfiles/buttons"


# VU meter background
  echo "\${lua fDrawImage ./images/.vu-backgrounds/$vubackground.png 153 105 236 68}"


# Checks for updates every hour
utime=$(date '+%M')
if [ "$utime" = "00" ]; then
   ./updater goupdater
fi
if [ -f ./tempfiles/new_update ]; then
   echo "\${lua fDrawImage ./images/nud 23 122 120 17}"
fi


exit 0

