#!/bin/bash
#
# File: /usr/local/bin/ikmuplayer
#
# Interaktive Benutzeroberflaeche im Textmodus fuer mplayer
#
#    Copyright (C) 2017  Ingo Kaesmann
#
#    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
#
#
# Script template: 0.3.6
#
# Exit codes:
# 0 = OK
# 1 = Error in parameters
# 2 = Error in configfile, configfile old or not found
# 3 = Library old or not found
# 8 = Error in other file or other file not found
# 20= Another instance of program is running
# 99= Other error
##############################################################################
# SOURCE STANDARD FILES

. /usr/local/etc/main-inka-sh.conf			# main config
. $LLIBDIR/lib-inka-std.sh				# functions standard
#test -e /usr/local/etc/x.conf && . /usr/local/etc/x.conf	# global config
test -e ~/.$(basename $0)rc && . ~/.$(basename $0)rc	# personal config
#test -e ~/.xrc && . ~/.xrc				# local config
##############################################################################
# DECLARATION OF VARIABLES

# STRINGS
pkgname=inka-media					# package name
lib_inka_std_sh_needed=1.3.6				# standard lib version
conf_file_needed=1.1.0					# conf file version
#----------------------- end of variables to configure -----------------------
version=$(grep "^VERSION" $PROGREG/$pkgname.inf | cut -f 2)	# version
scriptname=$(basename $0)				# name of script
tempfile=$TEMPDIR/$scriptname.$UID.$$.tmp		# temporary file
infolog="logger -p user.info -t $scriptname --"		# log info messages
errlog="logger -p user.err -t $scriptname -- ERROR:"	# log error messages
debuglog="logger -p user.debug -t $scriptname -- DEBUG:" # log debug messages
debug=no						# debug=yes/no
verbose=""						# verbose=""/-q/-v
opt_found=no						# opt_found=yes/no
opt=""							# option
#------------------------- end of standard variables -------------------------
plist=".ikmuplayer/plist.m3u"				# play list
econf=".ikmuplayer/equal.conf"				# equalizer config file
rep="$Rep"						# repeat music on/off
shuf="$Shuf"						# shuffle on/off
equal="$Equal"						# equalizer on/off
actpath="$Musicdir"				# actual path of music files
repeat=""						# repeat command
shuffle=""						# shuffle command
equalizer=""						# equalizer command (start)
equalval=""						# equalizer command
eqname=""						# equalizer name

# NUMBERS
err=0							# exit code
eqno=0							# equalizer number
#-----------------------------------------------------------------------------
eqbgline="\
 |                  ------------+------------                                |\r\c"

eqsline="\
 |                  0000000000000000000000000\r\c"


uline="\
 /                                                                           \\"

eline="\
 |                                                                           |"

bgline="\
                                                                             |\r\c"

gline="\
 \\___________________________________________________________________________/"

head="\
  ___________________________________________________________________________
 /                                                                           \\
 |                        i  k  m  u  p  l  a  y  e  r                       |
 \\___________________________________________________________________________/"

mlinemain="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                                   |
 |                                                                           |
 | K(o)nfiguration...                                     Programm-(I)nfo... |
 |                                 (s)tart                                   |
 | (d)ateien hinzufuegen                              (n)eue Wiedergabeliste |
 | Verze(i)chnis hinzufuegen                       Wiedergabeliste (a)nsehen |
 | Dateien per Su(c)he hinzufuegen              Wiedergabeliste b(e)arbeiten |
 |                                                                           |
 | eigene Wiedergabeliste    (S)peichern         (L)aden         (E)ntfernen |
 \\___________________________________________________________________________/"

mlineconfig="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         zu(r)ueck |
 |                                                                           |
 | (a)nzeigen der Konfiguration              an(z)eigen der Equalizer-Einst. |
 | (e)ditieren der Konfiguration            e(d)itieren der Equalizer-Einst. |
 \\___________________________________________________________________________/"

mlineinfoikmuplayer="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         zu(r)ueck |
 |                                                                           |
 | anzeigen von (i)nformationen ueber ikmuplayer                             |
 | anzeigen der GNU General Public (l)icense Version 2                       |
 \\___________________________________________________________________________/"

mlineokorno="\
 /                                                                           \\
 |                                 (h)ilfe                                   |
 |                                                                           |
 | (o)k, Auswahl uebernehmen                               (n)ein, abbrechen |
 \\___________________________________________________________________________/"

