#!/bin/bash
#set -n		# read but do not execute commands (check on syntax errors)
#
# File: /usr/local/bin/sample.dialog.templ
#
# Change audio volume of different MP3s.
#
#    Copyright (C) 2000  Juergen Kaesmann (JK)
#
##############################################################################
# GPL NOTICE
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
##############################################################################
# FIND SAMPLES
#
##############################################################################
# FILTER SAMPLES
#
##############################################################################
# AWK SAMPLES
#
#############################################################################
# SED SAMPLES
#
##############################################################################
# RSYNC OPTIONS
#
##############################################################################
# DIALOG SAMPLES
#
# Given/selected answer goes to STDERR
# For automatic size set h w lh fh to 0
#
# buildlist: display 2 lists side-by-side,move items between,h w lh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --visit-items --buildlist "$wtitle" height width list-height [ tag item status ] ... 2>$ansfile
#
# calendar: display and edit a date, h w = 15 60
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --calendar "$wtitle" height width day month year 2>$ansfile
#
# checklist: show a list with (de)selectable entries, h w lh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --checklist "$wtitle" height width list-height [tag item status] ... 2>$ansfile
#
# dselect: display and input a directory, h w = 25 80
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --dselect filepath height width 2>$ansfile
#
# editbox: display and edit a file, h w = 25 80
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --editbox filepath height width 2>$ansfile
#
# form: show a formula with editable entries, h w fh = 25 80 18, x,y count from top,left, IDX is fix
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --form "$wtitle" height width formheight [ label y x item y x flen ilen ] ... 2>$ansfile
#
# fselect: show a file selection box, h w = 25 80
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --fselect filepath height width 2>$ansfile
#
# gauge: display a meter, h w = 15 60
#dialog --title "$dtitle" --no-shadow --gauge "$wtitle" height width [percent]
#
# infobox: display a msg and exit, h w = 5 60
#dialog --title "$dtitle" --ok-label "Ok" --no-shadow --infobox "$wtitle" height width
#
# inputbox: show a msg, input an ans, h w = 8 60
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --inputbox "$text" height width ["$init"] 2>$ansfile
#
# inputmenu: like menu but with button RENAME to rename one of the entries, h w = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --inputmenu "$wtitle" height width menu-height [ tag item ] ... 2>$ansfile
#
# menu: show a menu, h w mh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --menu "$wtitle" height width menu-height [tag item] ... 2>$ansfile
#
# mixedform: like form but with field-type each, h w fh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --mixedform "$wtitle" height width formheight [ label y x item y x flen ilen itype ] ... 2>$ansfile
#
# mixedgauge: like gauge but with tag item pairs, h w = 15 60
#dialog --title "$dtitle" --no-shadow --mixedgauge "$wtitle" height width percent [ tag1 item1 ] ...
#
# msgbox: show a msg, h w = 7 60
#dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" height width
#
# passwordbox: like inputbox with invisible input, h w = 8 60
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --passwordbox "$wtitle" height width [init] 2>$ansfile
#
# passwordform: like form but with all fields are password widgets, h w fh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --passwordform "$wtitle" height width formheight [ label y x item y x flen ilen ] ... 2>$ansfile
#
# pause: display seconds to remain, h w = 8 39
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --pause "$wtitle" height width seconds
#
# prgbox: display output of command and enable to quit, h w = 25 80
#dialog --title "$dtitle" --no-shadow --prgbox ["$wtitle"] command height width
#
# programbox: display piped output of command with button OK, h w = 25 80
#dialog --title "$dtitle" --ok-label "Ok" --no-shadow --programbox "$wtitle" [text] height width
#
# progressbox: display piped output of command, no button, h w = 25 80
#dialog --title "$dtitle" --no-shadow --progressbox ["$wtitle"] height width
#
# radiolist: select one status to ON, h w lh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --radiolist "$wtitle" height width list-height [ tag item status ] ... 2>$ansfile
#
# rangebox: select with slider from a range of values, h w lh =25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --rangebox "$wtitle" height width list-height min-value max-value default-value 2>$ansfile
#
# tailbox: display text from a file like 'tail -f', h w = 25 80
#dialog --title "$dtitle" --ok-label "Ok" --no-shadow --tailbox "$file" height width
#
# tailboxbg: display text from a file like 'tail -f' in the background, h w = 25 80
#dialog --title "$dtitle" --no-shadow --tailboxbg "$file" height width
#
# textbox: show a file's content, h w = 25 80
#dialog --title "$dtitle" --no-shadow --tab-correct --textbox "$file" height width
#
# timebox: display and edit a time, h w = 3 39
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --timebox "$wtitle" height width [ hour minute second ] 2>$ansfile
#
# treeview: display tree data and select one of them, h w lh = 25 80 18
#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --treeview "$wtitle" height width list-height [ tag item status depth ] ... 2>$ansfile
#
# yesno: show a msg, select yes or no, h w = 6 60, Ret 0=yes 1=no other=cancel
#dialog --title "$dtitle" --no-shadow --yesno "$question\nOverwrite?" height width
#
##############################################################################
# REPLACE STRINGS
#
##############################################################################
# EXIT CODES

##############################################################################
# SOURCE FILES
#
. /usr/local/etc/main.conf				# config main
#. $LLIBDIR/libfuncerr-sh				# functions error
. $LLIBDIR/libfuncstd-sh				# functions standard
#. $LLIBDIR/libfuncmath-sh				# functions math
#. $LLIBDIR/libfuncip-sh				# functions IP
. /usr/local/etc/multimedia.conf			# config multimedia
#
#if [ ! -e ~/.samplerc ]; then				# install config user
#	cp /usr/local/share/ksm-sample/samplerc ~/.samplerc
#fi
#. ~/.samplerc						# config user

