#!/bin/bash

###############################################################################
#    streamripagent - starts streamripper depending on the track currently    #
#                     streamed in amarok                                      #
#    Copyright (C) 2007 Arno Rehn <arno@arnorehn.de>                          #
#                                                                             #
#    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 3 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.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
###############################################################################

############
# settings #
############

# where to save the ripped files (default: "$HOME/streamripagent")
TARGET_DIR="$HOME/streamripagent"

# interval of checking whether to start ripping (in seconds) (default: 1)
# note: 0 isn't a good idea since there might occur problems when tuning into a stream
INTERVAL=1

# how long to wait after the title has changed
# (to make sure everything has made it into the file) (in seconds) (default: 5)
WAIT_AFTER_TITLE_CHANGED=5

# whether to use kdialog (values: "true" or "false") (default: "true")
KDIALOG_ENABLE="true"

####################
# helper functions #
####################

# returns 0 if amarok is running, else 1
_isAmarokRunning() {
	ps -e | grep amarokapp > /dev/null && return 0
	return 1
}

# prints the protocol of the current track playing in amarok to stdout
_protocol() {
	dcop amarok player encodedURL | sed "s/:\/.*//" && return 0
	return 1
}

# prints the track currently playing in amarok to stdout
_currentTrack() {
	dcop amarok player title && return 0
	return 1
}

# prints the url of the current track to stdout
_url() {
	dcop amarok player encodedURL && return 0
	return 1
}

# returns 0 if the track should be ripped, else 1
# arguments:
#   1: track name
#      If no name is given, _currentTrack is used
_toRip() {
	local TRACKNAME="$1"
	[ -z "$TRACKNAME" ] && TRACKNAME="$(_currentTrack)"
	
	cat ~/.streamripagent | while read -r line; do
		echo "$TRACKNAME" | grep -i "$line" > /dev/null && return 0
	done && return 0
	return 1
}

# starts ripping of the current track
# arguments:
#   1: url
#      If no url is given, _url is used
_rip() {
	local URL="$1"
	[ -z "$URL" ] && URL="$(_url)"
	
	local RIPPING="$(_currentTrack)"
	
	touch "/tmp/streamripagent/$RIPPING.lock" # create a lock file
	
	streamripper "$(_url)" -A -a "$TARGET_DIR/$RIPPING.mp3" > /dev/null 2>&1 &
	local STREAMRIPPER_PID=$! # save the process id
	
	echo "Ripping $RIPPING to $TARGET_DIR/$RIPPING.mp3"
	[ "$KDIALOG_ENABLE" = "true" ] && kdialog --msgbox "Ripping $RIPPING to $TARGET_DIR/$RIPPING.mp3" & # display nice info
	
	while [ "$RIPPING" = "$(_currentTrack)" ]; do
		sleep 0 # do nothing, let it rip
	done
	rm "/tmp/streamripagent/$RIPPING.lock" # remove the lock file
	sleep $WAIT_AFTER_TITLE_CHANGED # wait
	kill "$STREAMRIPPER_PID"
	
	echo "Finished ripping $RIPPING to $TARGET_DIR/$RIPPING.mp3"
	[ "$KDIALOG_ENABLE" = "true" ] && kdialog --msgbox "Finished ripping $RIPPING to $TARGET_DIR/$RIPPING.mp3" & # display nice info
}

###############
# main script #
###############

mkdir -p "$TARGET_DIR"
mkdir -p /tmp/streamripagent
[ ! -e ~/.streamripagent ] && touch ~/.streamripagent

while true; do
	# check whether file should be ripped, it is a not a local file and it isn't currently being ripped
	if _isAmarokRunning && _toRip && [ "$(_protocol)" != "file" -a ! -e "/tmp/streamripagent/$(_currentTrack).lock" ]; then
		_rip & # start ripping
	fi
	sleep $INTERVAL
done
