#!/bin/bash
#
# File: /usr/local/bin/togglegpgswitch
#
# Toggle {en|dis}abling of gpgswitch.
#
#    Copyright (C) 2008  Juergen Kaesmann (JK)
#    Modified 2013 by Ingo Kaesmann (inka)
#
#    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
#
# Exit codes:
# 0 = OK
# 1 = Unknown command or option
# 2 = Error in parameter count
# 3 = Other error

##############################################################################
# SOURCE FILES

. /usr/local/etc/main-inka-sh.conf			# config main
#. $LLIBDIR/func-std.sh					# functions general

##############################################################################
# DECLARATION OF VARIABLES

# STRINGS
pkgname=ksm-gpgswitch-inka
version=$(grep "^VERSION" $PROGREG/$pkgname.inf | cut -f 2)	# version
#lib_func_std_sh_needed=0.2.0
#lib_func_ip_sh_needed=0.2.0
#lib_func_cddvd_sh_needed=0.3.0
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=yes/''/no
cmd_found=no						# cmd_found=yes/no
opt_found=no						# opt_found=yes/no
cmd=''							# cmd
#------------------End Of Standard Variables----------------------------------
ffile="/var/local/$pkgname/gpgswitch.on"		# flag file

# NUMBERS
err=0
##############################################################################
# FUNCTIONS

#-----------------------------------------------------------------------------
function helptext ()
{
# Output helptext
# Uses: cat
# Return codes:
# Standard

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

cat <<EOT

Aufruf: $scriptname [OPTION]
Schaltet gpgswitch ein oder aus, bzw. zeigt den Status an.

        -e      schaltet gpgswitch ein
        -d      schaltet gpgswitch aus
        -s      zeigt den Status an
-h, --help      Hilfe anzeigen und beenden
 --version      Versionsinformationen anzeigen und beenden

Voreinstellung: Zwischen Ein und Aus umschalten.

EOT

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

# Read the cmd, 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 cmds -h and -V directly.
# Exit on error.

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

# get command (1. cmdline parameter)
case $1 in
  -h|--help)	cmd='-h'; shift; cmd_found='yes';;	# cmd -h
  --version)	cmd='--v'; shift; cmd_found='yes';;	# cmd --v
  -d|-e|-s)	cmd="$1"; shift; cmd_found='yes';;	# cmd found
#  *)	echo "Error - unknown command $1 !" >&2		# error
#	exit 1;;
esac

# get options
#while [ "${1:0:1}" == "-" ]; do			# get all options
#	case $1 in
#	  -d|--debug)	debug=yes;;			# option -d
#	  -q|--quiet)	verbose=no;;			# option -q
#	  -v|--verbose)	verbose=yes;;			# option -v
#	  *)	echo "Error - unknown option $1 !" >&2	# error
#		exit 1;;
#	esac
#	shift
#	opt_found=yes
#done

# handle standard commands -h, --v
case $cmd in
  -h)	helptext; exit 0;;				# give help
  --v)	echo "$scriptname (${pkgname}) $version"; exit 0;; # output version
esac

# check libraries S I C
#Check_Libs

# check version of config files
#if [ "$version" != "$sample_cnf" ] || [ "$version" != "$sample_rc" ]; then
#	if [ "$verbose" != no ]; then
#		echo "Error in version of config files!" >&2
#	else
#		$errlog version of config files differ
#	fi
#	exit 3
#fi

# check arguments after commands and options
case "$cmd" in
#  -x)	if [ "$#" -eq 1 ]; then				# parameter count ok
#		arg1="$1"
#		shift
#	else						# error in param count
#		echo "Error in parameter count!" >&2
#		exit 2
#	fi;;
  *)	if [ "$#" -ne 0 ]; then				# error in param count
		echo "Fehler - Parameteranzahl falsch!" >&2
		exit 2
	fi;;
esac

##############################################################################
# MAINPROGRAM
# Uses: echo, ln, rm, test

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

# Check what to do (initial cmds, options and args are removed from cmdline)
case "$cmd" in
  '')
	if test -e ${ffile}; then
		rm ${ffile}
		echo "gpgswitch wurde ausgeschaltet."
	else
		cp /dev/null ${ffile}
		echo "gpgswitch wurde eingeschaltet."
	fi;;
  -d)
		rm -f ${ffile}
		echo "gpgswitch wurde ausgeschaltet.";;
  -e)
		cp /dev/null ${ffile}
		echo "gpgswitch wurde eingeschaltet.";;
  -s)
	if test -e ${ffile}; then
		echo "gpgswitch ist eingeschaltet."
	else
		echo "gpgswitch ist ausgeschaltet."
	fi;;
  *)
	echo "Fehler - Option unbekannt!" >&2	# error
	exit 1;;
esac