##############################################################################
# DECLARATION OF STANDARD CONSTANTS AND VARIABLES
#
readonly package=Multimedia				# package name
readonly package_file=ksm-multimedia			# package file name
readonly vinfopath="/usr/local/share/$package_file"	# path to package dir
readonly version=$(cat $vinfopath/VERSION)		# package version
readonly release=$(cat $vinfopath/RELEASE)		# package release
readonly scriptname=$(basename $0)			# name of script
#readonly tempfile=$TEMPDIR/$scriptname.$UID.tmp	# temporary file name
tempfile=$(mktemp -u $TEMPDIR/$scriptname.$UID.XXXXXX)	# temporary file name
#readonly lockfile=$TEMPDIR/lock/$scriptname.$UID.lock	# lock file name
readonly debuglog="/usr/bin/logger -p user.debug -t $scriptname -- DEBUG:"	# log dbg msg
readonly errlog="/usr/bin/logger -p user.err -t $scriptname -- ERROR:"		# log err msg
readonly infolog="/usr/bin/logger -p user.info -t $scriptname -- INFO:"		# log inf msg
cmd=''							# sub command
debug=no						# debug=yes/no
verbose=''						# verbose=yes/''/no
err=0							# error code

##############################################################################
# USER FILL IN AT FIRST RUN

##############################################################################
# SYSTEM

##############################################################################
# USER-CONSTANTS
#
# STRINGS
#readonly const=string
readonly ansfile=$(mktemp -u $TEMPDIR/$scriptname.$UID.ans.XXXXXX)

# NUMBERS
#readonly const=value

##############################################################################
# USER-VARIABLES
#
# STRINGS
tempfile2="$tempfile.2"

#args=''				# all args from cmdline
#arg1=''				# 1. arg from cmdline
#opt_o=''				# option -o (yes)
					# option --output (yes)
#opt_i=''				# option -i (arg)
					# option --infile (arg)
#opt_o=''				# option -o (arg)
					# option --outfile (arg)
#opt_p=''				# option -p (arg)
					# option --pages (arg)
startdir="/"
hline="/"
aflt=""
gv=0 					# global volume
gc=0					# global compander
gd=0					# global dynaudnorm profile

# NUMBERS
#p_first=0				# first page
#p_last=0				# last page

# ARRAYS
# Indexed arrays are always zero based string arrays.
# If index starts with 1 and all members are assigned then
# count of members of array = last index of array
declare -a arr
#maxarr="${#arr[@]}"			# get number of elements in arr
#unset arr[@]				# remove the entire array

#declare -a DynProf			# implizit done in local config
maxDynProf="${#DynProf[@]}"		# get number of elements in arr
maxprof=$[$maxDynProf - 1]		# range = 0 - n-1 = max idx

##############################################################################
# SIGNAL HANDLING

##############################################################################
# FUNCTIONS
##############################################################################
#
##############################################################################
# HELP
#
#=============================================================================
function helptext ()
{
# Output helptext
# Uses cat, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG helptext: $# $@" >&2
	$debuglog helptext: $# $@
fi

cat <<EOT
NAME
$scriptname - Change audio volume of different MP3s.

SYNOPSIS
$scriptname [options]

DESCRIPTION
Step through dirs and change audio volume of different MP3s.

Defaults:  Do it.

OPTIONS
Gnu style options:

  -h|--help    = Output help, then exit.
  -V|--version = Output version info, then exit.
  -d|--debug   = Turn on DEBUG mode.
  -q|--quiet   = Be quiet.
  -v|--verbose = Be verbose.
  --           = End of options.

EXIT STATUS
On program exit:

  Code 0 = OK

  See output of 'showerrmsg'.

FILES
Used directories and files:

  Config Global = /usr/local/etc/multimedia.conf
  Config User   = ~/.multimediarc

SEE ALSO
Related manpages:

  showerrmsg(1).

AUTHORS
Juergen Kaesmann <juergen@kaesmann-online.de>
EOT
#  -o|--output  = Output to STDOUT.
#  -i f         = Input from file <f>.
#  --infile=f   = Input from file <f>.
#  -o f         = Output to file <f>.
#  --outfile=f  = Output to file <f>.
#  -p s,e       = Pages s=START e=END.
#  --pages=s,e  = Pages s=START e=END.

#ARGUMENTS
#Give if needed:
#
#  a = Arg a

#EXAMPLES
#For instance:
#
#  Do it:
#  $scriptname

return
}

#=============================================================================
function show_about ()
{
# Show info about program.
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_about: $# $@" >&2
	$debuglog show_about: $# $@
fi

local dtitle='' msgtxt=''
local prog="AudioVolMan"
local vr="Version ${version}-${release}"
local auth="by Juergen Kaesmann"
local ret=0

CSet 28 "$prog"
ret=$?
if [ $ret -eq 0 ]; then Pop prog; fi

CSet 28 "$vr"
ret=$?
if [ $ret -eq 0 ]; then Pop vr; fi

CSet 28 "$auth"
ret=$?
if [ $ret -eq 0 ]; then Pop auth; fi

msgtxt="
$prog

$vr

$auth"

# msgbox: show a msg, h w = 7 60
#dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" height width
dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" 11 32
ret=$?

return $ret
}

#=============================================================================
function show_msg ()
{
# Show a msg text.
# par1 = msgtxt
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_msg: $# $@" >&2
	$debuglog show_msg: $# $@
fi

local dtitle="MESSAGE" msgtxt="$1"
local ret=0

# msgbox: show a msg, h w = 7 60
#dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" height width
dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" 7 60
ret=$?

return $ret
}

#=============================================================================
function show_error ()
{
# Show the error text.
# par1 = errcode
# par2 = additional errtext
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_error: $# $@" >&2
	$debuglog show_error: $# $@
fi

local dtitle='ERROR' msgtxt="\Z1${ErrMsg[$1]}\Zn $2"
local ret=0

# msgbox: show a msg, h w = 7 60
#dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" height width
dialog --title "$dtitle" --no-shadow --colors --msgbox "$msgtxt" 7 60
ret=$?

return $ret
}

