#!/bin/bash
##################################################################
#                                                                #
#  Use this startup script to load all depending conkies.        #
#  If for some reason one of the conkies is not loaded, all      #
#  conkies will be unloaded.                                     #
#                                                                #
#  Do not change filenames or window titles of the conkies!      #
#  Always use the .conky extention!                              #
#  The scripts depend heavy on them!                             #
#                                                                #
#  Scripted by Koentje   (remon@cobrasoft.nl)                    #
#                                                                #
#  PLEASE DO NOT EDIT THIS SCRIPT !!!                            #
#                                                 version: 2.6   #
##################################################################

# Get real path to working folder
  scrpath=$(readlink -f $0)
  workdir=$(dirname $scrpath)
  cd "$workdir"


source "./player.ini"


# Check if independencies are installed
  if [ "$(which conky)" = "" ]; then echo -e "\e[31mERROR\e[m 'conky' not installed!  DUH!!!\n"; exit 1; fi


# Check if theme exist
if [ -f "./themes/$theme.ini" ]; then
   source "./themes/$theme.ini"
else
   echo -e "\e[31mERROR\e[m [player.ini] Wrong theme name set! Available themes are:"
   while read -r themeini
   do
        echo -e "     \e[33m $(echo "$themeini" | awk -F'.' '{print $1}') \e[m"
   done< <(ls  ./themes | egrep "\.ini$")
   exit 1
fi

# Check if case graphic exist
if ! [ -f "./images/.cases/$case.png" ]; then
   echo -e "\e[31mERROR\e[m [$theme.ini] Wrong case value set! Available cases are:"
   while read -r cases
   do
        echo -e "     \e[33m $(echo "$cases" | awk -F'.' '{print $1}') \e[m"
   done< <(ls  ./images/.cases | egrep "\.png$")
   exit 1
fi

# Check if buttonset graphics exist
if ! [ -d "./images/.media-buttons/$buttons" ]; then
   echo -e "\e[31mERROR\e[m [$theme.ini] Wrong buttons value set! Available buttons are:"
   while read -r buttonsets
   do
        echo -e "     \e[33m $(echo "$buttonsets" | awk -F'.' '{print $1}') \e[m"
   done< <(ls  ./images/.media-buttons)
   exit 1
fi

# Check if VU background graphic exist
if ! [ -f "./images/.vu-backgrounds/$vubackground.png" ]; then
   echo -e "\e[31mERROR\e[m [$theme.ini] Wrong VU background value set! Available backgrounds are:"
   while read -r vubackgrounds
   do
        echo -e "     \e[33m $(echo "$vubackgrounds" | awk -F'.' '{print $1}') \e[m"
   done< <(ls  ./images/.vu-backgrounds | egrep "\.png$")
   exit 1
fi


# At first start, check if default cover art exists
  if ! [ -f "./coverart/coverart.png" ]; then
     if [ "$coverartdefault" = "" ]; then
        cp -f "./images/.coverart/default-coverart.png" "./coverart/coverart.png"
     else
        cp -f "./images/.coverart/default-coverart-$coverartdefault.png" "./coverart/coverart.png"
     fi
  fi


# Change alignment of player.conky
  sed -i "/    alignment = '/c\    alignment = '$cwalignment'," ./player.conky
  sed -i "/    gap_x = /c\    gap_x = $cwgapx," ./player.conky
  sed -i "/    gap_y = /c\    gap_y = $cwgapy," ./player.conky
