#!/bin/sh

LIST_OF_OSS_APPS="aumix sox"

show_help() {
    echo "setup-pulseaudio [ --enable | --disable | --status ]"
    echo ""
    echo "Modifies configuration files of some applications for PulseAudio"
    echo "  --enable   Enables PulseAudio"
    echo "  --disable  Disables PulseAudio"
    echo "  --status   Shows activation state (disabled or enabled) for PulseAudio"
    echo ""
    echo "You need to be root for this command to succeed"
    echo "You may need to re-login for changes to take effect"
    exit
}

check_root() {
    id=`id -u`
    if [ "x$id" = "x0" ]; then
	true
    else
	echo "You need to be root in order to enable/disable pulseaudio"
	false
    fi
}

PROFNAME=/etc/profile.d/pulseaudio.sh
CPROFNAME=/etc/profile.d/pulseaudio.csh

set_variable () {
    if test -f $PROFNAME &&
	grep -q "export $1"= $PROFNAME; then
	sed -i -e "s|export $1=.*|export $1=$2|g" $PROFNAME
    else
	echo "export $1=$2" >> $PROFNAME
    fi
    if test -f $CPROFNAME &&
	grep -q "setenv $1 " $CPROFNAME; then
	sed -i -e "s|setenv $1 .*|setenv $1 $2|g" $CPROFNAME
    else
	echo "setenv $1 $2" >> $CPROFNAME
    fi
}

delete_variable () {
    if test -f $PROFNAME &&
	grep -q "export $1"= $PROFNAME; then
	sed -i -e "/export $1=.*/d" $PROFNAME
    fi
    if test -f $CPROFNAME &&
	grep -q "setenv $1 " $CPROFNAME; then
	sed -i -e "/setenv $1 .*/d" $CPROFNAME
    fi
}

enable_phonon() {
    echo "Enabling PulseAudio for Phonon..."
    delete_variable PHONON_PULSEAUDIO_DISABLE
}

enable_alsa() {
    echo "Enabling PulseAudio for ALSA..."
    set_variable ALSA_CONFIG_PATH /etc/alsa-pulse.conf
}

enable_libao() {
    echo "Enabling PulseAudio for libao..."
    if test -f /etc/libao.conf; then
	if grep -q "default_driver=pulse" /etc/libao.conf; then
	    echo "Default driver is pulse already in /etc/libao.conf"
	else
	    echo "default_driver=pulse" >> /etc/libao.conf
	fi
    else
	echo "default_driver=pulse" >> /etc/libao.conf
    fi
}

enable_mplayer() {
    echo "Enabling PulseAudio for mplayer..."
    if test -f /etc/mplayer/mplayer.conf; then
	if grep -q '^ao=' /etc/mplayer/mplayer.conf; then
	    perl -pi -e "s|^ao=.*|ao=pulse|g;" /etc/mplayer/mplayer.conf
	else
	    echo "ao=pulse" >> /etc/mplayer/mplayer.conf
	fi
    fi

    # FIXME: mplayerplug-in uses $HOME/.mplayer/mplayerplug-in.conf
}

enable_speechd() {
    echo "Enabling PulseAudio for speech dispatcher..."
    if test -f /etc/speech-dispatcher/speechd.conf; then
	if grep -q 'AudioOutputMethod' /etc/speech-dispatcher/speechd.conf; then
            perl -pi -e "s|^.*AudioOutputMethod .*|AudioOutputMethod \"pulse\"|g;" /etc/speech-dispatcher/speechd.conf
	else
            echo "AudioOutputMethod \"pulse\"" >> /etc/speech-dispatcher/speechd.conf
	fi
    fi
}

enable_openal() {
    # nothing to do here. openal-soft is patched to prefer pulse but
    # it won't autostart the daemon.
    return 0
}

enable_oss() {
    echo "Enabling PulseAudio for OSS..."
    for app in $LIST_OF_OSS_APPS; do
	if grep -q "alias $app='padsp $app'" $PROFNAME; then
	    echo "Application $app already setup for PulseAudio"
	else
	    echo "alias $app='padsp $app'" >> $PROFNAME
	fi
    done
}

enable_sdl() {
    echo "Enabling PulseAudio for SDL..."
    # For SDL, we just add an environment variable, so that apps use the PA audio driver
    set_variable SDL_AUDIODRIVER pulse
}

enable_timidity() {
    echo "Enabling PulseAudio for Timidity..."
    # Use esound output for timidity
    if grep -q "alias timidity='timidity -Oe'" $PROFNAME; then
	echo "Timidity already setup for using PulseAudio"
    else
	echo "alias timidity='timidity -Oe'" >> $PROFNAME
    fi
}

