#!/bin/sh
#
# mp3play script for Linux (running bash) modified
# by Samuli Kaski on the 1. Feb 97, hope you enjoy it.
# 

# Paths used by the script, edit if necessary

MP3DIR=/usr/local/bin
TMPDIR=/tmp

# Check for dirs and approriate files

if [ ! -d $TMPDIR ]; then
	echo "The temporary directory $TMPDIR doesn't exist."
	exit
fi

if [ ! -w $TMPDIR ]; then
	echo "The temporary directory $TMPDIR isn't writeable to you."
	exit
fi

if [ ! -x $MP3DIR/l3dec ]; then
	echo "$MP3DIR/l3dec doesn't exist or has wrong access permissions."
	exit
fi

if [ ! -x $MP3DIR/mkmp3head ]; then
        echo "$MP3DIR/mkmp3head doesn't exist or has wrong access permissions."
        exit
fi

if [ ! -x $MP3DIR/wavplay ]; then
        echo "$MP3DIR/wavplay doesn't exist or has wrong access permissions."
        exit
fi

# Check who is running me

SHELLUSER=`whoami` 

if [ $SHELLUSER = "root" ]; then
	DOBOOST=YES
else
	echo "WARNING!"
	echo "You might want to run mp3play as root to boost"
	echo "it's priority over other applications in order to"
	echo "keep the sound from breaking up."

	sleep 3

	DOBOOST=NO
fi

# Strip any options before parsing for mp3 files

while [ $# -ge 1 ]
do
	case $1 in
		-*)     shift; ;;
		*)      break ;;
	esac
done

# Play all files and ignore any spaces between mp3 files

while [ $# -ne 0 ]
do
	case $1 in
		' ') shift; ;;
	esac

	if [ -f $1 ]; then
		if [ -r $1 ]; then
			$MP3DIR/l3dec -wav -fn 0 $1 $TMPDIR/head.in >& /dev/null
			$MP3DIR/mkmp3head $TMPDIR/head.in $TMPDIR/head.out
			/bin/rm -f $TMPDIR/head.in

			if [ $DOBOOST = "YES" ]; then
				nice -n -10 $MP3DIR/l3dec -wav -sto $1 | cat $TMPDIR/head.out - | $MP3DIR/wavplay
			else
				$MP3DIR/l3dec -wav -sto $1 | cat $TMPDIR/head.out - | $MP3DIR/wavplay
			fi

			/bin/rm -f $TMPDIR/head.out
		else
			echo "$1 isn't readable to you."
		fi
	else
		echo "$1: No such file, check your spelling."
	fi

	shift
done
