#!/bin/bash -e
#
#     audigy-script  --  A setup script
#
#     Author: Daniel Bertrand
#     Last Update:   February, 2002
#     Version:       0.1
#
# Description:
# -----------
# You can use this script to automatically configure the card 
# on module load
#
# To do so, add the following line to your module.config (or conf.module) file
# (and remove the leading '#'):
# 
# post-install emu10k1 /usr/local/etc/emu-script
#
# if you want to save mixer levels, you can use the following:
# 
# post-install emu10k1 /usr/local/etc/emu-script restore
# pre-remove emu10k1 /usr/local/etc/emu-script save
#
# configuration is done in the file 'emu10k1.conf'
#
# Requirements:
# ------------
# This script needs the following to be installed:
#
# -the emu10k1 tools: 
#      emu-config, emu-dspmgr and the dsp .bin files
# -aumix (installed by default on many Linux distros)
# 
#
# Optional argument processing:
# This script takes arguments which can override the settings in the
# config file. The following flags are supported:
#
# -d [yes|no]	use the digital output
# -t [yes|no]	enable the tone controls
# -3 [yes|no]	enable ac3 passthrough
# -i [yes|no]	enable livedrive ir
# -r [yes|no]	invert rear channels
# -m [yes|no]	enable multichannel mode
# -s [yes|no]	route all multichannel signals to sub
# -b [yes|no]	enable the 12dB front analog boost
#
# I don't include command line switches for enabling the various inputs
# or selecting if the card is 5.1 since I can't see being able to set or
# unset these on the fly being particularly useful.

# Default location of programs: 
BASE_PATH=/usr/local
DSPPATH=$BASE_PATH/share/emu10k1


#
# To use something other than /dev/dsp
# example "-D /dev/dsp1"
DSPDEV=""

# To use something other than /dev/dsp
# example "-M /dev/mixer1"
MIXERDEV=""

DSPMGR=$BASE_PATH/bin/emu-dspmgr $DSPDEV $MIXERDEV

CONFIG=$BASE_PATH/bin/emu-config $DSPDEV $MIXERDEV

DRIVER_VERSION=` $DSPMGR  -q` 
 
SAVEARGS="$@"

load(){

# Source configurations
  . $BASE_PATH/etc/emu10k1.conf

# Pick up any command line overrides
while getopts d:t:i:r:m:s:b: OPT $SAVEARGS; do
	case "$OPT" in
	d)
		USE_DIGITAL_OUTPUT=$OPTARG
		;;
	t)
		ENABLE_TONE_CONTROL=$OPTARG
		;;
	3)
		AC3PASSTHROUGH=$OPTARG
		;;
	i)
		ENABLE_LIVEDRIVE_IR=$OPTARG
		;;
	r)
		INVERT_REAR=$OPTARG
		;;
	m)
		MULTICHANNEL=$OPTARG
		;;
	s)
		ROUTE_ALL_TO_SUB=$OPTARG
		;;
	*)
		exit 1
		;;
	esac
	shift 2
done

#set some variables
if [ "$USE_DIGITAL_OUTPUT" = yes ]; then
    FRONT="Dig Front"
    CENTER="Dig Center"
    SUB="Dig LFE"
    REAR="Dig rear"
    CTR_SUB="Dig ctr/sub"
else
    FRONT="Front"
    CENTER="Center"
    SUB="sub"
    REAR="rear"
    CTR_SUB="ctr/sub"
fi

## functions to enable a inputs and volume controls
enable_input(){
    INPUT=$@
    $DSPMGR -a"$INPUT:$FRONT" -a"$INPUT:$REAR" -a"$INPUT:Headphones" 
}
enable_volume(){
    INPUT=$1
    VOLUME=$2

    $DSPMGR -p"$VOLUME Vol" -l"$INPUT" -f$DSPPATH/vol_2.bin -c"Vol_L" -m"${VOLUME}_l" -c"Vol_R" -m"${VOLUME}_r"
}    