enable_xine() {
    #echo "Enabling PulseAudio for Xine..."
    # FIXME: xine uses $HOME/.xine/config
    return 0
}

enable_autospawn() {
    echo "Enabling PulseAudio autospawn..."
    if grep -q ^autospawn /etc/pulse/client.conf; then
	perl -pi -e "s|^autospawn.*|autospawn = yes|g;" /etc/pulse/client.conf
    else
	echo "autospawn = yes" >> /etc/pulse/client.conf
    fi
}

disable_alsa() {
    echo "Disabling PulseAudio for ALSA..."
    delete_variable ALSA_CONFIG_PATH
}

disable_phonon() {
    echo "Disabling PulseAudio for Phonon..."
    set_variable PHONON_PULSEAUDIO_DISABLE 1
}

disable_libao() {
    if test -f /etc/libao.conf; then
	echo "Disabling PulseAudio for libao..."
	perl -pi -e "s|default_driver=pulse||g;" /etc/libao.conf
    fi
}

disable_mplayer() {
    if test -f /etc/mplayer/mplayer.conf; then
	echo "Disabling PulseAudio for mplayer..."
	perl -pi -e "s|ao=pulse||g;" /etc/mplayer/mplayer.conf
    fi
}

disable_speechd() {
    echo "Disabling PulseAudio for speech dispatcher..."
    if test -f /etc/speech-dispatcher/speechd.conf; then
	if grep -q 'AudioOutputMethod' /etc/speech-dispatcher/speechd.conf; then
            perl -pi -e "s|^.*AudioOutputMethod .*|#AudioOutputMethod \"pulse\"|g;" /etc/speech-dispatcher/speechd.conf
	fi
    fi
}

disable_openal() {
    # nothing to do here. openal-soft is patched to prefer pulse but
    # it won't autostart the daemon.
    return 0
}

disable_oss() {
    for app in $LIST_OF_OSS_APPS; do
	perl -pi -e "s|alias $app='padsp $app'||g;" $PROFNAME
    done
}

disable_sdl() {
    echo "Disabling PulseAudio for SDL..."
    delete_variable SDL_AUDIODRIVER
}

disable_timidity() {
    echo "Disabling PulseAudio for Timidity..."
    perl -pi -e "s|alias timidity='timidity -Oe'||g;" $PROFNAME
}

disable_xine() {
    #echo "Disabling PulseAudio for Xine..."
    # FIXME: xine uses $HOME/.xine/config
    return 0
}

disable_autospawn() {
    echo "Disabling PulseAudio autospawn..."
    if grep -q ^autospawn /etc/pulse/client.conf; then
	perl -pi -e "s|^autospawn.*|autospawn = no|g;" /etc/pulse/client.conf
    else
	echo "autospawn = no" >> /etc/pulse/client.conf
    fi
}

case $1 in
    --enable)
	check_root || exit
	ENABLE=1
	enable_alsa
	enable_libao
	enable_mplayer
	enable_openal
	enable_oss
	enable_sdl
	enable_timidity
	enable_xine
	enable_autospawn
	enable_phonon
	enable_speechd
	;;
    --disable)
	check_root || exit
	ENABLE=0
	disable_alsa
	disable_libao
	disable_mplayer
	disable_openal
	disable_oss
	disable_sdl
	disable_timidity
	disable_xine
	disable_autospawn
	disable_phonon
	disable_speechd
	;;
    --status)
	if [ -f /etc/sysconfig/sound ]; then
	    . /etc/sysconfig/sound
	fi
	if [ "x$PULSEAUDIO_ENABLE" = "xyes" ]; then
	    echo "enabled"
	else
	    echo "disabled"
	fi
	exit
	;;
    *)
	show_help
	;;
esac

# Now, update /etc/sysconfig/sound with the PA status
if grep -q PULSEAUDIO_ENABLE /etc/sysconfig/sound; then
    if [  "x$ENABLE" = "x1" ]; then
	perl -pi -e "s|PULSEAUDIO_ENABLE=\"no\"|PULSEAUDIO_ENABLE=\"yes\"|g;" /etc/sysconfig/sound
    else
	perl -pi -e "s|PULSEAUDIO_ENABLE=\"yes\"|PULSEAUDIO_ENABLE=\"no\"|g;" /etc/sysconfig/sound
    fi
else
    if [ "x$ENABLE" = "x1" ]; then
	echo "PULSEAUDIO_ENABLE=\"yes\"" >> /etc/sysconfig/sound
    else
	echo "PULSEAUDIO_ENABLE=\"no\"" >> /etc/sysconfig/sound
    fi
fi

/sbin/SuSEconfig