#=============================================================================
function show_debug ()
{
# Show a msg text.
# par1 = msgtxt
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_debug: $# $@" >&2
	$debuglog show_debug: $# $@
fi

local dtitle="DEBUG" msgtxt="$1"
local ret=0

# msgbox: show a msg, h w = 7 60
#dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" height width
dialog --title "$dtitle" --no-shadow --msgbox "$msgtxt" 7 60
ret=$?

return $ret
}

#=============================================================================
function show_info ()
{
# Show an info and exit.
# par1 = msgtxt
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_info: $# $@" >&2
	$debuglog show_info: $# $@
fi

local dtitle="INFO" wtitle="$1"
local ret=0

# infobox: display a msg and exit, h w = 7 60
#dialog --title "$dtitle" --ok-label "Ok" --no-shadow --infobox "$wtitle" height width
dialog --title "$dtitle" --no-shadow --infobox "$wtitle" 7 60
ret=$?

return $ret
}

#=============================================================================
function show_textfile ()
{
# Show content of a text file.
# par1 = dtitle
# par2 = filename
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_textfile: $# $@" >&2
	$debuglog show_textfile: $# $@
fi

local dtitle="$1" file="$2"
local ret=0

# textbox: show a file's content, h w = 25 80
#dialog --title "$dtitle" --no-shadow --tab-correct --textbox "$file" height width
dialog --title "$dtitle" --no-shadow --tab-correct --textbox "$file" 25 80
ret=$?

return $ret
}

#=============================================================================
function pause ()
{
# Make a Pause.
# par1 = wtitle
# par2 = Seconds to pause
# Uses dialog, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG pause: $# $@" >&2
	$debuglog pause: $# $@
fi

local dtitle="PAUSE" wtitle="$1" s="$2"
local ret=0

# pause: display seconds to remain, h w = 8 39
#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --pause "$wtitle" height width seconds
dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --pause "$wtitle" 8 39 $s
ret=$?

return $ret
}

#=============================================================================
function get_input ()
{
# Get input from user.
# par1 = text
# par2 = value (startvalue)
# Push result onto stack (1 string).
# Uses global ansfile
# Uses cat, dialog, logger, rm
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG get_input: $# $@" >&2
	$debuglog get_input: $# $@
fi

local dtitle="INPUT" txt="$1" val="$2" s=''
local ret=0

if [ -z "$3" ]; then					# parameter count ok
	# inputbox: show a msg, input an ans, h w = 8 60
	#dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --inputbox "$text" height width ["$init"] 2>$ansfile
	dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --no-shadow --inputbox "$txt" 8 60 "$val" 2>$ansfile
	ret=$?
	if [ "$ret" -eq 0 ]; then
		s=$(cat $ansfile)
		if [ -n "$s" ]; then
			Push "$s"
		else
			ret=10
		fi
	fi
	rm -f $ansfile
else
	show_error 2 "in get_input"
	ret=2
fi

return $ret
}

#=============================================================================
function get_yesno ()
{
# Get a yes/no answer from user.
# par1 = text
# Uses global ansfile
# Uses dialog, logger, rm
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
#	echo "DEBUG get_yesno: $# $@" >&2
	$debuglog get_yesno: $# $@
fi

local dtitle="YES or NO" txt="$1"
local ret=0

# yesno: show a msg, select yes or no, h w = 6 60, Ret 0=yes 1=no other=cancel
#dialog --title "$dtitle" --no-shadow --yesno "$question\nOverwrite?" height width
dialog --title "$dtitle" --no-shadow --yesno "$txt" 6 60
ret=$?
#rm -f $ansfile

return $ret
}

#=============================================================================
function kde_pop ()
{
# Give the KDE-Beep-Pop signal.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG kde_pop: $# $@" >&2
	$debuglog kde_pop: $# $@
fi

local ret=0

aplay -q /usr/local/share/ksm-sounds/KDE/KDE_Beep_Pop.wav
ret=$?

return $ret
}

#=============================================================================
function show_volinfo ()
{
# show volinfo
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG show_volinfo: $# $@" >&2
	$debuglog show_volinfo: $# $@
fi

local dtitle="AUDIO VOLUME INFO" s=''
local ret=0

# show volinfo file
s=$(audiovolinfo -f)
show_textfile "${hline}" "$s"
ret=$?

return $ret
}

