#!/bin/bash
#set -n		# read but do not execute commands (check on syntax errors)
#
# File: /usr/local/bin/mediaoverlay
#
# Overlay a video with an image.
#
#    Copyright (c) 2023  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
#
##############################################################################
# REPLACE STRINGS
#
##############################################################################
# EXIT CODES

##############################################################################
# SOURCE FILES
#
. /usr/local/etc/main.conf				# config main
#. $LLIBDIR/libfuncerr-sh				# functions error
. $LLIBDIR/libfuncstd-sh				# functions standard
#. $LLIBDIR/libfuncmath2-sh				# functions math2
#. $LLIBDIR/libfuncip-sh				# functions IP
#. /usr/local/etc/sample.conf				# config sample

#if [ ! -e ~/.samplerc ]; then				# install config user
#	cp /usr/local/share/ksm-sample/samplerc ~/.samplerc
#	echo "Please configure your ~/.samplerc"
#	exit 1
#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=$(/bin/cat $vinfopath/VERSION)		# package version
readonly release=$(/bin/cat $vinfopath/RELEASE)		# package release
readonly scriptname=$(basename $0)			# name of script
#readonly tempfile=$TEMPDIR/$scriptname.$UID.tmp	# temporary file name
#tempfile=$(mktemp $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=${_ERR_OK_}						# error code

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

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

##############################################################################
# USER-CONSTANTS

##############################################################################
# USER-VARIABLES
#
# STRINGS
#args=''				# all args from cmdline
arg1=''					# 1. arg from cmdline
arg2=''					# 2. arg from cmdline
arg3=''					# 3. arg from cmdline
arg4=''					# 4. arg from cmdline
arg5=''					# 5. 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)

# 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

##############################################################################
# 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 - Overlay a video with an image.

SYNOPSIS
$scriptname [-p] [options] [inf img xpos ypos outf]
$scriptname -L|-R|-l|-r [options] [inf img outf]
$scriptname -B|-b [options] [inf img img2 outf]

DESCRIPTION
Overlay a video with one or two images. We should use transparent PNG
images. Theese images will be overlayed over the main video. Overlay
position counts from the left,top corner (0,0).

Movies which are stored in DVD format (4:3) but use widescreen (16:9)
are distorted anamorph. mediainfo shows it (DAR 16:9). So images for
theese movies have to be distorted too before we overlay them.

I found that following formula gives good results:

  distorted width = image width / 1.4

The image height retains untouched. Scale only the image width to the
distorded width.

Defaults:
  Make a solid overlay

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.
  -B           = Auto overlay 2 images, first in the left top corner
                 second in the right top corner.
  -L           = Auto overlay image in the left top corner.
  -R           = Auto overlay image in the right top corner.
  -b           = Auto overlay 2 images, first in the left bottom corner
                 second in the right bottom corner.
  -l           = Auto overlay image in the left bottom corner.
  -r           = Auto overlay image in the right bottom corner.
  -p           = Make an opaque overlay.
  --           = End of options.

ARGUMENTS
Give if needed:

  inf  = The input video.
  img  = The PNG image which will overlayed over inf.
  img2 = The second PNG image which will overlayed over inf.
  xpos = X position of overlay.
  ypos = Y position of overlay
  outf = The output video.

EXIT STATUS
On program exit:

  Code 0 = OK

  See output of 'showerrmsg'.

SEE ALSO
Related manpages:

  showerrmsg(1), ffmpeg(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.

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

#FILES
#Used directories and files:
#
#  Config Global = /usr/local/etc/sample.conf
#  Config User   = ~/.samplerc

return
}

