#!/bin/sh

## Includes.
. funcrcfileSet

## Write the rcfile from scratch or upgrade an existing one.
## On entry: $1 = new to write a new rcfile or
##                a version number to upgrade from.

funcrcfileWrite() {
	if [ -z "$BASH" ]; then local FUNCNAME=funcrcfileWrite; fi
	if [ $DEBUG_TRANSITS -ne 0 ]; then echo "$FUNCNAME: IN"; fi

	## Local variables.
	local flac mp3 ogg other wav

	## Write the main section.
	if [ $1 = new ]; then
		echo '
version='$VERSION'' > $LOCAL_DATA_DIR/${TARGET}rc
	else
		funcrcfileSet version $VERSION main
	fi

	## Write the players section.
	if [ $1 = new ]; then
		## Find a flac player.
		if [ `command -v flac123` ]; then flac="flac123 -q"; fi

		## Find an mp3 player.
		if [ `command -v mpg123` ]; then mp3="mpg123 -q"
		elif [ `command -v mpg321` ]; then mp3="mpg321 -q"
		elif [ `command -v madplay` ]; then mp3="madplay -q"
		elif [ `command -v plaympeg` ]; then mp3="plaympeg"
		fi

		## Find an ogg player.
		if [ `command -v ogg123` ]; then ogg="ogg123 -q"; fi

		## Find a player for other.
		if [ `command -v playsound` ]; then other="playsound"; fi

		## Find a wav player.
		if [ `command -v wavplay` ]; then wav="wavplay -q"
		elif [ `command -v aplay` ]; then wav="aplay -q"
		fi

		echo '
[players]
# When playing a file, '$TITLE' will navigate down through
# this list attempting to match the format and if it doesn'"'"'t
# find anything suitable then it'"'"'ll use whatever'"'"'s in "other".
# These are best listed in descending order of popularity.
# You can remove or add anything you like to this list.
mp3='$mp3'
ogg='$ogg'
flac='$flac'
wav='$wav'

# If you'"'"'ve got playsound (part of the SDL_sound package) then
# it makes a great "other" as it'"'"'ll play mp3, ogg, aiff, flac,
# wav, mod, midi (if you'"'"'ve got the libraries) and only 22KB!
other='$other'' >> $LOCAL_DATA_DIR/${TARGET}rc
	fi

	## Write the options section.
	if [ $1 = new ]; then
		echo '
[options]
# This is the interval in milliseconds for the auto-next timer
# which is only active when something is playing. On average it
# will take half the interval for the next track to auto-play.
autonext_interval='$AUTONEXT_INTERVAL'

# Setting these to non-zero will generate lots of useful
# debugging information that gets output to the terminal.
debug_content='$DEBUG_CONTENT'
debug_transits='$DEBUG_TRANSITS'

# Setting a cover size of -1 will set it to the image size and
# any non-zero value will scale all images to that exact size.
cover_size='$COVER_SIZE'

cover_thumb_size='$COVER_THUMB_SIZE'

window_width='$WINDOW_WIDTH'' >> $LOCAL_DATA_DIR/${TARGET}rc
	fi
	if [ $1 = new -o $1 \< 0.1.4 ]; then
		echo '
# This is passed to the ls command and the first file matched will
# be set as the cover so modify it if you need to be more selective.
cover_search_pattern='"$COVER_SEARCH_PATTERN"'

dialog_readme_show='$DIALOG_README_SHOW'' >> $LOCAL_DATA_DIR/${TARGET}rc
	fi

	if [ $DEBUG_TRANSITS -ne 0 ]; then echo "$FUNCNAME: OUT"; fi
}