#=============================================================================
function overwrite_old ()
{
# overwrite old
# par1 = 
# Uses echo, logger, mv
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG overwrite_old: $# $@" >&2
	$debuglog overwrite_old: $# $@
fi

local s="" ss=""
local ret=0

cd $startdir

# overwrite orig MP3s with new ones
s=$(ls n*.mp3)
ret=$?
if [ $ret -eq 0 ]; then
	get_yesno "UPDATE all MP3s?"
	ret=$?
	if [ $ret -eq 0 ]; then
		for s in n*.mp3; do
			ss=${s#n*}
			mv $s $ss
		done
	fi
fi

return $ret
}

#=============================================================================
function checklist_ok ()
{
# Do checklist things.
# par1 = ans
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG checklist_ok: $# $@" >&2
	$debuglog checklist_ok: $# $@
fi

local ans="$1" s=""
local i=0 ret=0

# do something
#show_msg "checklist_ok: OK Button, ans=$ans"

cp /dev/null $tempfile2
for i in $ans; do
	s=$(echo "${arr[$i]}" | cut -d ' ' -f2)
	echo "$s" >> $tempfile2
done
ret=$?

return $ret
}

#=============================================================================
function checklist_extra ()
{
# Do extra things.
# par1 = ans
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG checklist_extra: $# $@" >&2
	$debuglog checklist_extra: $# $@
fi

local ans="$1" s=""
local i=0 ret=0

# do something
#show_msg "checklist_extra: Extra Button, ans=$ans"

cp /dev/null $tempfile2
i=0
while [ $i -lt $maxarr ]; do
	s=$(echo "${arr[$i]}" | cut -d ' ' -f2)
	echo "$s" >> $tempfile2
	let ++i
done
ret=$?

return $ret
}

#=============================================================================
function checklist_help ()
{
# Show the checklist help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG checklist_help: $# $@" >&2
	$debuglog checklist_help: $# $@
fi

local dtitle='HELP FOR CHECKLIST' msgtxt=""
local ret=0

msgtxt="Switch entries on/ff by hitting SPACE. If satisfied then hit OK. 
Lines should not exceed 69 Chars."
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function dialog_checklist ()
{
# Select items from a list
# par1 = dtitle
# par2 = wtitle
# par3 = File with data
# Uses globals, arr, ansfile
# Uses cat, dialog, echo, logger, rm
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG dialog_checklist: $# $@" >&2
	$debuglog dialog_checklist: $# $@
fi

local dtitle="$1" wtitle="$2" datafile="$3" s='' ans="" quit=no
local tis_list=""
local i=0 ret=0

# NO-BREAK SPACE = ' '
# [tag item status] ...				separate with TAB or NL
#tis_list=
#arr[0]="1 Alle Maenner off"
#arr[1]="2 Alle Frauen off"
#arr[2]="3 Alle Kinder off"

# Store listing in array
# input data from datafile
unset arr[@]						# remove entire array
i=0
while read s; do
	s=${s// /_}					# ch all spaces to _
	s=${s//	/_}					# ch all spaces to _
	arr[$i]="$i ${s} off"
	let ++i
done<$datafile
maxarr="${#arr[@]}"			# get number of elements in arr

while [ "$quit" != yes ]; do
	# checklist: show a list with (de)selectable entries, h w lh = 25 80 18
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --checklist "$wtitle" height width list-height [tag item status] ... 2>$ansfile
	dialog --title "$dtitle" --ok-label "OK" --extra-button --extra-label "All" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --hline "${hline}" --checklist "$wtitle" 25 80 18 ${arr[@]} 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	case "$ret" in
	    0)						# Yes or OK button
		checklist_ok "$ans"
		ret=$?
		quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		checklist_help
		ret=$?
		;;
	    3)						# Extra button
		checklist_extra "$ans"
		quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done

return $ret
}

#=============================================================================
function fselect_ok2 ()
{
# Do fselect2 things.
# par1 = ans
# Uses globals file
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_ok2: $# $@" >&2
	$debuglog fselect_ok2: $# $@
fi

local ans="$1"
local ret=0

# do something
#show_msg "fselect_ok2: OK Button, ans=$ans"

rm $ans
ret=$?

return $ret
}

#=============================================================================
function fselect_extra2 ()
{
# Do extra things.
# par1 = ans
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_extra2: $# $@" >&2
	$debuglog fselect_extra2: $# $@
fi

local ans="$1" dtitle=""
local n=0 ret=0

#show_msg "fselect_ok2_extra: OK Button, ans=$ans"

# overwrite orig MP3s ?
overwrite_old
ret=$?

return $ret
}

#=============================================================================
function fselect_help2 ()
{
# Show the fselect2 help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_help2: $# $@" >&2
	$debuglog fselect_help2: $# $@
fi

local dtitle='HELP FOR FSELECT' msgtxt=""
local ret=0

msgtxt="The screen is divided into 4 areas.

+---------------------------------++---------------------------------+
|                                 ||                                 |
|       Directory window          ||          Files window           |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
+---------------------------------++---------------------------------+
|  Input line                                                        |
+--------------------------------------------------------------------+
|                            Button area                             |
+--------------------------------------------------------------------+

Moving between the 4 areas is done by UP DN LEFT RIGHT HOME END.

The main thing is the input line.
Dir/File selection is only done from the input line.
It exist a line completion function. Enter the beginning of a dir/file
and hit SPACE. The input line will be completed if possible.

To enter a dir name use one of the following ways:
a) Go to the directory window, select your directory then hit SPACE.
   The directory will appear in the input line and the cursor is 
   positioned at the directory name.
b) From the input line enter a directory name or at least the first few 
   characters and hit SPACE. The input line will be completed if possible.

In both cases be sure that the rightmost char is a '/'

To enter a file name use one of the following ways:
a) Go to the file window, select your file then hit SPACE.
   The file will appear in the input line and the cursor is positioned 
   at the file name.
b) From the input line enter a file name or at least the first few 
   characters and hit SPACE. The input line will be completed if possible."

echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function dialog_fselect2 ()
{
# List all dirs and files and select one file, update whole dir.
# par1 = dtitle
# par2 = startdir
# Uses globals ansfile, startdir
# Uses cat, dialog, echo, logger, rm
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG dialog_fselect2: $# $@" >&2
	$debuglog dialog_fselect2: $# $@
fi

local dtitle="$1" ans="" quit=no
local l=0 ret=0
startdir="$2"

while [ "$quit" != yes ]; do
	# fselect: show a file selection box, h w = 15 80
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --fselect filepath height width 2>$ansfile
	dialog --title "$dtitle" --ok-label "DelFile" --extra-button --extra-label "Update" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --hline "${hline}" --fselect $startdir 15 80 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	l=${#ans}
	let --l
	if [ -n "$ans" ]; then
		l=${#ans}
		let --l
		if [ ${ans:$l} == "/" ]; then		# ans is a dir
			startdir="${ans}"
		else				# no trail. / -> may be a file
			startdir=$(dirname ${ans})
		fi
	fi
	case "$ret" in
	    0)						# Yes or OK button
		fselect_ok2 "$ans"
		ret=$?
		#quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		fselect_help2
		ret=$?
		;;
	    3)						# Extra button
		fselect_extra2 "$ans"
		ret=$?
		#quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done

return $ret
}