#=============================================================================
function overlay_beide_oben ()
{
# Auto overlay a video with 2 images.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=10,overlay=x=W-w-10:y=10' $arg4
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=10,overlay=x=W-w-10:y=10' $arg4 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=10,overlay=x=W-w-10:y=10' $arg4 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_links_oben ()
{
# Auto overlay a video with an image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=10' $arg3
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=10' $arg3 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=10' $arg3 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_rechts_oben ()
{
# Auto overlay a video with an image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=10' $arg3
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=10' $arg3 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=10' $arg3 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_beide_unten ()
{
# Auto overlay a video with 2 images.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=H-h-10,overlay=x=W-w-10:y=H-h-10' $arg4
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=H-h-10,overlay=x=W-w-10:y=H-h-10' $arg4 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -i $arg3 -filter_complex 'overlay=x=10:y=H-h-10,overlay=x=W-w-10:y=H-h-10' $arg4 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_links_unten ()
{
# Auto overlay a video with an image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=H-h-10' $arg3
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=H-h-10' $arg3 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -filter_complex 'overlay=x=10:y=H-h-10' $arg3 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_rechts_unten ()
{
# Auto overlay a video with an image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=H-h-10' $arg3
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=H-h-10' $arg3 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -filter_complex 'overlay=x=W-w-10:y=H-h-10' $arg3 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function overlay_opaque ()
{
# Overlay a video with an opaque image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

# make opaque overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex "[1:v]format=rgba, colorchannelmixer=aa=0.4[fg]; [fg]setsar=1[logo]; [0][logo]overlay=$arg3:$arg4" $arg5
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run ffmpeg"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex "[1:v]format=rgba, colorchannelmixer=aa=0.4[fg]; [fg]setsar=1[logo]; [0][logo]overlay=$arg3:$arg4" $arg5 1>/dev/null
	#1>/dev/null
	#2>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex "[1:v]format=rgba, colorchannelmixer=aa=0.4[fg]; [fg]setsar=1[logo]; [0][logo]overlay=$arg3:$arg4" $arg5 &>/dev/null
	#&>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

return $ret
}

#=============================================================================
function mainproc ()
{
# Overlay a video with an image.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0
#local -a arr
#maxarr="${#arr[@]}"			# get number of elements in arr
#unset arr[@]				# remove the entire array

# make opaque overlay in left top corner
#ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex "[1:v]format=rgba, colorchannelmixer=aa=0.4[fg]; [fg]setsar=1[logo]; [0][logo]overlay=10:10" $arg3 1>/dev/null

# make solid overlay
case "$verbose" in
    yes)					# Allow STDOUT and STDERR
	echo "Run $scriptname"
	ffmpeg -i $arg1 -i $arg2 -filter_complex "overlay=$arg3:$arg4" $arg5
	ret=$?
	;;
    '')						# Suppress STDOUT or STDERR
	#echo "Run $scriptname"
	ffmpeg -hide_banner -i $arg1 -i $arg2 -filter_complex "overlay=$arg3:$arg4" $arg5
	#2>/dev/null
	#1>/dev/null
	ret=$?
	;;
    no)						# Suppress STDOUT and STDERR
	ffmpeg -hide_banner -y -i $arg1 -i $arg2 -filter_complex "overlay=$arg3:$arg4" $arg5 &>/dev/null
	ret=$?
	;;
esac

if [ "$ret" -ne 0 ]; then				# for other progs
	Show_Err ${_ERR_GENERAL_} "$ret from ffmpeg!"
	ret=${_ERR_GENERAL_}
fi

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 ''!
#
# Options are divided in 2 classes:
#   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 a delimiter like ',' or ';' or '.'
# Use a delimiter which will not be part of the arg:
# sampleprog -m val1,val2
# sampleprog --myval=val1,val2
# It's at the programmers responsibility to do meaningfull things with the
# content of the options.
#
# While err = 0 then look for a next option.
# When GetOpt finishes then all options are removed from cmdline.
#
if [ "$debug" == "yes" ]; then				# DEBUG
	echo "DEBUG Parse Commandline: $# $@" >&2
	$debuglog Parse Commandline: $# $@
fi
#
err=0
while [ "$err" -eq 0 ]; do
	GetOpt 'hVdqvBLRblpr' 'help version debug quiet verbose' $@
	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";;
		    B)		cmd="-${_OPTNAME_}";;
		    L)		cmd="-${_OPTNAME_}";;
		    R)		cmd="-${_OPTNAME_}";;
		    b)		cmd="-${_OPTNAME_}";;
		    l)		cmd="-${_OPTNAME_}";;
		    p)		cmd="-${_OPTNAME_}";;
		    r)		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;;
		    :)	if [ "$verbose" != no ]; then
				echo "${ErrMsg[8]} -${_OPTVAL_}" >&2
			else
				$errlog ${ErrMsg[8]} -${_OPTVAL_}
			fi; exit 8;;
		    ?)	if [ "$verbose" != no ]; then
				echo "${ErrMsg[4]} -${_OPTVAL_}" >&2
			else
				$errlog ${ErrMsg[4]} -${_OPTVAL_}
			fi; exit 4;;
		    *)	if [ "$verbose" != no ]; then
				echo "${ErrMsg[99]} _OPTNAME_=${_OPTNAME_} _OPTVAL_=${_OPTVAL_}" >&2
			else
				$errlog ${ErrMsg[99]} _OPTNAME_=${_OPTNAME_} _OPTVAL_=${_OPTVAL_}
			fi; exit 99;;
		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
    ''|-p)
	if [ "$#" -eq 5 ]; then				# parameter count ok
		arg1="$1"
		arg2="$2"
		arg3="$3"
		arg4="$4"
		arg5="$5"
		shift 5
		#args="$@"
		#shift $#
	else						# error in param count
		if [ "$verbose" != no ]; then
			echo "${ErrMsg[2]}" >&2
		else
			$errlog ${ErrMsg[2]}
		fi
		exit 2
	fi
	;;
    -B|-b)
	if [ "$#" -eq 4 ]; then				# parameter count ok
		arg1="$1"
		arg2="$2"
		arg3="$3"
		arg4="$4"
		shift 4
		#args="$@"
		#shift $#
	else						# error in param count
		if [ "$verbose" != no ]; then
			echo "${ErrMsg[2]}" >&2
		else
			$errlog ${ErrMsg[2]}
		fi
		exit 2
	fi
	;;
    -L|-R|-l|-r)
	if [ "$#" -eq 3 ]; then				# parameter count ok
		arg1="$1"
		arg2="$2"
		arg3="$3"
		shift 3
		#args="$@"
		#shift $#
	else						# error in param count
		if [ "$verbose" != no ]; then
			echo "${ErrMsg[2]}" >&2
		else
			$errlog ${ErrMsg[2]}
		fi
		exit 2
	fi
	;;
    *)
	if [ "$#" -ne 0 ]; then				# error in param count
		if [ "$verbose" != no ]; then
			echo "${ErrMsg[2]}" >&2
		else
			$errlog ${ErrMsg[2]}
		fi
		exit 2
	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
	err=0
	;;
    -V)
	echo "${scriptname}: (${package}) $package_file ${version}-${release}"
	err=0
	;;
    -B)
	overlay_beide_oben
	err=$?
	;;
    -L)
	overlay_links_oben
	err=$?
	;;
    -R)
	overlay_rechts_oben
	err=$?
	;;
    -b)
	overlay_beide_unten
	err=$?
	;;
    -l)
	overlay_links_unten
	err=$?
	;;
    -r)
	overlay_rechts_unten
	err=$?
	;;
    -p)
	overlay_opaque
	err=$?
	;;
    *)							# error
	Show_Err 6 ""
	err=6
	;;
esac

#rm -f $lockfile

exit $err
