#!/bin/bash
#
# File: /usr/local/bin/chchar-german
#
# change special german characters to us-ascii, or us-ascii-characters
# to special german characters
#
#    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
# 8 = Error in other file or other file not found or not readable
##############################################################################
# SOURCE FILES


. /usr/local/etc/main-inka-sh.conf			# main config
. $LLIBDIR/lib-inka-std.sh				# functions standard
test -e /usr/local/etc/chchar-german.conf && . /usr/local/etc/chchar-german.conf	# global config
##############################################################################
# DECLARATION OF VARIABLES

# STRINGS
pkgname=inka-utilities					# package name
lib_inka_std_sh_needed=1.3.6				# standard lib version
conf_file_needed=0.1.3					# 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 -------------------------
arg1=""							# file

# NUMBERS
err=0							# exit code
##############################################################################
# INFORMATION ABOUT SED
# the general form
#sed -e SEDCMD1 $arg1
# delete line 'abc'
#SEDCMD1="/abc/d"
# replace 'abc' with 'def'
#SEDCMD1="s/abc/def/"
##############################################################################
# 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]... -s|-U|-u DATEI
Aendert deutsche Sonderzeichen u. Euroz. im Text aus DATEI in us-ascii-Zeichen,
bzw. us-ascii-Zeichen in deutsche Sonderzeichen und gibt auf stdout aus

        -s      us-ascii-Zeichen in deutsche Sonderzeichen aendern
        -U      deutsche Sonderzeichen in us-ascii-Zeichen aendern
                  (grosse Sonderz. --> AE, OE, UE)
        -u      deutsche Sonderzeichen in us-ascii-Zeichen aendern
                  (grosse Sonderz. --> Ae, Oe, Ue)
-h, --help      Hilfe anzeigen und beenden
 --version      Versionsinformationen anzeigen und beenden
--show-config   Konfiguration anzeigen und beenden
   --debug      Debug-Modus benutzen
        -q      keine Meldungen

"EURO" und "ss" werden nicht in Sonderzeichen umgewandelt.
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

Deutsche Umlaute muessen in folgenden Zeilen lesbar sein:
Ae   = $UmlAe
Oe   = $UmlOe
Ue   = $UmlUe
ae   = $Umlae
oe   = $Umloe
ue   = $Umlue
ss   = $Umlss
EURO = $UmlEURO

EOT