enable_with_vol(){
    INPUT=$1
    VOLUME=$2

    $DSPMGR  -a"$INPUT:$FRONT" -a"$INPUT:$REAR" -a"$INPUT:Headphones"
    $DSPMGR -p"$VOLUME Vol" -l"$INPUT" -f$DSPPATH/vol_2.bin -c"Vol_L" -m"${VOLUME}_l" -c"Vol_R" -m"${VOLUME}_r"
}
#Start by clearing everything and stopping the FX8010
$DSPMGR -x -z


# Enable inputs (route them to the outputs) and volume controls

# pcm can be up to 6 channels


$DSPMGR -a"pcm:$FRONT" -a"pcm rear:$REAR"  -a"pcm:headphones" -a"pcm:headphones" -a"pcm ctr/sub:$CTR_SUB"
$DSPMGR -l"pcm" -l"pcm rear" -l"pcm ctr/sub" -f$DSPPATH/vol_pcm.bin -c"Vol_L" -m"pcm_l" -c"Vol_R" -m"pcm_r"



# other inputs are only stereo

if [ "$ENABLE_CD_Spdif" = yes ]; then
     enable_with_vol "CD-Spdif" "dig1"
fi

if [ "$ENABLE_OPTICAL_SPDIF" = yes ]; then
    enable_with_vol "Opt. Spdif" "dig2"
fi
if [ "$ENABLE_LINE2_MIC2" = yes ]; then 
    enable_with_vol "Line2/Mic2" "line2"
fi
if [ "$ENABLE_RCA_SPDIF" = yes ]; then
    enable_with_vol "RCA Spdif" "dig3"
fi
if [ "$ENABLE_RCA_AUX" = yes ]; then
    enable_with_vol  "RCA Aux2" "line3"
fi


# Analog-in is already routed to the analog front output
# So we route it only to the remaining outputs

$DSPMGR  -a"analog:$REAR" -a"analog:Headphones" 
if [ "$USE_DIGITAL_OUTPUT" = yes ]; then
    $DSPMGR -a"analog:dig front" 
fi

# We record analog inputs by default.
$DSPMGR  -a"analog:recording"

#Output volume controls:
enable_volume "$FRONT" "vol"
enable_volume "$REAR" "ogain"


if [ "$ENABLE_TONE_CONTROL" = yes ] ; then

    TONE=tone.bin

    $DSPMGR -l"$FRONT L" -f$DSPPATH/$TONE -cbass -mbass -ctreble -mtreble
#The next 3 'inherit' the oss control of the above line:
    $DSPMGR -l"$FRONT R" -f$DSPPATH/$TONE 
    $DSPMGR -l"$REAR R" -f$DSPPATH/$TONE
    $DSPMGR -l"$REAR L" -f$DSPPATH/$TONE
#    $DSPMGR -l"$CENTER"  -f$DSPPATH/$TONE
    
    aumix -t 68
    aumix -b 68
fi



# Need to toggle third output into digital mode

if [ "$CARD_IS_5_1" = yes ] ; then
    if [ "$USE_DIGITAL_OUTPUT" = yes ] ; then
	$CONFIG -d
    else
	$CONFIG -a
    fi
fi

# See if we should enable IR

if [ "$ENABLE_LIVEDRIVE_IR" = yes ] ; then
     $CONFIG -i
fi

# for digital output we can do ac3passthrough
# this patch must be last in the signal chain
if [ "$USE_DIGITAL_OUTPUT" = yes ]; then
    if [ "$AC3PASSTHROUGH" = yes ] ; then
	$DSPMGR -l"dig front" -F"$DSPPATH/ac3pass-audigy.bin"
    fi
fi


# Restart the FX8010
$DSPMGR -y

}

case "$1" in

	restore | restart)
                load
		# restore mixer settings
		/bin/aumix-minimal -f /etc/.aumixrc -L  >/dev/null 2>&1 || : 
                ;;
        save | stop)
		# save mixer settings
		/bin/aumix-minimal -f /etc/.aumixrc -S   >/dev/null 2>&1 || :  
		;;
        *)
		load
esac
