#!/bin/sh

# script to help automate generation of config file and startup of ices2,
# mostly useful for people trying to do things like run ices2 from cron.

# contributed by Ciaran Anscomb <ciaran.anscomb@6809.org.uk>
# distributed under GPL, see LICENSE

# 2001-10-20	Use new ices2 savefile feature instead of wget
#		Better time format
# 2001-10-27	Default device according to module selected
# 2001-12-28	Support -mb, -Mb and -q
# 2002-08-10	-b implies <managed>, -q disables that
#		-mb and -MB can now apply to -q
# 2002-08-11	Metadata now defined within <instance>
#		Support resampling!  First -sr will set device samplerate
#		If subsequent -sr differs, inserts <resample> section

# You will probably want to leave this commented out - I need it tho...
#LD_LIBRARY_PATH=/usr/local/ogg/lib
#export LD_LIBRARY_PATH
#PATH=/usr/local/ogg/bin:/usr/ucb:/usr/bin:/usr/bin:/bin
#cd /usr/local/ogg/bin

# Some moderately sensible defaults
samplerate=44100
channels=2
quality=3
module=oss
server=localhost
port=8000
password=hackme
metadatafile=/var/tmp/metadata.$$

cleanup() {
	rm -f $metadatafile
}

trap cleanup 2 15

if [ "x$module" = "xoss" ]; then
	device=/dev/dsp
else
	device=/dev/audio
fi

if [ "x$1" = "x" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
	cat << EOF
run_ices, a script to start ices2 from the command line.
Usage: $0 [OPTION] mountpoint [-o filename] ...
Example: $0 -S localhost -P 8000 -p hackme -b 64000 -o low.ogg
		path/low.ogg -b 128000 -o high.ogg path/high.ogg

General configuration:
  -sr n		Set sample rate of audio device [$samplerate]
  -c n		Set number of channels [$channels]
  -m module	Use named ices module (oss,sun) [$module]
  -d device	Specify audio device [$device]
  -S server	Server to stream to [$server]
  -P port	Port to connect to [$port]
  -p pass	Server password
  -A artist	Artist for encoding
  -T title	Title for encoding
  -C copyright	Set copyright information
  -t hh:mm:ss	Finish encoding after specified time [don't stop]

Per-instance encoding configuration:
  -q n		Set quality 0-10 [$quality]
  -b n		Set nominal bitrate [undefined]
  -mb n		Set minimum bitrate [undefined]
  -Mb n		Set maximum bitrate [undefined]
  -o filename	Write this encoding to a file

When listing more than one mountpoint, you only need to override the
parameters that need changing since the last one.  Multiple encodings
come at the expense of CPU.  Make sure you specify all the parameters
before the mountpoint.

When deciding on the quality, you can either specify a generic quality
setting with -q or nominal, minimum and maximum bitrates with -b, -mb
and -Mb respectively.

EOF
	exit 0
fi

while [ "x$1" != "x" ]; do
	opt=$1; shift
	case $opt in
	-sr) samplerate=$1; shift;
		if [ "x$devsamplerate" = "x" ]; then
			devsamplerate=$samplerate;
		fi
		;;
	-c) channels=$1; shift;
		if [ "x$devchannels" = "x" ]; then
			devchannels=$channels;
		fi
		;;
	-m) module=$1; shift;
		if [ "x$module" = "xoss" ]; then
			device=/dev/dsp
		else
			device=/dev/audio
		fi
		;;
	-d) device=$1; shift; ;;
	-S) server=$1; shift; ;;
	-P) port=$1; shift; ;;
	-p) password=$1; shift; ;;
	-T) title=$1; shift; ;;
	-A) artist=$1; shift; ;;
	-C) copyright=$1; shift; ;;
	-o) savefile=$1; shift; ;;
	-t|-x) time=`echo $1|sed 's/:/ 60*/;s/:/+60*/;s/$/+p/'|dc`; shift; ;;
	-q) quality=$1; shift; bitrate=""; minbitrate=""; maxbitrate="" ;;
	-b) bitrate=$1; shift; quality="" ;;
	-mb) minbitrate=$1; shift; ;;
	-Mb) maxbitrate=$1; shift; ;;
	*) mount=$opt;
		if [ "x$init" = "x" ]; then
			if [ "x$devsamplerate" = "x" ]; then
				devsamplerate=$samplerate;
			fi
			if [ "x$devchannels" = "x" ]; then
				devchannels=$channels;
			fi
			cat > live.xml << EOF
<?xml version="1.0"?>
<ices>
	<background>0</background>
	<logpath>/usr/local/ogg/log</logpath>
	<logfile>ices.log</logfile>
	<loglevel>1</loglevel>

	<stream>
		<input>
			<module>$module</module>
			<param name="rate">$devsamplerate</param>
			<param name="channels">$devchannels</param>
			<param name="device">$device</param>
			<param name="metadata">1</param>
			<param name="metadatafilename">$metadatafile</param>
		</input>
EOF
			init=1
		fi
		cat >> live.xml << EOF
		<instance>
			<metadata>
				<name>$artist - $title</name>
				<genre></genre>
				<description>$copyright</description>
			</metadata>
			<hostname>$server</hostname>
			<port>$port</port>
			<password>$password</password>
			<mount>/$mount</mount>
EOF
		if [ "$samplerate" != "$devsamplerate" ]; then
			cat >> live.xml << EOF
			<resample>
				<in-rate>$devsamplerate</in-rate>
				<out-rate>$samplerate</out-rate>
			</resample>
EOF
		fi
		echo "			<encode>" >> live.xml
		if [ "x$quality" != "x" ]; then
			echo "				<quality>$quality</quality>" >> live.xml
		else
			echo "				<managed>1</managed>" >> live.xml
		fi
		if [ "x$maxbitrate" != "x" ]; then
			echo "				<maximum-bitrate>$maxbitrate</maximum-bitrate>" >> live.xml
		fi
		if [ "x$bitrate" != "x" ]; then
			echo "				<nominal-bitrate>$bitrate</nominal-bitrate>" >> live.xml
		fi
		if [ "x$minbitrate" != "x" ]; then
			echo "				<minimum-bitrate>$minbitrate</minimum-bitrate>" >> live.xml
		fi
		cat >> live.xml << EOF
				<samplerate>$samplerate</samplerate>
				<channels>$channels</channels>
			</encode>
			<savefile>$savefile</savefile>
		</instance>
EOF
		unset savefile
		;;
	esac
done

cat >> live.xml << EOF
	</stream>
</ices>
EOF

cat > $metadatafile << EOF
ORGANIZATION=$artist
DESCRIPTION=$title
COPYRIGHT=$copyright
EOF
ices live.xml &
icespid=$!
sleep 2
#kill -USR1 $icespid
if [ "x$time" != "x" ]; then
	sleep $time
	kill -INT $icespid
else
	wait $icespid
fi
cleanup
