#!/bin/bash
#
# Media-Detect by Damon Kaswell
# License: GPL	See http://www.gnu.org/copyleft/gpl.html
# Version: 0.55

# This script detects which media players are running, if any,
# and passes commands to them accordingly.
# If no configured media players are running, it generates
# a menu to allow you to start one.
#
# This script is configurable. Any media application
# that accepts command lines can be added.  Edit the file
# ~/.media-detect.conf or use the command 'media-menu edit'

# USAGE
# The commands this script recognizes are as follows:
# Rewinding:		media-detect previous
# Fast-forwarding:	media-detect next
# Stopping:		media-detect stop
# Pausing/playing:	media-detect play-pause
# Opening an app:	media-detect menu (Deprecated; use "media-menu")
# Editing menu:		media-detect edit (Deprecated; use "media-menu edit")
#
# EXTENDED BUTTON USAGE
# This script can be configured to use 5 additional user-defined commands:
# Button 1:		media-detect extbtn1
# Button 2:		media-detect extbtn2
# Button 3:		media-detect extbtn3
# Button 4:		media-detect extbtn4
# Button 5:		media-detect extbtn5

################################################################################
# Section:      Startup tests                                                  #
# Purpose:      Gets initial parameters and tests if media-menu is needed.     #
################################################################################
CONFIG_FILE=~/.media-detect.conf
TEMP_DIR=/tmp

touch $CONFIG_FILE
if [ ! -s "$CONFIG_FILE" ]
then
	media-menu new
	exit 0
else
	CURRENT_CONFIG=`cat "$CONFIG_FILE"`
fi
if [ "$1" = "menu" -o -z "$1" ]
then
	media-menu
	exit 0
fi
if [ "$1" = "edit" ]
then
	media-menu edit
	exit 0
fi

GET_MIXER=`echo "$CURRENT_CONFIG" | grep -i default_mixer`
GET_CHANNEL=`echo "$CURRENT_CONFIG" | grep -i default_channel`
MIXER_GLOBAL="${GET_MIXER#default_mixer=}"
CHANNEL_GLOBAL="${GET_CHANNEL#default_channel=}"
################################################################################
# Section:      Get running apps                                               #
# Purpose:      Determines which applications are currently running.           #
################################################################################
getAppString ()
{
	echo "$CURRENT_CONFIG" | grep $1 | cut -d" " -f2-30
}
CONFIG_COUNT=`echo "$CURRENT_CONFIG" | grep -c prog`
APPS_NUM=0
APPS_TOTAL=0
APPS_MEM=`ps -ef`
until [ "$APPS_NUM" = "$CONFIG_COUNT" -o "$APPS_TOTAL" = "1" ]
do
	let "APPS_NUM+=1"
	SEARCH_STRING="`getAppString prog$APPS_NUM`"
	if (echo "$APPS_MEM" | grep $SEARCH_STRING | grep -vq xinetd)
	then
		PROG_NUM="$APPS_NUM"
		let "APPS_TOTAL+=1"
	fi
done
if [ "$APPS_TOTAL" = "0" ]
then
	if [ "$1" = "volup" ]
	then
		aumix -d $MIXER_GLOBAL -$CHANNEL_GLOBAL +5
		exit 0
	elif [ "$1" = "voldown" ]
	then
		aumix -d $MIXER_GLOBAL -$CHANNEL_GLOBAL -5
		exit 0
	elif [ "$1" = "mute" ]
	then
		if [ -e ~/.mdmute ]
		then
			rm ~/.mdmute
			aumix -L 2>>/dev/null
			exit 0
		else
			aumix -S
			aumix -d $MIXER_GLOBAL -$CHANNEL_GLOBAL 0
			touch ~/.mdmute
			exit 0
		fi
	else
		media-menu menu
		exit 0
	fi
fi

################################################################################
# Section:      Check DCOP command                                             #
# Purpose:      Checks to see if the program was configured as a DCOP app.     #
################################################################################
APP_TYPE="`echo "$CURRENT_CONFIG" | grep type$PROG_NUM | cut -d" " -f2-2`"
if [ "$APP_TYPE" = "dcop" ]
then
	DCOP_PROG=`dcop | grep $SEARCH_STRING`
	PRE_CMD="dcop $DCOP_PROG "
else
	PRE_CMD=""
fi
################################################################################
# Section:      Create app commands                                            #
# Purpose:      Gets the application command information.                      #
################################################################################

MIXER="`getAppString mixr$PROG_NUM`"
if [ "$MIXER" = "default" ]
then
	MIXER="$MIXER_GLOBAL"
fi

CHANNEL="`getAppString chnl$PROG_NUM`"
if [ "$CHANNEL" = "default" ]
then
	CHANNEL="$CHANNEL_GLOBAL"
fi

case $1 in
	"previous")BASE_CMD="`getAppString prev$PROG_NUM`";ALTS_CMD="`getAppString aprv$PROG_NUM`";;
	"next")BASE_CMD="`getAppString next$PROG_NUM`";ALTS_CMD="`getAppString anxt$PROG_NUM`";;
	"stop")BASE_CMD="`getAppString stop$PROG_NUM`";ALTS_CMD="`getAppString astp$PROG_NUM`";;
	"play-pause")BASE_CMD="`getAppString plps$PROG_NUM`";ALTS_CMD="`getAppString apls$PROG_NUM`";;
	"extbtn1")BASE_CMD="`getAppString 1btn$PROG_NUM`";;
	"extbtn2")BASE_CMD="`getAppString 2btn$PROG_NUM`";;
	"extbtn3")BASE_CMD="`getAppString 3btn$PROG_NUM`";;
	"extbtn4")BASE_CMD="`getAppString 4btn$PROG_NUM`";;
	"extbtn5")BASE_CMD="`getAppString 5btn$PROG_NUM`";;
	"volup")aumix -d $MIXER -$CHANNEL +5;exit 0;;
	"voldown")aumix -d $MIXER -$CHANNEL -5;exit 0;;
	"mute")if [ -e ~/.mdmute ]
		then
			rm ~/.mdmute
			aumix -L 2>>/dev/null
			exit 0
		else
			aumix -S
			aumix -d $MIXER_GLOBAL -$CHANNEL_GLOBAL 0
			touch ~/.mdmute
			exit 0
		fi;;
	*)exit 1;;
esac

################################################################################
# Section:      Check alternates                                               #
# Purpose:      Determines if we should use an alternate command.              #
################################################################################
if [ -n "$ALTS_CMD" ]
then
	if [ -e $TEMP_DIR/mdlock$PROG_NUM$USER.tmp ]
	then
		rm $TEMP_DIR/mdlock$PROG_NUM$USER.tmp
		BASE_CMD="$ALTS_CMD"
	else
		touch $TEMP_DIR/mdlock$PROG_NUM$USER.tmp
	fi
fi

################################################################################
# Section:      Run command                                                    #
# Purpose:      Runs the configured command                                    #
################################################################################
$PRE_CMD$BASE_CMD
exit 0