#=============================================================================
function fselect_ok ()
{
# Do fselect things.
# par1 = ans
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_ok: $# $@" >&2
	$debuglog fselect_ok: $# $@
fi

local ans="$1" dtitle="AUDIO VOLUME INFO" s="" ss=""
local i=0 n=0 ret=0

# do something
#show_msg "fselect_ok: OK Button, ans=$ans"

cd $startdir

# select and do volinfo
dialog_menu3
ret=$?

return $ret
}

#=============================================================================
function fselect_extra ()
{
# Do extra things.
# par1 = ans
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_extra: $# $@" >&2
	$debuglog fselect_extra: $# $@
fi

local dtitle="fselect_extra" ans="$1" s="" dir="" b=""
local i=0 n=0 ret=0

cd $startdir

# select the filter type
dialog_menu2
ret=$?
if [ $ret -eq 0 ]; then
	case "$aflt" in
	    v)
		get_input "Input volume (dB):" ""
		ret=$?
		if [ $ret -eq 0 ]; then
			Pop gv
			ls *.mp3 > $tempfile
			dialog_checklist "FILTER" "Select MP3s for setting to $gv dB" $tempfile
			ret=$?
			if [ $ret -eq 0 ] || [ $ret -eq 3 ]; then
				n=$(cat $tempfile2 | wc -l)
				i=0
				while [ $i -lt $n ]; do
					let ++i
					s=$(Get_Lines $tempfile2 $i $i)
					ret=$?
					show_info "Please wait until modifications are done ..."
					audiofilterv -q -- $gv $s
					ret=$?
				done
				kde_pop
			fi
			rm -f $tempfile
			rm -f $tempfile2
			gc=""
			gd=""
		fi
		;;
	    c)
		get_input "Input compander profile:" 13
		ret=$?
		if [ $ret -eq 0 ]; then
			Pop gc
			ls *.mp3 > $tempfile
			dialog_checklist "FILTER" "Select MP3s for setting to compander profile $gc " $tempfile
			ret=$?
			if [ $ret -eq 0 ] || [ $ret -eq 3 ]; then
				n=$(cat $tempfile2 | wc -l)
				i=0
				while [ $i -lt $n ]; do
					let ++i
					s=$(Get_Lines $tempfile2 $i $i)
					ret=$?
					show_info "Please wait until modifications are done ..."
					audiofilterc -q -- $gc $s
					ret=$?
				done
				kde_pop
			fi
			rm -f $tempfile
			rm -f $tempfile2
			gv=""
			gd=""
		fi
		;;
	    b)
		get_input "Input volume and compander profile (vol,prof):" 0,13
		ret=$?
		if [ $ret -eq 0 ]; then
			Pop b
			gv=$(echo $b | cut -d ',' -f 1)
			gc=$(echo $b | cut -d ',' -f 2)
			ls *.mp3 > $tempfile
			dialog_checklist "FILTER" "Select MP3s for setting to $gv dB and compander profile $gc" $tempfile
			ret=$?
			if [ $ret -eq 0 ] || [ $ret -eq 3 ]; then
				n=$(cat $tempfile2 | wc -l)
				i=0
				while [ $i -lt $n ]; do
					let ++i
					s=$(Get_Lines $tempfile2 $i $i)
					ret=$?
					show_info "Please wait until modifications are done ..."
					audiofilterb -q -- $gv $gc $s
					ret=$?
				done
				kde_pop
			fi
			rm -f $tempfile
			rm -f $tempfile2
			gd=""
		fi
		;;
	    d)
		get_input "Input dynaudnorm profile (0-$maxprof):" 0
		ret=$?
		if [ $ret -eq 0 ]; then
			Pop gd
			ls *.mp3 > $tempfile
			dialog_checklist "FILTER" "Select MP3s for setting to dynaudnorm profile $gd" $tempfile
			ret=$?
			if [ $ret -eq 0 ] || [ $ret -eq 3 ]; then
				n=$(cat $tempfile2 | wc -l)
				i=0
				while [ $i -lt $n ]; do
					let ++i
					s=$(Get_Lines $tempfile2 $i $i)
					ret=$?
					show_info "Please wait until modifications are done ..."
					audiofilterd -q -- $gd $s
					ret=$?
				done
				kde_pop
			fi
			rm -f $tempfile
			rm -f $tempfile2
			gv=""
			gc=""
		fi
		;;
	    u)
		get_input "Input dynaudnorm profile (0-$maxprof), (prof,vol):" 0,3
		ret=$?
		if [ $ret -eq 0 ]; then
			Pop b
			gd=$(echo $b | cut -d ',' -f 1)
			gv=$(echo $b | cut -d ',' -f 2)
			ls *.mp3 > $tempfile
			dialog_checklist "FILTER" "Select MP3s for setting to dynaudnorm profile $gd and volume $gv" $tempfile
			ret=$?
			if [ $ret -eq 0 ] || [ $ret -eq 3 ]; then
				n=$(cat $tempfile2 | wc -l)
				i=0
				while [ $i -lt $n ]; do
					let ++i
					s=$(Get_Lines $tempfile2 $i $i)
					ret=$?
					show_info "Please wait until modifications are done ..."
					audiofilteru -q -- $gd $gv $s
					ret=$?
				done
				kde_pop
			fi
			rm -f $tempfile
			rm -f $tempfile2
			gc=""
		fi
		;;
	    *)
		show_err "9" "Flag for audiofilter not found"
		;;
	esac
fi

return $ret
}