mlineaddfiles="\
 /                                                                           \\
 |                                 (h)ilfe                                   |
 |                                                                           |
 | (o)k, Auswahl uebernehmen     (n)ein, abbrechen     anderes V(e)rzeichnis |
 \\___________________________________________________________________________/"

hklinemplayer="\
  ___________________________________________________________________________
 /                                                                           \\
 |                   H o t k e y s   d e s   m p l a y e r                   |
 |                                                                           |
 | (Links)    10 Sek. zurueck                         (Rechts)   10 Sek. vor |
 | (Runter)    1 Min. zurueck                         (Hoch)      1 Min. vor |
 | (Bild R)   10 Min. zurueck                         (Bild H)   10 Min. vor |
 |                                                                           |
 | (<)   vorheriger Titel                              (>)   naechster Titel |
 |                                                                           |
 | (/)   leiser               (m)   stumm ein/aus               (*)   lauter |
 |                                                                           |
 | (q) / (Esc)   stop                      (p) / (Leertaste)   Pause ein/aus |
 \\___________________________________________________________________________/"


##############################################################################
# FUNCTIONS

function show_help ()
{
# Show help
# Uses: cat
# Return codes: standard

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

cat <<EOT

Aufruf: $scriptname [OPTION]...
Interaktive Textkonsolen-Benutzerschnittstelle fuer mplayer.

-h, --help      Hilfe anzeigen und beenden
--show-config   Programmkonfiguration anzeigen und beenden
 --version      Versionsinformationen anzeigen und beenden
   --debug      Debug-Modus benutzen
        -q      ohne Meldungen
        -v      mit vielen Meldungen

EOT
return
}
#-----------------------------------------------------------------------------
function help_proc ()
{
# Show help in interactive program mode.
# Uses:$PAGER
# Return codes: 0 = ok

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

$PAGER <<EOT

                           Hilfe fuer ikmuplayer

==============================================================================
                              Aufbau der Ansicht

Der obere Bereich zeigt den Namen des Programms an.
Der untere Bereich zeigt die aktuell waehlbaren Befehle an.
Ggf. gibt  es  einen  weiteren  Bereich  darunter,  der  weitere  Befehle  und
Zustaende anzeigt.

==============================================================================
                                    Eingabe

Die  in  runden  Klammern stehenden Buchstaben muessen je nach Angabe entweder
als Klein- oder Grossbuchstaben eingegeben werden.  Bei  (Nr)  ist  eine  Zahl
einzugeben. Abschliessend an eine Eingabe ist die Enter-Taste zu druecken.

Konfigurationsdateien werden im Editor bearbeitet.  Datei speichern und Editor
beenden. Die Aenderungen werden sofort uebernommen.

Auswahlen  werden  im Editor angezeigt.  Bei einzeiligen Auswahlmoeglichkeiten
ist die gewuenschte Zeile an die oberste Position zu setzen.  Nur  diese  wird
ausgewertet.   Bei   mehrzeiligen  Auswahlmoeglichkeiten  werden  alle  Zeilen
ausgewertet. Die Datei danach speichern und den Editor beenden.

Die Hotkeys des mplayers werden von diesem direkt ausgewertet.

==============================================================================
                                    Ausgabe

Die  Ausgabe  der  Befehle  erfolgt  entweder  im  in  der Systemkonfiguration
eingestellten Textbetrachter oder auf einem vorher geleerten Bildschirm.

==============================================================================
                                    Betrieb

Beim  Hinzufuegen  von  Dateien  zur  Wiedergabeliste  ist  das   anzuzeigende
Verzeichnis eine einzeilige Auswahlmoeglichkeit,  die Dateien sind jedoch eine
mehrzeilige.

Beim  Hinzufuegen  von  Verzeichnissen  zur Wiedergabeliste handelt es sich um
eine  einzeilige   Auswahlmoeglichkeit.  Jedoch   werden   alle   Dateien   in
evtl. vorhandenen Unterverzeichnissen uebernommen.

Beim  Hinzufuegen  von  Dateien zur Wiedergabeliste per Suchfunktion muss eine
Zeichenfolge angegeben werden.  Alle Verzeichnisse  oder  Dateien,  die  diese
Zeichenfolge  enthalten,  werden im Editor angezeigt.  Es ist eine mehrzeilige
Auswahlmoeglichkeit.

Die   Wiedergabeliste   kann   unter   einem   beliebigen   Namen  als  eigene
Wiedergabeliste gespeichert und zu einem spaeteren  Zeitpunkt  wieder  geladen
werden. 

Das  Entfernen  einer  eigenen  zu  einem  frueheren  Zeitpunkt  gespeicherten
Wiedergabeliste ist eine einzeilige Auswahlmoeglichkeit.

==============================================================================
                             Hotkeys des mplayers:
(Links)    10 Sek. zurueck                              (Rechts)   10 Sek. vor
(Runter)    1 Min. zurueck                              (Hoch)      1 Min. vor
(Bild R)   10 Min. zurueck                              (Bild H)   10 Min. vor

(<)   vorheriger Titel                                   (>)   naechster Titel
(/)   leiser                  (m)   stumm ein/aus                 (*)   lauter
(q) / (Esc)   stop                           (p) / (Leertaste)   Pause ein/aus

==============================================================================

EOT
return 0
}
#-----------------------------------------------------------------------------
function show_config ()
{
# Show configuration of program
# Uses: cat, cut, grep
# Return codes: standard

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

cat <<EOT

Pfad zur Musik: $Musicdir

Voreinstellung Wiederholung der Wiedergabeliste: $Rep

Voreinstellung Zufallsreihenfolge der Musikstuecke: $Shuf

Voreinstellung Equalizer: $Equal

EOT

return
}
#-----------------------------------------------------------------------------
function main_menue ()
{
# Mainmenue.
# Uses: clear, echo, read, rm
# Return codes: 0 = ok

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

local cmd="" string="" lline=0

# read equalizer config
lline=$(cat ~/$econf | wc -l)
eqno=$(Read_Lines ~/$econf $lline $lline)
equalval=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d '"' -f 2)
eqname=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d "#" -f 2)

