#!/bin/sh
#
# audiowrapperplugin
#
# helper script for mozilla audio plugins which play via an external program
#
# (c) 2005, 2006 by Dirk Jagdmann <doj@cubic.org>

PLAYER=audacious
TMP=/dev/shm

player()
{
  if [ "$PLAYER" = "xmms" -o "$PLAYER" = "audacious" ] ; then
    $PLAYER -s
    $PLAYER "$@"
  else
    $PLAYER "$@"
  fi
}

# check if TMP exists
if [ ! -d $TMP -o ! -w $TMP ]
then TMP=/tmp
fi

# check command line args
if [ $# -ge 2 -a "$1" = "-s" ]
then echo $2 > $TMP/audioapp
     exit 0
fi

if [ $# -ge 2 -a "$1" = "-d" ]
then rm -f $TMP/audioapp
     exit 0
fi

# get player application
if [ -f $TMP/audioapp ]
then PLAYER=`cat $TMP/audioapp`
fi

# construct valid absolute filename
WD=$PWD
cd /
FILE=$1
if [ ! -f $FILE ]
then FILE=$WD/$FILE
     if [ ! -f $FILE ]
     then logger "audiowrapperplugin: could not find $1"
          exit 1
     fi
fi

# check if player is already running
if ! pgrep $PLAYER
then ($PLAYER 2>&1 | logger) &
     sleep 5
fi

# determine file type and call player
logger "audiowrapperplugin: $@ -> $FILE"

# filetypes we can determine with grep
if grep '#EXTM3U' $FILE &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.m3u
     logger "audiowrapperplugin: open as m3u"
     player $TMP/audiowrapperplugin.m3u
     exit 0
fi

if grep '\[playlist\]' $FILE &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.pls
     logger "audiowrapperplugin: open as pls"
     player $TMP/audiowrapperplugin.pls
     exit 0
fi

# filetypes we can determine with file
F=$TMP/audiowrapperplugin.file
file $FILE > $F

if grep MPEG $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.mp3
     logger "audiowrapperplugin: open as mp3"
     player $TMP/audiowrapperplugin.mp3
     exit 0
fi

if grep "Vorbis audio" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.ogg
     logger "audiowrapperplugin: open as ogg"
     player $TMP/audiowrapperplugin.ogg
     exit 0
fi

if grep "PlaySID" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.sid
     logger "audiowrapperplugin: open as sid"
     player $TMP/audiowrapperplugin.sid
     exit 0
fi

if grep "RIFF" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.wav
     logger "audiowrapperplugin: open as wav"
     player $TMP/audiowrapperplugin.wav
     exit 0
fi

if grep "Sun/NeXT audio data" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.au
     logger "audiowrapperplugin: open as au"
     player $TMP/audiowrapperplugin.au
     exit 0
fi

if grep "Fasttracker II module sound data" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.xm
     logger "audiowrapperplugin: open as xm"
     player $TMP/audiowrapperplugin.xm
     exit 0
fi

if grep "Protracker module sound data" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.mod
     logger "audiowrapperplugin: open as mod"
     player $TMP/audiowrapperplugin.mod
     exit 0
fi

if grep "ScreamTracker III Module sound data" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.s3m
     logger "audiowrapperplugin: open as s3m"
     player $TMP/audiowrapperplugin.s3m
     exit 0
fi

if grep "Impulse Tracker module sound data" $F &> /dev/null
then ln -fs $FILE $TMP/audiowrapperplugin.it
     logger "audiowrapperplugin: open as it"
     player $TMP/audiowrapperplugin.it
     exit 0
fi

# generic filetype
logger "audiowrapperplugin: open generic"
player "$@"