#=============================================================================
function fselect_help ()
{
# Show the fselect help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG fselect_help: $# $@" >&2
	$debuglog fselect_help: $# $@
fi

local dtitle='HELP FOR FSELECT' msgtxt=""
local ret=0

msgtxt="The screen is divided into 4 areas.

+---------------------------------++---------------------------------+
|                                 ||                                 |
|       Directory window          ||          Files window           |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
|                                 ||                                 |
+---------------------------------++---------------------------------+
|  Input line                                                        |
+--------------------------------------------------------------------+
|                            Button area                             |
+--------------------------------------------------------------------+

Moving between the 4 areas is done by UP DN LEFT RIGHT HOME END.

The main thing is the input line.
Dir/File selection is only done from the input line.
It exist a line completion function. Enter the beginning of a dir/file
and hit SPACE. The input line will be completed if possible.

To enter a dir name use one of the following ways:
a) Go to the directory window, select your directory then hit SPACE.
   The directory will appear in the input line and the cursor is 
   positioned at the directory name.
b) From the input line enter a directory name or at least the first few 
   characters and hit SPACE. The input line will be completed if possible.

In both cases be sure that the rightmost char is a '/'

To enter a file name use one of the following ways:
a) Go to the file window, select your file then hit SPACE.
   The file will appear in the input line and the cursor is positioned 
   at the file name.
b) From the input line enter a file name or at least the first few 
   characters and hit SPACE. The input line will be completed if possible."

echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function dialog_fselect ()
{
# List all dirs and files and select one file or a dir
# par1 = dtitle
# par2 = startdir
# Uses globals ansfile, startdir
# Uses cat, dialog, echo, logger, rm
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG dialog_fselect: $# $@" >&2
	$debuglog dialog_fselect: $# $@
fi

local dtitle="$1" ans="" d1="" d2="" quit=no
local l=0 ret=0
startdir="$2"

while [ "$quit" != yes ]; do
	# fselect: show a file selection box, h w = 15 80
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --fselect filepath height width 2>$ansfile
	dialog --title "$dtitle" --ok-label "VolInfo" --extra-button --extra-label "Make" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --hline "${hline}" --fselect $startdir 15 80 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	if [ -n "$ans" ]; then
		l=${#ans}
		let --l
		if [ ${ans:$l} == "/" ]; then		# ans is a dir
			startdir="${ans}"
		else				# no trail. / -> may be a file
			startdir=$(dirname ${ans})
		fi
		d2=$(basename $startdir)
		d1=$(dirname $startdir)
		d1=$(basename $d1)
		hline="/${d1}/${d2}/"
	fi
	case "$ret" in
	    0)						# Yes or OK button
		fselect_ok "$ans"
		ret=$?
		#quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		fselect_help
		ret=$?
		;;
	    3)						# Extra button
		fselect_extra "$ans"
		ret=$?
		#quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done

return $ret
}

#=============================================================================
function menu_ok3 ()
{
# Do menu things.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_ok3: $# $@" >&2
	$debuglog menu_ok3: $# $@
fi

local ans="$1" ans="$1" msgtxt="menu_ok3: ans=$ans"
local ret=0

#show_msg "$msgtxt"
#ret=$?

case "$ans" in
    1)							# show all volinfos
	show_info "Please wait until volume infos are ready ..."
	audiovolinfo -qa
	ret=$?

	kde_pop
	# show volinfo file
	show_volinfo
	ret=$?

	# overwrite orig MP3s ?
	overwrite_old
	ret=$?
	;;
    2)							# show n* volinfos
	ls n*.mp3 > $tempfile2
	ret=$?
	if [ $ret -eq 0 ]; then
		show_info "Please wait until volume infos are ready ..."
		n=$(cat $tempfile2 | wc -l)
		i=0
		while [ $i -lt $n ]; do
			let ++i
			s=$(Get_Lines $tempfile2 $i $i)
			ret=$?
			ss="$ss $s"
		done
		audiovolinfo $ss > $tempfile
		kde_pop
		show_textfile "${hline}" $tempfile
		ret=$?
	fi
	rm -f $tempfile
	rm -f $tempfile2

	# overwrite orig MP3s ?
	overwrite_old
	ret=$?
	;;
    3)							# select manually
	ls *.mp3 > $tempfile
	# checklist liefert die auswahl in tempfile2
	dialog_checklist "FILTER" "Select MP3s for volinfo" $tempfile
	ret=$?
	if [ $ret -eq 0 ]; then
		show_info "Please wait until volume infos are ready ..."
		n=$(cat $tempfile2 | wc -l)
		i=0
		while [ $i -lt $n ]; do
			let ++i
			s=$(Get_Lines $tempfile2 $i $i)
			ret=$?
			ss="$ss $s"
		done
		audiovolinfo $ss > $tempfile
		kde_pop
		show_textfile "${hline}" $tempfile
		ret=$?
	fi
	rm -f $tempfile
	rm -f $tempfile2

	# overwrite orig MP3s ?
	overwrite_old
	ret=$?
	;;
    *)						# Others
	show_error 9 "Unknown menu3 entry."
	ret=9
	;;
esac

return $ret
}

