#!/bin/sh
# $Log: transfer_play.in,v $
# Revision 1.10  2023-01-04 23:41:33+05:30  Cprogrammer
# fixed process_cmdline
#
# Revision 1.9  2022-11-25 21:17:02+05:30  Cprogrammer
# added --clearsrc, --cleardst to clear current playlist on source, destination
#
# Revision 1.8  2022-10-21 13:45:26+05:30  Cprogrammer
# added command line options --play, --no-rm
#
# Revision 1.7  2022-06-20 01:07:37+05:30  Cprogrammer
# use directories set in ./configure
#
# Revision 1.6  2021-02-14 18:43:07+05:30  Cprogrammer
# display list of valid hosts on wrong input
#
# Revision 1.5  2020-09-05 12:32:42+05:30  Cprogrammer
# clear playlist before adding
#
# Revision 1.4  2020-08-30 10:57:30+05:30  Cprogrammer
# use state_file definition in mpd.conf to figure out state_file directory
#
# Revision 1.3  2020-08-11 22:23:23+05:30  Cprogrammer
# use mpc to play and remove playlist
#
# Revision 1.2  2020-07-23 01:47:58+05:30  Cprogrammer
# removed incorrect check for tohost
#
# Revision 1.1  2020-07-22 15:50:47+05:30  Cprogrammer
# Initial revision
#
#
# $Id: transfer_play.in,v 1.10 2023-01-04 23:41:33+05:30 Cprogrammer Exp mbhangui $
#

get_mpd_conf_value()
{
    grep "^$1" /etc/mpd.conf|awk '{print $2}' | \
    sed -e 's{"{{g'
}

get_mpd_state()
{
	(
	do_print=0
	cat $state_dir/mpd_state.$1 | while read line
	do
		if [ " $line" = " playlist_begin" ] ; then
			do_print=1
			continue
		fi
		if [ " $line" = " playlist_end" ] ; then
			break
		fi
		if [ $do_print -eq 1 ] ; then
			echo $line
		fi
	done
	) | sed -e 's{^.*:{{'
}

usage()
{
if [ -f /usr/bin/less ] ; then
	MORE=/usr/bin/less
else
	MORE=/usr/bin/more
fi
$MORE 1>&2 <<EOF
Usage: transfer_play [OPTION] source destination

Known values for OPTION are:

--play
  transfer and start playing current playlist on destination

--clearsrc
  clear current playlist from source

--cleardst
  clear current playlist from destination
EOF
exit $1
}

process_cmdline()
{
while test $# -gt 0; do
	case "$1" in
	-*=*)
	optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
	optval=`echo "$1" | cut -d'=' -f1`
	prog_args="$prog_args $optval=\"$optarg\""
	;;
	-*\>*)
	optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*>//'`
	optarg=">$optarg"
	optval=`echo "$1" | cut -d'>' -f1`
	prog_args="$prog_args $optval\"$optarg\""
	;;
	-*\<*)
	optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*<//'`
	optarg="<$optarg"
	optval=`echo "$1" | cut -d'<' -f1`
	prog_args="$prog_args $optval\"$optarg\""
	;;
	*)
	optarg=
	prog_args="$prog_args $1"
	;;
	esac

	case "$1" in
	--play)
	do_play=1
	shift
	;;
	--cleardst)
	cleardst=1
	shift
	;;
	--clearsrc)
	clearsrc=1
	shift
	;;
	--help)
	usage 0
	;;
	esac
	if [ $# -eq 2 ] ; then
		srchost=$1
		dsthost=$2
		break
	elif [ $# -lt 2 ] ; then
		usage 0
	fi
done
}

set -e
do_play=0
clearsrc=0
cleardst=0
process_cmdline "$@"
if [ -z "$srchost" -o -z "$dsthost" ] ; then
	echo "Must specify source and destination hosts"
	usage 1
fi
if [ " $dsthost" = " $srchost" ] ; then
	echo "destination cannot be same as source" 1>&2
	usage 1
fi
playlist_dir=$(get_mpd_conf_value playlist_directory)
if [ $? -ne 0 ] ; then
	echo "could not get playlist directory from mpd.conf" 1>&2
	exit 1
fi
music_dir=$(get_mpd_conf_value music_dir)
if [ $? -ne 0 ] ; then
	echo "could not get music directory from mpd.conf" 1>&2
	exit 1
fi

state_file=$(get_mpd_conf_value state_file)
state_dir=$(dirname $state_file)
if [ ! -f $state_dir/mpd_state.$srchost ] ; then
	echo "No such host $srchost" 1>&2
	echo "List of valid hosts"
	ls $state_dir/mpd_state.*|cut -d. -f2
	exit 1
fi

out=$(mktemp -t mpdevXXXXXXXXXX)
# get current playlist from $srchost
get_mpd_state $srchost > $out.m3u

if [ -s $out.m3u ] ; then
	mv $out.m3u $playlist_dir

	# clear current playlist on $dsthost
	if [ $cleardst -eq 1 ] ; then
		mpc --host="$dsthost" clear
	fi
	# create current playlist from $outb
	outb=$(basename $out)
	mpc --host=$dsthost load $outb
	# delete playlist $outb.m3u from playlist directory
	mpc --host=$dsthost rm $outb

	if [ $do_play -eq 1 ] ; then
		# start playing current playlist on $dsthost
		mpc --host=$dsthost play
	fi

	# clear current playlist on $srchost
	if [ $clearsrc -eq 1 ] ; then
		mpc --host="$srchost" clear
	fi
	/bin/rm -f $out
else
	/bin/rm -f $out $out.m3u
	echo "empty current playlist on source" 1>&2
	exit 1
fi