# Calculate position of vu-meters.conky and place it exactly over player.conky
  posi=$(grep 'alignment' "./player.conky" | xargs | cut -d' ' -f3 | sed s/,*$//g)
  gapx=$(grep 'gap_x' "./player.conky" | xargs | cut -d' ' -f3 | sed s/,*$//g)
  gapy=$(grep 'gap_y' "./player.conky" | xargs | cut -d' ' -f3 | sed s/,*$//g)
  minw=$(grep 'minimum_width' "./player.conky" | xargs | cut -d' ' -f3 | sed s/,*$//g)
  minh=$(grep 'minimum_height' "./player.conky" | xargs | cut -d' ' -f3 | sed s/,*$//g)
  sed -i "/    alignment = '/c\    alignment = '$posi'," ./vu-meters.conky
  sed -i "/    gap_x = /c\    gap_x = $gapx," ./vu-meters.conky
  sed -i "/    gap_y = /c\    gap_y = $gapy," ./vu-meters.conky
  sed -i "/    minimum_width = /c\    minimum_width = $minw," ./vu-meters.conky
  sed -i "/    minimum_height = /c\    minimum_height = $minh," ./vu-meters.conky
  if [ "${mouseevents,,}" = "lua" ]; then
     sed -i "/    lua_load = /c\    lua_load = './image.lua ./vu-meters.lua ./click_events.lua'," ./vu-meters.conky
     sed -i "/    lua_mouse_hook = /c\    lua_mouse_hook = 'mouse_events'," ./vu-meters.conky
  elif [ "${mouseevents,,}" = "bash" ]; then
     sed -i "/    lua_load = /c\    lua_load = './image.lua ./vu-meters.lua'," ./vu-meters.conky
     sed -i "/    lua_mouse_hook = /c\    lua_mouse_hook = ''," ./vu-meters.conky
  else
     echo -e "\e[31mERROR:\e[m [player.ini] Wrong mouse events value !" >/dev/stderr
     exit 1
  fi


# Change needle color in vu-meters.lua
  sed -i "/  local needle_color =/c\  local needle_color = 0x$vuneedlecolor                 -- color of indicator needle (default orange) ---" ./vu-meters.lua
# Change needle alpha in vu-meters.lua
  if [ "$vuneedlealpha" != "" ]; then
     sed -i "/  local needle_alpha =/c\  local needle_alpha = $vuneedlealpha" ./vu-meters.lua
  else
     sed -i "/  local needle_alpha =/c\  local needle_alpha = 1" ./vu-meters.lua
  fi
# Change needle length in vu-meters.lua
  if [ "$vuneedlelength" != "" ]; then
     sed -i "/  local needle_width =/c\  local needle_width = $vuneedlelength                       -- length of needle --" ./vu-meters.lua
  else
     sed -i "/  local needle_width =/c\  local needle_width = 55                       -- length of needle --" ./vu-meters.lua
  fi
# Change needle radius in vu-meters.lua
  if [ "$vuneedleradius" != "" ]; then
     sed -i "/  local needle_radius =/c\  local needle_radius = $vuneedleradius                      -- bottom of needle rests here --" ./vu-meters.lua
  else
     sed -i "/  local needle_radius =/c\  local needle_radius = 30                      -- bottom of needle rests here --" ./vu-meters.lua
  fi
# Change needle thickness in vu-meters.lua
  if [ "$vuneedlethickness" != "" ]; then
     sed -i "/  local needle_thickness =/c\  local needle_thickness = $vuneedlethickness                   -- give needle a thickness (actually width?) so it is visible --" ./vu-meters.lua
  else
     sed -i "/  local needle_thickness =/c\  local needle_thickness = 0.02                   -- give needle a thickness (actually width?) so it is visible --" ./vu-meters.lua
  fi
# Change needle start angle in vu-meters.lua
  if [ "$vuneedlestartangle" != "" ]; then
     sed -i "/  local start_angle =/c\  local start_angle = $vuneedlestartangle                   -- note: 0 is at \"3 o-clock\"... example: to start at top/12 o-clock use -90 --" ./vu-meters.lua
  else
     sed -i "/  local start_angle =/c\  local start_angle = -135                   -- note: 0 is at \"3 o-clock\"... example: to start at top/12 o-clock use -90 --" ./vu-meters.lua
  fi
# Change needle end angle in vu-meters.lua
  if [ "$vuneedleendangle" != "" ]; then
     sed -i "/  local end_angle =/c\  local end_angle = $vuneedleendangle                   -- note: 0 is at \"3 o-clock\"... example: to start at top/12 o-clock use -90 --" ./vu-meters.lua
  else
     sed -i "/  local end_angle =/c\  local end_angle = -45" ./vu-meters.lua
  fi
# Change time bar colors in time-bar.lua
  sed -i "/{ -- time bar/,/}, -- time bar/s/           fg_colour=.*$/           fg_colour={0x$timebarcolor,$timebaralpha},/" ./time-bar.lua
  sed -i "/{ -- time bar/,/}, -- time bar/s/           bg_colour=.*$/           bg_colour={0x$timebarbgcolor,$timebarbgalpha},/" ./time-bar.lua
# Change time bar blocks in time-bar.lua
  if [ "$timebarblocks" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           blocks=.*$/           blocks=$timebarblocks,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           blocks=.*$/           blocks=29,/" ./time-bar.lua
  fi
# Change time bar block spaces in time-bar.lua
  if [ "$timebarblockspaces" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           space=.*$/           space=$timebarblockspaces,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           space=.*$/           space=1.2,/" ./time-bar.lua
  fi
# Change time bar block width in time-bar.lua
  if [ "$timebarblockwidth" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           width=.*$/           width=$timebarblockwidth,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           width=.*$/           width=4,/" ./time-bar.lua
  fi
# Change time bar block height in time-bar.lua
  if [ "$timebarblockheight" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           height=.*$/           height=$timebarblockheight,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           height=.*$/           height=4,/" ./time-bar.lua
  fi
# Change time bar pos x in time-bar.lua
  if [ "$timebarposx" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           x=.*$/           x=$timebarposx,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           x=.*$/           x=195,/" ./time-bar.lua
  fi
# Change time bar pos y in time-bar.lua
  if [ "$timebarposy" != "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           y=.*$/           y=$timebarposy,/" ./time-bar.lua
  else
     sed -i "/{ -- time bar/,/}, -- time bar/s/           y=.*$/           y=86,/" ./time-bar.lua
  fi
# Change time bar skew in time-bar.lua
  if [ "${timebarskew,,}" = "off" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           skew_x=.*$/           skew_x=0,/" ./time-bar.lua
     sed -i "/{ -- time bar/,/}, -- time bar/s/           skew_y=.*$/           skew_y=0,/" ./time-bar.lua
     sed -i "/{ -- time bar/,/}, -- time bar/s/           angle=.*$/           angle=90,/" ./time-bar.lua
  elif [ "${timebarskew,,}" = "on" ] || [ "$timebarskew" = "" ]; then
     sed -i "/{ -- time bar/,/}, -- time bar/s/           skew_x=.*$/           skew_x=20,/" ./time-bar.lua
     sed -i "/{ -- time bar/,/}, -- time bar/s/           skew_y=.*$/           skew_y=10,/" ./time-bar.lua
     sed -i "/{ -- time bar/,/}, -- time bar/s/           angle=.*$/           angle=109,/" ./time-bar.lua
  fi

# Change volume bar colors in time-bar.lua
  sed -i "/{ -- volume bar/,/}, -- volume bar/s/           fg_colour=.*$/           fg_colour={0x$volumebarcolor,$volumebaralpha},/" ./time-bar.lua
  sed -i "/{ -- volume bar/,/}, -- volume bar/s/           bg_colour=.*$/           bg_colour={0x$volumebarbgcolor,$volumebarbgalpha},/" ./time-bar.lua
# Change volume bar cap in time-bar.lua
  if [ "$volumebarcap" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           cap=.*$/           cap=\"$volumebarcap\",/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           cap=.*$/           cap=\"b\",/" ./time-bar.lua
  fi
  if [ "$volumebarblocks" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           blocks=.*$/           blocks=$volumebarblocks,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           blocks=.*$/           blocks=8,/" ./time-bar.lua
  fi
# Change volume bar block spaces in time-bar.lua
  if [ "$volumebarblockspaces" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           space=.*$/           space=$volumebarblockspaces,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           space=.*$/           space=2,/" ./time-bar.lua
  fi
# Change volume bar block width in time-bar.lua
  if [ "$volumebarblockwidth" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           width=.*$/           width=$volumebarblockwidth,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           width=.*$/           width=4.5,/" ./time-bar.lua
  fi
# Change volume bar block height in time-bar.lua
  if [ "$volumebarblockheight" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           height=.*$/           height=$volumebarblockheight,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           height=.*$/           height=3,/" ./time-bar.lua
  fi
# Change volume bar pos x in time-bar.lua
  if [ "$volumebarposx" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           x=.*$/           x=$volumebarposx,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           x=.*$/           x=269,/" ./time-bar.lua
  fi
# Change volume bar pos y in time-bar.lua
  if [ "$volumebarposy" != "" ]; then
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           y=.*$/           y=$volumebarposy,/" ./time-bar.lua
  else
     sed -i "/{ -- volume bar/,/}, -- volume bar/s/           y=.*$/           y=164,/" ./time-bar.lua
  fi

sleep 1



# Get all *.conky files into array
  while read -r files
  do
      conkies+=("${files##*/}")
  done< <(find "$workdir" -maxdepth 1 -type f -name "*.conky" | sort)


# Number of conkies
  for index in ${!conkies[@]}; do
      nr_conkies=$(echo "$index")
  done
  nr_conkies=$((nr_conkies+1))


# Load all conkies
z=1
function load_conkies() {
    echo -e "\n\e[33m  -- Conky Musicplayer v$(cat ./player.conky | grep 'version=' | awk -F'=' '{print $2}') -- \n\e[m"
    touch "./tempfiles/load_conkies"
    for conky in "${conkies[@]}"
    do
        echo -e "loading: $z/$nr_conkies  \e[32m$conky\e[m"
        if [ "$z" -lt "$nr_conkies" ]; then
          conky -c "$conky" &
          sleep 1
        else
          conky -c "$conky"
          ./stop
        fi
      z=$((z+1))
    done
    z=1
}


# Kill all conkies
function kill_conkies() {
   ./stop
}

if [ "${mouseevents,,}" = "bash" ]; then
   ./click_events goclickevents & >/dev/stderr
fi

load_conkies