while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$mlinemain"
	echo "$uline"
	echo -e "$bgline"
	echo " | Wiede(r)holung:        $rep"
	echo -e "$bgline"
	echo " | Z(u)fallsreihenfolge:  $shuf"
	echo -e "$bgline"
	echo -e "                                (Nr): $eqno   Name:$eqname \r\c"
	echo " | Equali(z)er:           $equal"
	echo "$gline"

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_proc;;
		o)	config_menue;;			# menue of show config
		I)	info_ikmuplayer_menue;;		# menue of info about ikmuplayer
		s)	start_proc;;			# window of shortkey info and start
		d)	add_files_proc;;		# add files
		i)	add_dir_proc;;			# add dir
		c)	add_searched_files_proc;;	# add files by searching
		n)	cp /dev/null ~/$plist;;		# new playlist
		a)	$PAGER ~/$plist;;		# show playlist
		e)	$EDITOR ~/$plist;;		# edit playlist
		S)					# save playlist as own name
			clear;echo "vorhandene Wiedergabelisten:";echo ""
			ls ~/.ikmuplayer/*.m3u
			echo ""
			echo -e "Name der neuen eigenen Wiedergabeliste (ohne .m3u):  \c"
			read string
			cp ~/$plist ~/.ikmuplayer/$string.m3u;;
		L)					# select own playlist to play
			ls ~/.ikmuplayer/*.m3u | grep -v plist.m3u > $tempfile;$EDITOR $tempfile
			line=$(Read_Lines $tempfile 1 1);cat $line > ~/$plist;;
		E)					# delete own playlist
			ls ~/.ikmuplayer/*.m3u | grep -v plist.m3u > $tempfile;$EDITOR $tempfile
			line=$(Read_Lines $tempfile 1 1);rm $line;;
		r)	if [ "$rep" == "on" ];then rep="off";else rep="on";fi;;
		u)	if [ "$shuf" == "on" ];then shuf="off";else shuf="on";fi;;
		z)	if [ "$equal" == "on" ];then equal="off";else equal="on";fi;;
		*)					# number of equalizer
			if [ $cmd -ge 1 ] && [ $cmd -le 1000 ] >&2; then
				cat ~/$econf | grep -q ^EqualVal-${cmd}-
				if [ $? -eq 0 ]; then
					eqno=$cmd
					equalval=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d '"' -f 2)
					eqname=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d "#" -f 2)
					lline=$(cat ~/$econf | wc -l)
					Erase_Lines ~/$econf $lline $lline > $tempfile
					cp $tempfile ~/$econf
					echo $eqno >> ~/$econf
					rm $tempfile
				fi
			fi;;
	esac
done
rm -f $tempfile
return 0
}
#-----------------------------------------------------------------------------
function config_menue ()
{
# Build config menue and get there commands
# Uses: clear, echo, read, rm
# Return codes: 0 = ok

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

local cmd fqall fq0 fq1 fq2 fq3 fq4 fq5 fq6 fq7 fq8 fq9 fqr1 fqr2 fqr3 fqr4 fqr5 fqr6 fqr7 fqr8 fqr9 fqg0 fqg1 fqg2 fqg3 fqg4 fqg5 fqg6 fqg7 fqg8 fqg9 m
cmd=""
fqall=""
fq0=""
fq1=""
fq2=""
fq3=""
fq4=""
fq5=""
fq6=""
fq7=""
fq8=""
fq9=""
fqr1=""
fqr2=""
fqr3=""
fqr4=""
fqr5=""
fqr6=""
fqr7=""
fqr8=""
fqr9=""
fqg0=""
fqg1=""
fqg2=""
fqg3=""
fqg4=""
fqg5=""
fqg6=""
fqg7=""
fqg8=""
fqg9=""
m=0

while :; do
	# show quasi-windows
	clear
	# fq0 - fq9
	# equalval: -af equalizer=-0:1:2:3:4:5:6:7:-8:-9
	fqall=${equalval#*=}		# -0:1:2:3:4:5:6:7:-8:-9
	fq9=${fqall##*:}		# -9
	fqr9=${fqall%:*}		# -0:1:2:3:4:5:6:7:-8
	fq8=${fqr9##*:}			# -8
	fqr8=${fqr9%:*}			# -0:1:2:3:4:5:6:7
	fq7=${fqr8##*:}			# 7
	fqr7=${fqr8%:*}			# -0:1:2:3:4:5:6
	fq6=${fqr7##*:}			# 6
	fqr6=${fqr7%:*}			# -0:1:2:3:4:5
	fq5=${fqr6##*:}			# 5
	fqr5=${fqr6%:*}			# -0:1:2:3:4
	fq4=${fqr5##*:}			# 4
	fqr4=${fqr5%:*}			# -0:1:2:3
	fq3=${fqr4##*:}			# 3
	fqr3=${fqr4%:*}			# -0:1:2
	fq2=${fqr3##*:}			# 2
	fqr2=${fqr3%:*}			# -0:1
	fq1=${fqr2##*:}			# 1
	fqr1=${fqr2%:*}			# -0
	fq0=${fqr1##*:}			# 0
	echo "$head"
	echo "$mlineconfig"
	echo "$uline"
	echo -e "$bgline"
	echo " | Equalizer  (Nr): $eqno   Name:$eqname"
	echo "$eline"
	echo " | Band    Hz       -12         0         +12                                |"
	echo -e "$eline"
	# value + 33
	echo -e "$eqbgline"
	m=$((fq9+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  10    16000     ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq8+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  9     8000      ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq7+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  8     4000      ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq6+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  7     2000      ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq5+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  6     1000      ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq4+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  5     500       ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq3+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  4     250       ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq2+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  3     125       ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq1+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  2     62,5      ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$eqbgline"
	m=$((fq0+33))
	echo -e "${eqsline:0:$m}\r\c"
	line=" |  1     31,25     ------------+------------"
	m=$((m-1))
	echo "${line:0:$m}"
	echo -e "$gline"
	read cmd
	case "$cmd" in
		q)	clear;exit 0;;
		h)	help_proc;;
		r)	return;;
		a)	show_config | $PAGER;;
		e)	$EDITOR ~/.ikmuplayerrc;. ~/.ikmuplayerrc;;
		z)				# show equalizer setup
			cat ~/$econf | grep ^EqualVal | $PAGER;;
		d)				# select equalizer setup
			$EDITOR ~/$econf
			equalval=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d '"' -f 2)
			eqname=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d "#" -f 2);;
		*)					# number of equalizer
			if [ $cmd -ge 1 ] && [ $cmd -le 1000 ] >&2; then
				cat ~/$econf | grep -q ^EqualVal-${cmd}-
				if [ $? -eq 0 ]; then
					eqno=$cmd
					equalval=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d '"' -f 2)
					eqname=$(cat ~/$econf | grep ^EqualVal-${eqno}- | cut -d "#" -f 2)
					lline=$(cat ~/$econf | wc -l)
					Erase_Lines ~/$econf $lline $lline > $tempfile
					cp $tempfile ~/$econf
					echo $eqno >> ~/$econf
					rm $tempfile
				fi
			fi;;



	esac
done
return 0
}
#-----------------------------------------------------------------------------
function info_ikmuplayer_menue ()
{
# Shows infos about ikmuplayer and shows the license.
# Uses: cat, clear, echo, read, rm
# Return codes: 0 = ok

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

local cmd="" i=""

while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$mlineinfoikmuplayer"

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_proc;;
		r)	rm -f $tempfile;return;;
		i)					# show ikmuplayer info
			echo "" > $tempfile
			echo "$scriptname" >> $tempfile
			echo "Version: $version" >> $tempfile
			echo "copyright (C) 2016, Ingo Kaesmann ingo@ingokaesmann.de" >> $tempfile
			echo "Dieses Programm wird unter den Bedingungen der GPL Vers. 2 veroeffentlicht." >> $tempfile
			echo "Es wird OHNE GARANTIE vertrieben." >> $tempfile
			cat $tempfile | $PAGER;;
		l)	cat /usr/local/share/doc/packages/$pkgname/GPL-2.0.txt | $PAGER;;	# show gpl
	esac
done
return 0
}
#-----------------------------------------------------------------------------
function add_files_proc ()
{
# Add selected files to playlist.
# Uses: cat, clear, echo, read, rm, tree
# Return codes: 0 = ok

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

local cmd="" i=""

clear
tree -FfiL 1 --dirsfirst --noreport $actpath > $tempfile	# list all dirs/files in actuel path in tempfile
$EDITOR $tempfile				# work tempfile
actpath=$(Read_Lines $tempfile 1 1)		# actuel path

while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$mlineaddfiles"

	read cmd
	case "$cmd" in
		h)	help_proc;;
		o)	cat $tempfile >> ~/$plist;rm -f $tempfile;return;;	# add files of selected dir to playlist
		n)	rm -f $tempfile;return;;				# escape
		e)								# select more
			tree -FfiL 1 --dirsfirst --noreport $actpath > $tempfile	# list all dirs/files in actuel path in tempfile
			$EDITOR $tempfile						# work tempfile
			actpath=$(Read_Lines $tempfile 1 1);;				# actuel path
	esac
done
return 0
}
#-----------------------------------------------------------------------------
function add_dir_proc ()
{
# Add files of a selected dir to playlist.
# Uses: clear, echo, read, rm, tree
# Return codes: 0 = ok

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

local cmd="" i="" line=""

clear
tree -dfi --noreport $Musicdir > $tempfile	# list all music dirs in tempfile
$EDITOR $tempfile				# work tempfile

while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$mlineokorno"

	read cmd
	case "$cmd" in
		h)	help_proc;;
		o)				# add files of selected dir to playlist
			line=$(Read_Lines $tempfile 1 1)	# dir
			tree -fi --noreport $line >> ~/$plist	# write music files to playlist
			rm -f $tempfile;return;;
		n)	rm -f $tempfile;return;;	# ecape
	esac
done
return 0
}
#-----------------------------------------------------------------------------
function add_searched_files_proc ()
{
# Add searched selected files to playlist.
# Uses: cat, clear, echo, read, rm, tree
# Return codes: 0 = ok

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

local cmd="" i="" string=""

clear
echo -e "Zeichenfolge eingeben:  \c"		# ask for searching string
read string					#  read it

tree -fi --noreport $Musicdir | grep $string > $tempfile	# list all dirs/files they contain string in tempfile
$EDITOR $tempfile				# work tempfile

while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$mlineokorno"

	read cmd
	case "$cmd" in
		h)	help_proc;;
		o)	cat $tempfile >> ~/$plist;rm -f $tempfile;return;;	# add files of selected dir to playlist
		n)	rm -f $tempfile;return;;				# escape
	esac
done
return 0
}
#-----------------------------------------------------------------------------
function start_proc ()
{
# show mplayer's hotkeys.
# Uses: clear, echo, mplayer
# Return codes: 0 = ok

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

# show quasi-windows
clear
echo "$hklinemplayer"

# create command variables
if [ "$rep" == "on" ]; then			# repeat on/off
	repeat="-loop 0"
else
	repeat=""
fi
if [ "$shuf" == "on" ]; then			# shuffle on/off
	shuffle="-shuffle"
else
	shuffle=""
fi
if [ "$equal" == "on" ]; then			# equalizer on/off
	equalizer="$equalval"
else
	equalizer=""
fi

# start mplayer
mplayer -nogui -msgcolor -nolirc $repeat $shuffle $equalizer -playlist ~/$plist

return 0
}
##############################################################################
# PARSE COMMANDLINE

# Read all options with args and all additional args into variables.
# Stop parsing after last parameter.
# Try to do a check on all parameters and version numbers.
# Handle options -h, -C and -V directly.
# Exit on error.

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

# get options and options with arguments
while [ "${1:0:1}" == "-" ]; do				# get all options
	case "$1" in
	  -h|--help)	opt=-h;;			# option -h
	  --version)	opt=--v;; 			# option --version
	  --show-config) opt=--s;;			# option --show-config
	  --debug)	debug=yes;;			# option --debug
	  -q|--quiet)	verbose=-q;;			# option -q
	  -v|--verbose)	verbose=-v;;			# option -v
#-------------------------- end of standard options --------------------------
#	  -x|-y|-z)	opt="$1";;			# option -xyz
#	  -z)	opt_z=$1;;				# option -z
#	  -z)	opt_z=$1				# option -z + 1 arg
#		if [ "$#" -gt 1 ] & [ "${2:0:1}" != "-" ]; then	# par2 is 1.arg of opt_z
#			opt_z1=$2
#			shift				# shift option
#		else					# error no arg
#			if [ "$verbose" != -q ]; then
#				echo "Fehler - Wert fuer Option $1 fehlt!" >&2
#			else
#				$errlog arg for option $1 is missing
#			fi
#			exit 1
#		fi;;
	  *)	if [ "$verbose" != "-q" ]; then
			echo "Fehler - Option $1 falsch!" >&2	# error
		else
			$errlog unknown option $1
		fi
		exit 1;;
	esac
	shift			# shift option, or last argument for option
	opt_found=yes
done

# handle standard options -h, --v, --s
case "$opt" in
  -h)	show_help; exit 0;;					# show help
  --v)	echo "$scriptname (${pkgname}) $version"; exit 0;;	# show version
  --s)	show_config; exit 0;;					# show config
esac

# check arguments after options
case "$opt" in
#  -x|-y|-z)	if [ $# -eq 1 ]; then		# parameter count ok
#				arg1="$1"
#				shift
#		else				# error in param count
#			if [ "$verbose" != "-q" ]; then
#				echo "Fehler - Parameteranzahl falsch!" >&2
#			else
#				$errlog parameter count wrong
#			fi
#			exit 1
#		fi;;
  *)	if [ $# -ne 0 ]; then			# error in param count
		if [ "$verbose" != "-q" ]; then
			echo "Fehler - Parameteranzahl falsch!" >&2
		else
			$errlog parameter count
		fi
		exit 1
	fi;;
esac
##############################################################################
# CHECK LIBRARY AND CONFIG

# check library
Compare_Version $lib_inka_std_sh_needed $Lib_Inka_Std_Sh_Version
ret=$?						# (ret: 0,1,2,9,127)
if [ $ret -eq 1 ]; then		# needed lib-version higher than installed
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - Programm benoetigt neuere Version von lib-inka-std.sh!" >&2
	else
		$errlog newer version of lib-inka-std.sh needed
	fi
	exit 3
elif [ $ret -eq 9 ] || [ $ret -eq 127 ]; then		# other error (version or lib missing)
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - Versionspruefung von lib-inka-std.sh nicht moeglich!"
	else
		$errlog check of version of lib-inka-std.sh impossible
	fi
	exit 3
else					# check ok, reset $ret
	ret=0
fi

# check version of config files - set config var and program name!
#Compare_Version $conf_file_needed $Program_Conf
Compare_Version $conf_file_needed $Ikmuplayer_Rc
ret=$?						# (ret: 0,1,2,9)
if [ $ret -eq 1 ]; then		# needed config-version higher than installed
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - $scriptname benoetigt neuere Version der Konfig-datei!" >&2
	else
		$errlog newer version of conf file needed
	fi
	exit 2
elif [ $ret -eq 9 ]; then		# other error (version of conf file missing)
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - Versionspruefung der Konfig-datei von $scriptname nicht moeglich!"
	else
		$errlog check of version of conf file impossible
	fi
	exit 2
else					# check ok, reset $ret
	ret=0
fi
##############################################################################
# MAINPROGRAM

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

# Check what to do (initial options and args are removed from cmdline)
case "$opt" in
#  -x|-y)						# opt -xy
#	main_proc
#	err=$?;;
  "")	main_menue;;					# no opt given
#	if [ "$verbose" != "-q" ]; then
#		echo "Fehler - Option fehlt!" >&2
#	else
#		$errlog option missing
#	fi
#	err=1;;
esac

exit $err
