#!/bin/bash
#
# A script for Controlling Exaile
# Copyright (C) 2007  Nicholas Omann <Nicholas.Omann@member.fsf.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

#  README:
#
#  1. Installing
#    First, use: chmod +x eControl to make it executable
#          Also: chmod 755 /usr/bin/eControl
#
#    Second, find a place to store eControl (option a is recommended).
#      a) place it in your /usr/bin
#        -This will allow you to click on it at any time in order to use it
#        -Will be used in examples
#
#      b) call it using /path/to/file/eControl
#        -This way you must always call it using the path
#      
#  2. Using eControl
#
#    a)Running from XChat - adding user commands
#      You can run eControl from XChat in many ways. The most simple way
#      is to create a user command for it. To do that click on Settings->
#      Advanced -> User Commands... Click add new command. At which point
#      the following commands are recommended:
#      exec -o eControl playing color
#      exec eControl next
#      exec eControl play
#      You will place these in the 2nd box on the bottom. The name you put
#      in the first box will be able to be used to call teh command then
#      if you type /name (name being whatever name you chose).
#      
#    b)Running from XChat - without user commands
#      You can choose not to create user commands. If you then want to run
#      eControl you will have to type the whole thing every time.


VERSION="1.0"
AUTHOR="Nicholas Omann"

# Color choices:
#  red "\000321"  
#  blue "\000350"
#  green "\000309"

TEXT_COLOR_1="\000350"  
TEXT_COLOR_2="\000321" 

# Configures dbus to make method calls
DBUS="dbus-send --type=method_call --dest=org.exaile.DBusInterface /DBusInterfaceObject"
#Configures dbus to print replies
DBUSR="dbus-send --print-reply --type=method_call --dest=org.exaile.DBusInterface /DBusInterfaceObject"

#Tests to make sure that Exaile is actually running(avoids spamming dbus errors
EXAILE_TEST="$(ps -F -u $(whoami) -U $(whoami) | grep exaile | grep -v grep)"
if [ "$EXAILE_TEST" == "" ] ; then
  echo "Exaile is not on."
  exit 0
fi

# Start of case statment that does all the work
case "$1" in
  help) #displays help
    echo "Type eControl [command]"
    echo "Commands:"
    echo "          next"
    echo "          play (also pauses)"
    echo "          playing [time] [rating] [color]"
    echo "          prev"
    echo "          rate [1..8]"
    echo "          version"
    echo "          volume [up,down] [1..100]"
  ;;
  version) # Displays Verion information
    echo "Version: $VERSION"
    echo "Made by: $AUTHOR"
  ;;
  volume)  # Changes volume (Defaults to increments of 10)
    if [ "$2" = "up" ] ; then
      if [ "$3" = "" ]
        then
          INC=10
        else
          INC=$3
      fi
        NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.increase_volume int32:$INC)"
    fi
    
    if [ "$2" = "down" ] ; then
      if [ "$3" = "" ] 
        then
          INC=10
        else
          INC=$3
      fi
        NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.decrease_volume int32:$INC)"  
    fi     
  ;;
   play) #plays but if playing pauses
    NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.play_pause)"
  ;;
  next) #Plays next song
    NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.next_track)"
  ;;
  prev) #Plays previous song
    NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.prev_track)"
  ;;
  rate)
    NULLCOMMANDOUTPUT="$($DBUS org.exaile.DBusInterface.set_rating int32:$2)"
  ;;
  playing)  #echo "is listening to $TITLE by $ARTIST from $ALBUM [$INDEX/$TOTAL] [Exaile v$EXAILE_VERSION]"
    TIME="$($DBUSR org.exaile.DBusInterface.current_position)"
    TIME=${TIME##*byte\ }
    MIN=0
    if (( $TIME >= 60 )) ; then
      while (( TIME >= 60)) ; do 
        let "MIN += 1"
        let "TIME -= 60"
      done
    fi
    TIMEF="$MIN:$TIME"
    TOTAL_TIME="$($DBUSR org.exaile.DBusInterface.get_length)"
    TOTAL_TIME=${TOTAL_TIME#*\"}
    TOTAL_TIME=${TOTAL_TIME%\"}
    ALBUM="$($DBUSR org.exaile.DBusInterface.get_album | grep \")"
    ALBUM=${ALBUM#*\"}
    ALBUM=${ALBUM%\"}
    ARTIST="$($DBUSR org.exaile.DBusInterface.get_artist)"
    ARTIST=${ARTIST#*\"}
    ARTIST=${ARTIST%\"}
    TITLE="$($DBUSR org.exaile.DBusInterface.get_title)"
    TITLE=${TITLE#*\"}
    TITLE=${TITLE%\"}
    RATING="$($DBUSR org.exaile.DBusInterface.get_rating)"
    RATING=${RATING##*int32\ }
    TOTAL="8"
    EXAILE_VERSION="$($DBUSR org.exaile.DBusInterface.get_version)"
    EXAILE_VERSION=${EXAILE_VERSION#*\"}
    EXAILE_VERSION=${EXAILE_VERSION%\"}
	  
	  #color options
	  if [ "$2" = "color" -o "$3" = "color" -o "$4" = "color" ] ; then
	    COLOR_1="$TEXT_COLOR_1"
	    COLOR_2="$TEXT_COLOR_2"
	  else
	    COLOR_1=""
	    COLOR_2=""
	  fi

	  OUTPUT="${COLOR_1}is listening"	  
	  if [ -n "$TITLE" ] ; then
	    OUTPUT="$OUTPUT to ${COLOR_2}$TITLE"
	  fi
	  if [ -n "$ARTIST" ] ; then
	    OUTPUT="$OUTPUT ${COLOR_1}by ${COLOR_2}$ARTIST"
	  fi
	  if [ -n "$ALBUM" ] ; then
	    OUTPUT="$OUTPUT ${COLOR_1}from ${COLOR_2}$ALBUM"
	  fi
	  if [ "$2" = "time" -o "$3" = "time" -o "$4" = "time" ] ; then
	    OUTPUT="$OUTPUT [$TIMEF/$TOTAL_TIME]"
	  fi
	  if [ "$2" = "rating" -o "$3" = "rating" -o "$4" = "rating" ] ; then
	    OUTPUT="$OUTPUT [$RATING/$TOTAL]"
	  fi
	  
	  echo -e "$OUTPUT ${COLOR_1}[Exaile v$EXAILE_VERSION]"
  ;;
  *) # Default case for invalid commands
    echo "Error - invalid command supplied to eControl try help"
  ;;
esac
exit 0

