#!/bin/bash
#
# File: /usr/local/bin/inka-pkgmanager
#
# User interface for package management programs
# uses inkapkginform, inkapkginstall, inkapkgupdate
#
#    Copyright (C) 2013  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.0
#
# Exit codes:
# 0 = OK
# 1 = Error in parameters
# 2 = Error in configfile, configfile old or not found
# 3 = Library old or not found
##############################################################################
# SOURCE FILES

. /usr/local/etc/main-inka-sh.conf			# config main
. /usr/local/etc/inka-basis.conf			# global config
. $LLIBDIR/lib-inka-std.sh				# functions general
. $LLIBDIR/lib-inka-basis.sh				# functions general
##############################################################################
# DECLARATION OF VARIABLES

# STRINGS
pkgname=inka-basis					# package name
lib_inka_std_sh_needed=1.3.6				# standard lib version
lib_inka_basis_sh_needed=1.3.3
conf_file_needed=1.3.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 -------------------------
head="\
  ___________________________________________________________________________
 /                                                                           \\
 |                       i n k a - p k g m a n a g e r                       |
 \\___________________________________________________________________________/"

plinemain="\
 /                                                                           \\
 | Menue:                                                                    |
 \\___________________________________________________________________________/"

mlinemain="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                                   |
 | K(o)nfiguration...                                                        |
 | In(f)ormationen ueber inka-pkgmanager...                                  |
 | (i)nstallierte Pakete...                                                  |
 | (a)ngebotene Pakete...                                                    |
 \\___________________________________________________________________________/"

plineconfig="\
 /                                                                           \\
 | Menue: Konfiguration                                                      |
 \\___________________________________________________________________________/"

mlineconfig="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | (a)nzeige der Konfiguration                                               |
 | (e)ditieren der Konfiguration                                             |
 \\___________________________________________________________________________/"

plineinfopkgmanager="\
 /                                                                           \\
 | Menue: Info ueber inka-pkgmanager                                         |
 \\___________________________________________________________________________/"

mlineinfopkgmanager="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | Anzeige von In(f)ormationen ueber inka-pkgmanager                         |
 | Anzeige der GNU General Public (l)icense Version 2                        |
 \\___________________________________________________________________________/"

plineinstalledpkg="\
 /                                                                           \\
 | Menue: Installierte Pakete                                                |
 \\___________________________________________________________________________/"

mlineinstalledpkg="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | Anzeige aller (i)nstallierter Pakete mit Informationen                    |
 | Anzeige ge(s)uchter installierter Pakete                                  |
 | Anzeige der Informations(l)iste eines Pakets                              |
 | Anzeige der Aenderun(g)en eines Pakets                                    |
 | Anzeige eines (p)akets, zu dem eine Datei gehoert                         |
 | Deinstallation - Konfig-Dateien beh(a)lten...                             |
 | Deinstallation - v(o)llstaendig...                                        |
 \\___________________________________________________________________________/"

plineuninstallsave="\
 /                                                                           \\
 | Menue: Installierte Pakete / Deinstallation - Konfig-Dateien behalten     |
 \\___________________________________________________________________________/"

plineuninstallcompl="\
 /                                                                           \\
 | Menue: Installierte Pakete / Deinstallation - vollstaendig                |
 \\___________________________________________________________________________/"

mlineuninstall="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | (o)k, deinstallieren                                                      |
 \\___________________________________________________________________________/"

plineservedpkg="\
 /                                                                           \\
 | Menue: Angebotene Pakete                                                  |
 \\___________________________________________________________________________/"

mlineservedpkg="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | Anzeige aller Server mit (p)aketen                                        |
 | Anzeige angebotener Pakete von Servern mit (i)nfos                        |
 | Installation von Paketen aus (a)uswahl aller nichtinstallierten...        |
 | Installation von ge(s)uchten Paketen...                                   |
 | Herunter(l)aden eines Pakets...                                           |
 | Akt(u)alisierung aller Pakete...                                          |
 \\___________________________________________________________________________/"