return 0
}
#-----------------------------------------------------------------------------
function usascii_to_special_german ()
{
# Change us-ascii chars to special german chars
# AE/Ae --> special german character, OE/Oe --> special german character,
# UE/Ue --> special german character, ae --> special german character,
# oe --> special german character, ue --> special german character
# Uses: echo, sed
# return codes: standard

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

local SEDCMD1 SEDCMD2 SEDCMD3 SEDCMD4 SEDCMD5 SEDCMD6 SEDCMD7 SEDCMD8 SEDCMD9 SEDCMD10 SEDCMD11
SEDCMD1="s/AE/${UmlAe}/g"
SEDCMD2="s/OE/${UmlOe}/g"
SEDCMD3="s/UE/${UmlUe}/g"
SEDCMD4="s/Ae/${UmlAe}/g"
SEDCMD5="s/Oe/${UmlOe}/g"
SEDCMD6="s/Ue/${UmlUe}/g"
SEDCMD7="s/ae/${Umlae}/g"
SEDCMD8="s/oe/${Umloe}/g"
SEDCMD9="s/ue/${Umlue}/g"
SEDCMD10="s/a${Umlue}/aue/g"				# correction
SEDCMD11="s/e${Umlue}/eue/g"				# correction

# change chars and output to stdout
sed -e ${SEDCMD1} $arg1 | sed -e ${SEDCMD2} | \
sed -e ${SEDCMD3} | sed -e ${SEDCMD4} | sed -e ${SEDCMD5} | \
sed -e ${SEDCMD6} | sed -e ${SEDCMD7} | sed -e ${SEDCMD8} | \
sed -e ${SEDCMD9} | sed -e ${SEDCMD10} | sed -e ${SEDCMD11}

return 0
}
#-----------------------------------------------------------------------------
function special_german_to_usascii_gg ()
{
# Change special german chars to us-ascii chars
# Special german character --> AE, special german character --> OE,
# Special german character --> UE, special german character --> ae,
# Special german character --> oe, special german character --> ue,
# Special german character --> ss, Euro-Sign --> EURO
# Uses: echo, sed
# Return codes: standard

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

local SEDCMD1 SEDCMD2 SEDCMD3 SEDCMD4 SEDCMD5 SEDCMD6 SEDCMD7 SEDCMD8
SEDCMD1="s/${UmlAe}/AE/g"
SEDCMD2="s/${UmlOe}/OE/g"
SEDCMD3="s/${UmlUe}/UE/g"
SEDCMD4="s/${Umlae}/ae/g"
SEDCMD5="s/${Umloe}/oe/g"
SEDCMD6="s/${Umlue}/ue/g"
SEDCMD7="s/${Umlss}/ss/g"
SEDCMD8="s/${UmlEURO}/EURO/g"

# change chars and output to stdout
sed -e ${SEDCMD1} $arg1 | sed -e ${SEDCMD2} | \
sed -e ${SEDCMD3} | sed -e ${SEDCMD4} | sed -e ${SEDCMD5} | \
sed -e ${SEDCMD6} | sed -e ${SEDCMD7}

return 0
}
#-----------------------------------------------------------------------------
function special_german_to_usascii_gk ()
{
# Change special german chars to us-ascii chars
# Special german character --> Ae, special german character --> Oe,
# Special german character --> Ue, special german character --> ae,
# Special german character --> oe, special german character --> ue,
# Special german character --> ss, Euro-Sign --> EURO
# Uses: echo, sed
# Return codes: standard

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

local SEDCMD1 SEDCMD2 SEDCMD3 SEDCMD4 SEDCMD5 SEDCMD6 SEDCMD7 SEDCMD8
SEDCMD1="s/${UmlAe}/Ae/g"
SEDCMD2="s/${UmlOe}/Oe/g"
SEDCMD3="s/${UmlUe}/Ue/g"
SEDCMD4="s/${Umlae}/ae/g"
SEDCMD5="s/${Umloe}/oe/g"
SEDCMD6="s/${Umlue}/ue/g"
SEDCMD7="s/${Umlss}/ss/g"
SEDCMD8="s/${UmlEURO}/EURO/g"

# change chars and output to stdout
sed -e ${SEDCMD1} $arg1 | sed -e ${SEDCMD2} | \
sed -e ${SEDCMD3} | sed -e ${SEDCMD4} | sed -e ${SEDCMD5} | \
sed -e ${SEDCMD6} | sed -e ${SEDCMD7} | sed -e ${SEDCMD8}

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 --version 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 --------------------------
	  -U|-u|-s)	opt="$1";;			# option -U, -u, -s
	  *)	if [ "$verbose" != "-q" ]; then
			echo "Fehler - Option $1 falsch!" >&2	# error
		else
			$errlog unknown option $1
		fi
		exit 1;;
	esac
	shift
	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 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!
Compare_Version $conf_file_needed $ChChar_German_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
	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 conffile 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

# check arguments after options
case "$opt" in
  -U|-u|-s)	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 file exist and readable
test -r $arg1
if [ "$?" != "0" ]; then
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - Datei $arg1 nicht vorhanden oder nicht lesbar!" >&2
	else
		$errlog file not exist or not readable
	fi
	exit 8
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
  -s)	usascii_to_special_german			# opt -s
	err=$?;;
  -U)	special_german_to_usascii_gg			# opt -U
	err=$?;;
  -u)	special_german_to_usascii_gk			# opt -u
	err=$?;;
  "")							# no opt given
	if [ "$verbose" != "-q" ]; then
		echo "Fehler - Option fehlt!" >&2
	else
		$errlog option missing
	fi
	err=1;;
esac

exit $err