#=============================================================================
function menu_extra3 ()
{
# Do extra things. Show the helptext.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_extra3: $# $@" >&2
	$debuglog menu_extra3: $# $@
fi

local ans="$1" dtitle='DO MENU EXTRA3' msgtxt=''
local ret=0

msgtxt="Extra Button pressed, menu number=$ans"
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function menu_help3 ()
{
# Show the menu help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_help3: $# $@" >&2
	$debuglog menu_help3: $# $@
fi

local dtitle='HELP FOR MENU3' msgtxt=""
local ret=0

msgtxt="Select an entry with UP/DOWN then hit OK"
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function dialog_menu3 ()
{
# Show a volinfo menu with dialog.
# par1 = 
# Uses global ansfile
# Uses cat, dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG dialog_menu3: $# $@" >&2
	$debuglog dialog_menu3: $# $@
fi

local dtitle='MENU' wtitle='Select a menu entry for VolInfo' ans='' quit=no
local ti_list=''
local ret=0
#local -a arr
#local maxarr="${#arr[@]}"		# get number of elements in arr
#unset arr[@]				# remove the entire array

# NO-BREAK SPACE = ' '
# [tag item] ...				separate with TAB or NL
ti_list="1 Select ALL songs
2 Select n* songs
3 Select INDIVIDUAL songs"

while [ "$quit" != yes ]; do
	# menu: show a menu, h w mh = 25 80 18
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --menu "$wtitle" height width menu-height [tag item] ... 2>$ansfile
	dialog --title "$dtitle" --ok-label "Ok" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --menu "$wtitle" 10 60 3 $ti_list 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	case "$ret" in
	    0)						# Yes or OK button
		menu_ok3 "$ans"
		ret=$?
		quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		menu_help3
		ret=$?
		;;
	    3)						# Extra button
		menu_extra3 "$ans"
		ret=$?
		#quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done

return $ret
}

#=============================================================================
function menu_ok2 ()
{
# Do menu things.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_ok2: $# $@" >&2
	$debuglog menu_ok2: $# $@
fi

local ans="$1"
local ret=0

case "$ans" in
    1)
	aflt=v
	;;
    2)
	aflt=c
	;;
    3)
	aflt=b
	;;
    4)
	aflt=d
	;;
    5)
	aflt=u
	;;
    *)
	;;
esac
ret=$?

return $ret
}

#=============================================================================
function menu_extra2 ()
{
# Do extra things. Show the helptext.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_extra2: $# $@" >&2
	$debuglog menu_extra2: $# $@
fi

local ans="$1" dtitle='DO_MENU_EXTRA' msgtxt=''
local ret=0

msgtxt="Extra Button pressed, menu number=$ans"
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function menu_help2 ()
{
# Show the menu help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_help2: $# $@" >&2
	$debuglog menu_help2: $# $@
fi

local dtitle='HELP FOR MENU' msgtxt=""
local ret=0

msgtxt="Select an entry with UP/DOWN then hit OK"
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function dialog_menu2 ()
{
# Show a make menu with dialog.
# par1 = 
# Uses global ansfile
# Uses cat, dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG dialog_menu2: $# $@" >&2
	$debuglog dialog_menu2: $# $@
fi

local dtitle='MENU' wtitle='Select a menu entry for processing' ans='' quit=no
local ti_list=''
local ret=0
#local -a arr
#local maxarr="${#arr[@]}"		# get number of elements in arr
#unset arr[@]				# remove the entire array

# NO-BREAK SPACE = ' '
# [tag item] ...				separate with TAB or NL
ti_list="1 Change VOLUME
2 Use COMPANDER
3 Change VOLUME and use COMPANDER
4 Use DYNAUDNORM
5 Use DYNAUDNORM and change VOLUME"

while [ "$quit" != yes ]; do
	# menu: show a menu, h w mh = 25 80 18
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --menu "$wtitle" height width menu-height [tag item] ... 2>$ansfile
	dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --hline "Last  v:$gv  c:$gc  d:$gd" --menu "$wtitle" 12 60 5 $ti_list 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	case "$ret" in
	    0)						# Yes or OK button
		menu_ok2 "$ans"
		ret=$?
		quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		menu_help2
		ret=$?
		;;
	    3)						# Extra button
		menu_extra2 "$ans"
		ret=$?
		#quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done

return $ret
}

#=============================================================================
function menu_ok ()
{
# Do menu things.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_ok: $# $@" >&2
	$debuglog menu_ok: $# $@
fi

local ans="$1"
local ret=0

case "$ans" in
    1)
	dialog_fselect "VolInfo and Make" "$startdir"
	;;
    2)
	show_volinfo
	;;
    3)
	dialog_fselect2 "Delete files and Update dir" "$startdir"
	;;
    4)
	show_about
	;;
    *)
	;;
esac
ret=$?

return $ret
}

#=============================================================================
function menu_extra ()
{
# Do extra things. Show the helptext.
# par1 = menu number
# Uses echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_extra: $# $@" >&2
	$debuglog menu_extra: $# $@
fi

local dtitle="MANPAGE" ans="$1"
local ret=0

#show_msg "menu_extra: Extra Button, ans=$ans"

helptext > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function menu_help ()
{
# Show the menu help text.
# par1 = 
# Uses dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG menu_help: $# $@" >&2
	$debuglog menu_help: $# $@
fi

local dtitle='HELP FOR MENU' msgtxt=""
local ret=0

msgtxt="Select an entry with UP/DOWN then hit OK"
echo "$msgtxt" > $tempfile

show_textfile "$dtitle" "$tempfile"
ret=$?
rm -f $tempfile

return $ret
}

#=============================================================================
function mainproc ()
{
# Show the main menu with dialog.
# par1 = 
# Uses global ansfile
# Uses cat, dialog, echo, logger
# Return codes:
# Standard

if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG mainproc: $# $@" >&2
	$debuglog mainproc: $# $@
fi

local dtitle='MAIN MENU' wtitle='Select a menu entry' ans='' quit=no
local ti_list=''
local ret=0
#local -a arr
#local maxarr="${#arr[@]}"		# get number of elements in arr
#unset arr[@]				# remove the entire array

# NO-BREAK SPACE = ' '
# [tag item] ...				separate with TAB or NL
ti_list="1 VolInfo and Make
2 Show last VolInfo
3 Delete and Update Files
4 About AudioVolMan"

while [ "$quit" != yes ]; do
	# menu: show a menu, h w mh = 25 80 18
	#dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Extra" --cancel-label "Cancel" --help-button --help-label "Help" --no-shadow --no-tags --menu "$wtitle" height width menu-height [tag item] ... 2>$ansfile
	dialog --title "$dtitle" --ok-label "Ok" --extra-button --extra-label "Man" --cancel-label "Quit" --help-button --help-label "Help" --no-shadow --no-tags --hline "${hline}" --menu "$wtitle" 25 80 18 $ti_list 2>$ansfile
	ret=$?
	ans=$(cat $ansfile)
	rm -f $ansfile
	case "$ret" in
	    0)						# Yes or OK button
		menu_ok "$ans"
		ret=$?
		#quit=yes
		;;
	    1|255)					# No or Cancel or ESC
		quit=yes
		ret=1
		;;
	    2)						# Help button
		menu_help
		ret=$?
		;;
	    3)						# Extra button
		menu_extra "$ans"
		ret=$?
		#quit=yes
		;;
	    *)						# Others
		show_error 99 "From dialog = $ret"
		quit=yes
		ret=99
		;;
	esac
