#!/bin/bash
# player.sh v1.5
#   v1.5 -- Added "stopafter" direct control function; stops player after a delay.
# A small script to control Amarok via a terminal; especially useful over ssh connections.
# Copyright (C) 2008 Adarsh Carter
# Published under the conditions of the GNU GPL.

COMMAND="dcop --user $USER amarok player"

DO_PREV="$COMMAND prev"
DO_NEXT="$COMMAND next"
DO_PLAY="$COMMAND play"
DO_PAUSE="$COMMAND pause"
DO_STOP="$COMMAND stop"
DO_STATUS="$COMMAND status"
DO_TITLE="$COMMAND title"
DO_ALBUM="$COMMAND album"
DO_ARTIST="$COMMAND artist"
DO_YEAR="$COMMAND year"
DO_CURRENTTIME="$COMMAND currentTime"
DO_TOTALTIME="$COMMAND totalTime"
DO_CURRENTTIME_SECONDS="$COMMAND trackCurrentTime"
DO_TOTALTIME_SECONDS="$COMMAND trackTotalTime"

# Display Information
display_info() {
	echo " "`$DO_ARTIST`"                                    "
	echo " \""`$DO_TITLE`\" \(`$DO_YEAR`\)"                                    "
	echo "  "`$DO_ALBUM`"                                    "
	}

# CLI Interface for Player Control
full_control() {
	clear
	tput cup 5 0
	echo "--------------------------------------------"
	echo -e " <<\t>\t>>\tx\t\tquit"
	echo -e " p\tb\tn\ts\t\tq"
	# Loop until user terminates.
	while [ 1 ]; do
		tput cup 0 0
		display_info
		echo "--------------------------------------------"
		current=`$DO_CURRENTTIME_SECONDS`
		total=`$DO_TOTALTIME_SECONDS`
		remain_minutes=$(( ( $total - $current ) / 60 ))
		remain_seconds=$(( ( $total - $current ) % 60 ))
		echo " -"$remain_minutes\:`date --date "00:00:$remain_seconds" +%S` \(`$DO_CURRENTTIME`\/`$DO_TOTALTIME`\)"                                    "
		read -n 1 -s -t 1 selection
		# Available commands for player control.
		case "$selection" in
			"p")
				$DO_PREV
				;;
			"b")
				if [ `$DO_STATUS` -eq 2 ]; then
					$DO_PAUSE
				else
					$DO_PLAY
				fi
				;;
			"s")
				$DO_STOP
				;;
			"n")
				$DO_NEXT
				;;
			"q")
				exit
				;;
			*)
				;;
		esac
		selection="nothing"
	done
	}

# Stop after $1.
delayed_stop() {
	sleep $1
	$COMMAND stop
	}

# Usage Note
usage() {
	echo -e "Usage:\t`basename $0` [prev|pause|play|next|stop|info|control|stopafter time[s|m|h]]"
	echo -e "\tBy default `basename $0` displays information (explicitly with \"info\" command)."
	echo -e "\tFor a CLI media controller, send the \"control\" command."
	}

# Main
# Default action.
if [ -z $1 ] || [ $1 == "info" ]; then
	display_info
# Special commands.
elif [ $1 == "stopafter" ]; then
	# Check time for stop after; if nothing, throw error and exit.
	# Note:  does not check for garbage after the number (e.g., 23adfsd).
	if [ ! -z $2 ] ; then
		clean=`echo $2 | tr -d 'a-ce-gi-ln-z[:upper:][:punct:]'`
		case "$clean" in
			[a-zA-Z]*)
				usage
				;;
			*)
				number=(`echo $clean | tr '[:alpha:]' ' '`)
				letter=(`echo $clean | tr '[:digit:]' ' '`)
				unit=`echo $letter | cut -c 1`
				if [ -z $unit ]; then
					unit="s"
				fi
				echo Will stop player in $number$unit.
				delayed_stop $number$unit &
				;;
		esac
	else
		usage
	fi
# Direct control.
elif [ $1 == "prev" ] || [ $1 == "pause" ] || [ $1 == "play" ] || [ $1 == "next" ] || [ $1 == "stop" ]; then
	# If player is paused and user selects pause, unpause.
	if [ `$DO_STATUS` -eq 1 ] && [ $1 == "pause" ]; then
		$COMMAND play
	# Otherwise, send command.
	else
		$COMMAND $1
	fi
# Load player control interface.
elif [ $1 == "control" ]; then
	full_control
# Help command displays usage.
elif [ $1 == "help" ] || [ $1 == "--help" ]; then
	usage
# If unknown command, display usage.
else
	usage
fi

