#!/bin/bash -e
#
#     emu-script  --  A setup script
#
#     Author: Daniel Bertrand
#     Last Update:   July 17, 2001
#     Version:       0.4 
#
# 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
#
# Configure (see options below) then copy this file to /usr/local/etc/
#
# General card config options:

CARD_IS_5_1=no
USE_DIGITAL_OUTPUT=no
ENABLE_TONE_CONTROL=yes
AC3PASSTHROUGH=no
ENABLE_LIVEDRIVE_IR=no

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

DSPMGR=$BASE_PATH/bin/emu-dspmgr
CONFIG=$BASE_PATH/bin/emu-config

#
# More configurable things below, after the line with "$DSMGR -z"
#

#set some variables
if [ $USE_DIGITAL_OUTPUT == yes ]; then
    FRONT="Digital"
else
    FRONT="Front"
fi

##function to enable an input
enable_input(){
    INPUT=$@
    $DSPMGR -a"$INPUT L:"$FRONT" L"
    $DSPMGR -a"$INPUT R:"$FRONT" R"
    $DSPMGR -a"$INPUT L:Rear L"
    $DSPMGR -a"$INPUT R:Rear R"
}
input_volume(){
    INPUT=$1
    VOLUME=$2

    $DSPMGR -p"$VOLUME Vol L" -l"$INPUT L" -f$DSPPATH/vol.bin
    $DSPMGR -p"$VOLUME Vol R" -l"$INPUT R" -f$DSPPATH/vol.bin

    $DSPMGR  -p"$VOLUME Vol L" -c"Vol" -m"${VOLUME}_l"
    $DSPMGR  -p"$VOLUME Vol R" -c"Vol" -m"${VOLUME}_r"
}    

#Start by clearing everything and stopping the FX8010
$DSPMGR -x -z


#
# Livedrive and other special inputs can be enabled here by un-commenting
# the appropriate lines
# 

#this line enables input:
    enable_input "CD-Spdif"
#this line maps it to an OSS mixer control:
    input_volume "CD-Spdif" "dig1"

#    enable_input "Opt. Spdif"
#    input_volume "Opt. Spdif" "dig2"

#    enable_input "Line2/Mic2"
#    input_volume "Line2/Mic2" "line2"

#    enable_input "RCA Spdif"
#    input_volume "RCA Spdif" "dig3"

#    enable_input "RCA Aux"
#    input_volume "RCA Aux" "line3"


if [ $AC3PASSTHROUGH == yes ]; then
    $DSPMGR -a"fx15:Digital L"
    $DSPMGR -a"fx15:Digital R"
    $DSPMGR -l"Digital L" -l"Digital R" -f$DSPPATH/ac3pass.bin
fi


# Add common routes:



#/dev/dsp1 to rear 
$DSPMGR -a"Pcm1 L:Rear L"
$DSPMGR -a"Pcm1 R:Rear R"

#Analog sources in to record
$DSPMGR -a"Analog L:ADC Rec L"
$DSPMGR -a"Analog R:ADC Rec R"

$DSPMGR -a"Analog L:Rear L"
$DSPMGR -a"Analog R:Rear R"


# Add common Patches

#/dev/dsp to rear (add volume later)
$DSPMGR -a"Pcm L:Rear L"
$DSPMGR -a"Pcm R:Rear R"


#PCM volume gain
$DSPMGR -l"Pcm1 L" -f$DSPPATH/gain.bin
$DSPMGR -l"Pcm1 R" -f$DSPPATH/gain.bin
$DSPMGR -l"Pcm L" -f$DSPPATH/gain.bin
$DSPMGR -l"Pcm R" -f$DSPPATH/gain.bin

#Master Volume Controls
$DSPMGR -p"Master R" -l"Rear R" -f$DSPPATH/vol.bin -cVol -mvol_r
$DSPMGR -p"Master L" -l"Rear L" -f$DSPPATH/vol.bin -cVol -mvol_l


#digital/analog specific setup
if [ $USE_DIGITAL_OUTPUT == yes ]; then
    $DSPMGR -v"Pcm L:Digital L" -mpcm_l
    $DSPMGR -v"Pcm R:Digital R" -mpcm_r

    $DSPMGR -a"Analog L:Digital L"
    $DSPMGR -a"Analog R:Digital R"
    $DSPMGR -p"Master R" -l"Digital R" -f$DSPPATH/vol.bin
    $DSPMGR -p"Master L" -l"Digital L" -f$DSPPATH/vol.bin
    
    enable_input "Pcm"
    input_volume "Pcm" "pcm"
#bogus routes to "ground" pcm to analog output:
    $DSPMGR -a"fx15:Front L"
    $DSPMGR -a"fx15:Front R"
else
    
    
    #/dev/dsp to rear
    $DSPMGR -v"Pcm L:Rear L" -mpcm_l
    $DSPMGR -v"Pcm R:Rear R" -mpcm_r

    $DSPMGR -a"Pcm L:Front L"
    $DSPMGR -a"Pcm R:Front R"
    # analog and master volume control are taken care of by 
    # harware for analog front.
fi

if [ $ENABLE_TONE_CONTROL == yes ] ; then

    $DSPMGR -l"Pcm L" -f$DSPPATH/tone.bin -cbass -mbass -ctreble -mtreble
#The next 3 'inherit' the oss control of the above line:
    $DSPMGR -l"Pcm R" -f$DSPPATH/tone.bin 
    $DSPMGR -l"Pcm1 L" -f$DSPPATH/tone.bin
    $DSPMGR -l"Pcm1 R" -f$DSPPATH/tone.bin
fi

# Need to toggle third output on 5.1 cards

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

# Restart the FX8010
$DSPMGR -y