done
clear

return $ret
}

##############################################################################
# GET OPTIONS
##############################################################################
# Give 2 strings and all cmdline_args to func GetOpt
# (POSIX_options Gnu_long_options cmdline_args):
# GetOpt 'hVdqvi:m:' 'help version debug quiet verbose infile= myval=' $@
#
# A ':' after a POSIX option means this option needs an arg.
# A '=' after a Gnu long option means this option needs an arg.
#
# All cmd/options have to be initialised to ''!
#
# POSIX options are classified in 2 categories:
#   a) cmds (mutually exclusive). All cmds are given to var 'cmd'.
#   b) options (as many as you like). Each option is given to it's own var.
# As a general rule: If cmds/options are given multiple then last given wins.
#
# Options can take 1 argument (no whitespace in arg). If you want to give more
# than 1 arg or want to use multiple strings as 1 arg then concatenate them
# together with ',' as delimiter:
# sampleprog -m val1,val2
# sampleprog --myval=val1,val2
# It's at the programmers responsibility to do meaningfull things with the
# content of the options.
#
# If err != 0 then all args are processed, else continue to look for options.
# When GetOpt finishes then all cmds/options are removed from actual
# cmdline args.
#
err=0
while [ "$err" -eq 0 ]; do
	GetOpt 'hVd' 'help version debug' $@
	err=$?
	#echo "DEBUG GET OPTIONS: err=${err}  _OPTNAME_=${_OPTNAME_}  _OPTVAL_=${_OPTVAL_}  _OPTPOS_=${_OPTPOS_}  _OPTIDX_=${_OPTIDX_}" >&2
	if [ $err -eq 0 ]; then
		case "$_OPTNAME_" in
		    h|help)	cmd="-h";;
		    V|version)	cmd="-V";;
#		    a)		cmd="-${_OPTNAME_}";;
		    d|debug)	debug=yes;;
#		    q|quiet)	verbose=no;;
#		    v|verbose)	verbose=yes;;
#		    o|output)	opt_o=yes;;
#		    i|infile)	opt_i="${_OPTVAL_}";;
#		    o|outfile)	opt_o="${_OPTVAL_}";;
#		    p|pages)	opt_p="${_OPTVAL_}";;
		    --)		break;;
		    :)	Show_Err ${_ERR_MISSING_ARG_FOR_OPT_} "-${_OPTVAL_}"
			exit ${_ERR_MISSING_ARG_FOR_OPT_};;
		    ?)	Show_Err ${_ERR_UNKNOWN_OPT_} "-${_OPTVAL_}"
			exit ${_ERR_UNKNOWN_OPT_};;
		    *)	Show_Err ${_ERR_UNKNOWN_ERR_} "_OPTNAME_=${_OPTNAME_} _OPTVAL_=${_OPTVAL_}"
			exit ${_ERR_UNKNOWN_ERR_};;
		esac
	fi
done
err=0
shift ${_OPTIDX_}				# rm all opts from cmdline
#
# Uncomment if giving a cmd is mandatory
#if [ -z "$cmd" ]; then					# error no cmd given
#	Show_Err 7 ""
#	exit 7
#fi

##############################################################################
# CHECK LINK NAMES

##############################################################################
# CHECK OPTIONS

##############################################################################
# CHECK VERSION

##############################################################################
# CHECK ARGUMENTS
#
case "$cmd" in
#    '')
#	if [ "$#" -eq 1 ]; then				# parameter count ok
#		arg1="$1"
#		shift
#		#args="$@"
#		#shift $#
#	else						# error in param count
#		Show_Err ${_ERR_WRONG_PAR_COUNT_} ""
#		exit ${_ERR_WRONG_PAR_COUNT_}
#	fi
#	;;
    *)
	if [ "$#" -ne 0 ]; then				# error in param count
		Show_Err ${_ERR_WRONG_PAR_COUNT_} ""
		exit ${_ERR_WRONG_PAR_COUNT_}
	fi
	;;
esac

##############################################################################
# LOCK

##############################################################################
# MAINPROGRAM
##############################################################################
#
if [ "$debug" == "yes" ]; then				# DEBUG
	#echo "DEBUG Main: $# $@" >&2
	$debuglog Main: $# $@
fi

# Check what to do (sub commands, options and args are removed from cmdline)
case "$cmd" in
    '')
	mainproc
	err=$?
	;;
    -h)
	helptext
	exit 0
	;;
    -V)
	echo "${scriptname}: (${package}) $package_file ${version}-${release}"
	exit 0
	;;
    *)							# error
	Show_Err ${_ERR_SUB_CMD_NOT_FOUND_} ""
	err=${_ERR_SUB_CMD_NOT_FOUND_}
	;;
esac

#rm -f $lockfile

exit $err
