#!/bin/sh

# initially from coron@ on some Session
# $Id: musicspool.sh,v 1.5 2009/09/20 12:37:27 coron Exp $

# How to configure an lpd to accept remote requests (in a nutshell):
#
# lpd needs a '+' entry in /etc/hosts.lpd to allow printing from
# any remote host.
#
# lpd also needs working DNS entries (reverse(forward(rhost)) ==
# rhost) to accept remote host rhost.

set -eu

# from printcap(5):
# The if filter is invoked with arguments:
# if [-c] -wwidth -llength -iindent -n login -h host acct-file

# we need some of those arguments, so parse them correctly
args=`getopt ch:i:j:l:n:w: $*`
set -- $args

while [ $# -ge 0 ]; do
	case "$1" in
		-c)
			shift;;
		-h)
			host="$2"
			shift; shift;;
		-i)
			shift; shift;;
		-j)
			filename="$2"
			shift; shift;;
		-l)
			shift; shift;;
		-n)
			login="$2"
			shift; shift;;
		-w)
			shift; shift;;
		--)
			shift; break;;
	esac
done

fn=$(mktemp play.XXXXXXXXX)
cat >$fn
type=$(file $fn)

if [ X"${type#*: MP3}" != X"$type" ]; then
	/usr/local/bin/mpg321 -q "$fn"
elif [ X"${type#*: Ogg}" != X"$type" ]; then
	/usr/local/bin/ogg123 -q "$fn"
elif [ X"${filename#*[Mm][Pp]3}" != X"$filename" ]; then
	/usr/local/bin/mpg321 -q "$fn"
fi

rm "$fn"