plineinstall="\
 /                                                                           \\
 | Menue: Angebotene Pakete / Installation (Auswahl/Suche)                   |
 \\___________________________________________________________________________/"

mlineinstall="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | (o)k, installieren                                                        |
 \\___________________________________________________________________________/"

plinedownload="\
 /                                                                           \\
 | Menue: Angebotene Pakete / Herunterladen eines Pakets                     |
 \\___________________________________________________________________________/"

mlinedownload="\
 /                                                                           \\
 | (q)uit                          (h)ilfe                         Zu(r)ueck |
 | (o)k, herunterladen                                                       |
 \\___________________________________________________________________________/"

# NUMBERS
err=0							# exit code
##############################################################################
# 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]
Paketverwaltung fuer inka-Pakete (tarballs)

-h, --help      Hilfe anzeigen und beenden
 --version      Versionsinformationen anzeigen und beenden
--show-config   Konfiguration anzeigen und beenden

EOT

return
}
#-----------------------------------------------------------------------------
function main_menue ()
{
# Build main menue and get there commands.
# Variables: head, mlinemain, plinemain
# Uses: clear, echo, read
# Uses: config_menue, help_menue, info_basis_menue, installed_pkg_menue, served_pkg_menue
# Return codes: 0 = ok

local cmd="" i=""

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

	read cmd
	case "$cmd" in
		q)	clear;exit 0;;
		h)	help_menue;;
		o)	config_menue;;
		f)	info_pkgmanager_menue;;
		i)	installed_pkg_menue;;
		a)	served_pkg_menue;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function help_menue ()
{

$PAGER <<EOT

                       Hilfe fuer inka-pkgmanagement

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

Der obere Bereich zeigt den Namen des Programms an.
Der mittlere Bereich zeigt die aktuelle Position im Menue an.
Der untere Bereich zeigt die aktuell waehlbaren Befehle an.

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

Die in runden Klammern stehenden Buchstaben koennen eingegeben werden.
Abschliessend an eine Eingabe ist die Enter-Taste zu druecken.

Bei Auswahlmoeglichkeiten mehrerer Server oder Pakete nach einer
Befehlseingabe werden diese im Editor dargestellt.
Die Zeilen mit nicht gewuenschten Eintraegen sind zu entfernen, so dass nur
noch die Zeilen uebrigbleiben, die die gewuenschte Auswahl enthaelt.
Die Datei danach speichern und den Editor beenden.

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

Die Ausgabe der Befehle erfolgt fast immer in einem Pager.
Nach dem Lesen den Pager schliessen.
Wenn Ausgabe nicht im Pager erfolgt, nach dem Lesen Enter-Taste druecken.

==============================================================================
                                 Navigation
Quit
Hilfe
Konfiguration
	|-- Quit
	|-- Zurueck
	|-- Anzeige der Konfiguration
	\`-- Editieren der Konfiguration

Informationen ueber inka-pkgmanager
	|-- Quit
	|-- Zurueck
	|-- Anzeige von Infos
	\`-- Anzeige der Lizenz

Installierte Pakete
	|-- Quit
	|-- Zurueck
	|-- Anzeige aller installierter Pakete mit Infos
	|-- Anzeige gesucherter installierter Pakete
	|-- Anzeige Informationsliste eines Pakets
	|-- Anzeige Aenderungen eines Pakets
	|-- Anzeige eines Pakets zu dem eine Datei gehoert
	|-- Deinstallation (Konfig-Dateien behalten)
	|	|-- Quit
	|	|-- Zurueck
	|	\`-- Ok, deinstallieren
	|
	\`-- Deinstallation (vollstaendig)
		|-- Quit
		|-- Zurueck
		\`-- Ok, deinstallieren

Angebotene Pakete
	|-- Quit
	|-- Zurueck
	|-- Anzeige aller Server mit Paketen
	|-- Anzeige angebotener Pakete von Servern mit Infos
	|-- Installation Pakete aus Auswahl aller nichtinstallierten
	|	|-- Quit
	|	|-- Zurueck
	|	\`-- Ok, installieren
	|
	|-- Installation von gesuchten Paketen
	|	|-- Quit
	|	|-- Zurueck
	|	\`-- Ok, installieren
	|
	|-- Herunterladen eines Pakets
	|	|-- Quit
	|	|-- Zurueck
	|	\`-- Ok, herunterladen
	|
	\`-- Aktualisieren aller Pakete
		|-- Quit
		|-- Zurueck
		\`-- Ok, installieren

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

Installations-Server (URLs):
EOT
for s in $UrlList; do
	echo "$s"					# show all urls
done
cat <<EOT

Lokales Paketverzeichnis von .tgz, .tar.gz, .tar, .tar.bz2:
$PackageDir

Bei automatischer Aktualisierung nicht zu ueberpruefende / nicht herunterzuladende Pakete:
EOT
for s in $ExList; do
	echo "$s"					# show excluded packages
done
cat <<EOT

Einstellung fuer automatischen Aktualisierungslauf
-s = nur Anzeige neuer Paketversionen
-x = Installation neuer Paketversionen
Einstellung: $DefCmd

Aktualisierungslauf fuer Pakete erfolgt zu folgenden Zeiten:
EOT
grep "&&" /etc/cron.d/inkapkgupdate.cron | cut -f 1
cat <<EOT

Aktualisierungslauf fuer inka-basis erfolgt zu folgenden Zeiten:
EOT
grep "&&" /etc/cron.d/inkabasisupdater.cron | cut -f 1

return
}
#-----------------------------------------------------------------------------
function config_menue ()
{
# Build config menue and get there commands.
# Variables: head, mlineconfig, plineconfig, EDITOR, PAGER
# Uses: clear, echo, read, inkapkgupdate
# Uses: help_menue
# Return codes: 0 = ok


local cmd="" i=""

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

	read cmd
	case "$cmd" in
		q)	clear;exit 0;;
		h)	help_menue;;
		r)	return 0;;
		a)	show_config | $PAGER;;
		e)	$EDITOR /usr/local/etc/inka-basis.conf;. /usr/local/etc/inka-basis.conf;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function info_pkgmanager_menue ()
{
# Build info pkgmanager menue, show infos about inka-pkgmanager
# and get there commands.
# Variables: head, mlineinfopkgmanager, plineinfopkgmanager
# Uses: cat, clear, echo, read
# Uses: help_menue
# Return codes: 0 = ok


local cmd="" i=""

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

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_menue;;
		r)	rm -f $tempfile;return 0;;
		f)	# show output-text
			echo "" > $tempfile
			echo "$scriptname" >> $tempfile
			echo "Version: $version" >> $tempfile
			echo "copyright (C) 2012, 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;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function installed_pkg_menue ()
{
# Build installed pkg menue and get there commands
# Variables: head, mlineinstalledpkg, plineinstalledpkg
# Uses: clear, echo, read
# Uses: help_menue uninstall_menue
# Return codes: 0 = ok


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

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

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_menue;;
		r)	rm -f $tempfile;return 0;;
		i)	inkapkginform -i | $PAGER;;	# show all inst. p. name, vers., description
		s)	# search for installed packages and show it
			clear
			echo -e "Name des Pakets (auch Fragment): \c"
			read string
			inkapkginform -a > $tempfile
			grep "$string" $tempfile | $PAGER;;
		l)	# show file list of an inst. package
			clear
			echo -e "Name des Pakets (auch Fragment): \c"
			read string
			inkapkginform -f $string | $PAGER;;
		g)	# show changelog of an inst. package
			clear
			echo -e "Name des Pakets (auch Fragment): \c"
			read string
			inkapkginform -c $string | $PAGER;;
		p)	# show an inst. package this contains a file named serchstring
			clear
			echo -e "Name der Datei (auch Fragment): \c"
			read string
			inkapkginform -p $string | $PAGER;;
		a)	# uninstall an inst. package (save config files)
			inkapkginform -a > $tempfile
			$EDITOR $tempfile
			uninstall_save_menue;;
		o)	# uninstall an inst. package complete
			inkapkginform -a > $tempfile
			$EDITOR $tempfile
			uninstall_compl_menue;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function uninstall_save_menue ()
{
# Subfunction of installed_pkg_menue
# Uninstall packages, but save config files
# Variables: head, mlineuninstall, plineuninstallsave
# Uses: clear, echo, read, rm
# Uses: help_menue
# Return codes: 0 = ok


local uninst="" cmd="" cmd2="" i=""

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

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_menue;;
		r)	return 0;;
		o)	# ok for uninstalling packages
			uninst=$(cat $tempfile)
			for f in $uninst; do inkapkginstall -u $f;done
			echo "Weiter mit Enter-Taste."
			read cmd2
			case "$cmd2" in
				q)	rm -f $tempfile;clear;exit 0;;
				h)	help_menue;;
				*)	return 0;;
			esac;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function uninstall_compl_menue ()
{
# Subfunction of installed_pkg_menue
# Uninstall packages complete
# Variables: head, mlineuninstall, plineuninstallcompl
# Uses: clear, echo, read, rm
# Uses: help_menue
# Return codes: 0 = ok


local uninst="" cmd="" cmd2="" i=""

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

	read cmd
	case "$cmd" in
		q)	rm -f $tempfile;clear;exit 0;;
		h)	help_menue;;
		r)	return 0;;
		o)	# ok for uninstalling packages
			uninst=$(cat $tempfile)
			for f in $uninst; do inkapkginstall -U $f;done
			echo "Weiter mit Enter-Taste."
			read cmd2
			case "$cmd2" in
				q)	rm -f $tempfile;clear;exit 0;;
				h)	help_menue;;
				*)	return 0;;
			esac;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function served_pkg_menue ()
{
# Build served pkg menue and get there commands
# Variables: head, mlineconfig, plineconfig
# Uses: clear, cp, echo
# Uses: help_menue
# Return codes: 0 = ok


local cmd="" string="" urls="" i="" tfile1=$tempfile.spm.1.tmp tfile2=$tempfile.spm.2.tmp g=0
while :; do
	# show quasi-windows
	clear
	echo "$head"
	echo "$plineservedpkg"
	echo "$mlineservedpkg"

	read cmd
	case "$cmd" in
		q)	rm -f $tfile1 $tfile2;clear;exit 0;;
		h)	help_menue;;
		r)	rm -f $tfile1 $tfile2;return 0;;
		p)	inkapkginform -S | $PAGER;;	# show all servers with packages
		i)	# show index.html/README of every wished server
			cp /dev/null $tfile1
			for f in $UrlList;do echo $f >> $tfile1;done
			$EDITOR $tfile1
			urls=$(cat $tfile1)
			for f in $urls;do inkapkginform -I $f | $PAGER;done;;
		a)	# install packages of a list of all not installed packages
			inkapkginform -N > $tfile1
			$EDITOR $tfile1
			install_menue $tfile1;;
		s)	# search for packages and install (from all availables packages)
			inkapkginform -A > $tfile1
			clear
			echo -e "Name des Pakets (auch Fragment):  \c"
			read string	#
			grep "$string" $tfile1 > $tfile2
			if [ $? -ne 0 ]; then
				echo "Kein Paket mit diesem Namen gefunden." | $PAGER
			else
				$EDITOR $tfile2
				install_menue $tfile2
			fi;;
		l)	# search for packages and download it
			inkapkginform -A > $tfile1
			clear
			echo -e "Name des Pakets (auch Fragment):  \c"
			read string
			grep "$string" $tfile1 > $tfile2
			if [ $? -ne 0 ]; then
				echo "Kein Paket mit diesem Namen gefunden." | $PAGER
			else
				$EDITOR $tfile2
				download_menue $tfile2
			fi;;
		u)	# show files with new version of installed packages and ask for installing
			inkapkgupdate --shownewfiles > $tfile1
			$EDITOR $tfile1
			install_menue $tfile1;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function install_menue ()
{
# Subfunction of served_pkg_menue
# Install packages
# Par1: File this contains package file names to install
# Variables: head, mlineinstall, plineinstall,
# Uses: clear, echo, read, rm
# Uses: help_menue
# Return codes: 0 = ok


local p1=$1 pkgfiles"" cmd="" cmd2="" i=""

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

	read cmd
	case "$cmd" in
		q)	rm -f $tfile1 $tfile2;clear;exit 0;;
		h)	help_menue;;
		r)	return 0;;
		o)	# ok for installing
			pkgfiles=$(cat $p1)
			for f in $pkgfiles;do inkapkginstall -i $f;done
			echo "Weiter mit Enter-Taste."
			read cmd2		# push key after reading output
			case "$cmd2" in
				q)	rm -f $tfile1 $tfile2;clear;exit 0;;
				h)	help_menue;;
				*)	return 0;;
			esac;;
	esac
done

return 0
}
#-----------------------------------------------------------------------------
function download_menue ()
{
# Subfunction of served_pkg_menue
# Download packages
# Par1: File this contains package file names to download
# Variables: head, mlinedownload, plinedownload
# Uses: clear, echo, read, rm
# Uses: help_menue
# Return codes: 0 = ok


local p1=$1 pkgfiles"" cmd="" cmd2="" i=""

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

	read cmd
	case "$cmd" in
		q)	rm -f $tfile1 $tfile2;clear;exit 0;;
		h)	help_menue;;
		r)	return 0;;
		o)	# ok for download
			pkgfiles=$(cat $p1)
			for f in $pkgfiles;do inkapkginstall -n $f;done
			echo "Weiter mit Enter-Taste."
			read cmd2		# push key after reading output
			case "$cmd2" in
				q)	rm -f $tfile1 $tfile2;clear;exit 0;;
				h)	help_menue;;
				*)	return 0;;
			esac;;
	esac
done

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, --show-config 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
#-------------------------- end of standard options --------------------------
	  *)
		echo "Fehler - Option $1 falsch!" >&2	# error
		exit 1;;
	esac
	shift
	opt_found=yes
done

# handle standard options -h, -V, -C
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 std 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
	echo "Fehler - Programm benoetigt neuere Version von lib-inka-std.sh!" >&2
	exit 3
elif [ $ret -eq 9 ] || [ $ret -eq 127 ]; then		# other error (version or lib missing)
	echo "Fehler - Versionspruefung von lib-inka-std.sh nicht moeglich!"
	exit 3
else					# check ok, reset $ret
	ret=0
fi

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

# check version of config files				# set config var!
Compare_Version $conf_file_needed $Inka_Basis_Conf
#Compare_Version $conf_file_needed $Program_Rc
ret=$?					# ret: 0,1,2,9
if [ $ret -eq 1 ]; then		# needed config-version higher than installed
	echo "Fehler - $scriptname benoetigt neuere Version der Konfig-datei!" >&2
	exit 2
elif [ $ret -eq 9 ]; then		# # other error (version or conffile missing)
	echo "Fehler - Versionspruefung der Konfig-datei von $scriptname nicht moeglich!"
	exit 2
else					# check ok, reset $ret
	ret=0
fi

# check arguments after options
case "$opt" in
  *)	if [ "$#" -ne 0 ]; then				# error in param count
		echo "Fehler - Parameteranzahl falsch!" >&2
		exit 1
	fi;;
esac
##############################################################################
# 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
  "")							# no option given
	main_menue
	err=$?;;
esac

exit $err
