#!/bin/bash
#set -n		# read but do not execute commands (check on syntax errors)
#
# File: /usr/local/bin/mediarecod-avi2mp4
#
# Create an MP4 from an AVI media file.
#
#    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
#
##############################################################################
# FILTER
#
##############################################################################
# AWK
#
##############################################################################
# SED
#
##############################################################################
# XARGS
#
##############################################################################
# REPLACE STRINGS
#
##############################################################################
# RSYNC
#
##############################################################################
# DIALOG
#
##############################################################################
# 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
#. $LLIBDIR/libfunchart-sh				# functions chart
#. /usr/local/etc/multimedia.conf			# config multimedia

#if [ ! -e ~/.samplerc ]; then				# install config user
#	cp /usr/local/share/ksm-sample/samplerc.templ ~/.samplerc
#	echo "Please configure your ~/.samplerc"
#	exit 1
#fi
#. ~/.samplerc						# config user

# Select config global OR config local. Config local gets priority.
#if [ -e ~/.samplerc ]; then
#	. ~/.samplerc					# config user
#else
#	. /usr/local/etc/sample.conf			# config global
#fi

##############################################################################
# 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=$(cat $vinfopath/VERSION)		# package version
readonly release=$(cat $vinfopath/RELEASE)		# package release
readonly scriptname=$(basename $0)			# name of script
#readonly tempfile=$TEMPDIR/$scriptname.$UID.tmp	# temporary file name
#readonly tempfile=$(mktemp -u $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
#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>
opt_c=''				# option -c yes
opt_crf=''				# option --crf=<arg>
opt_r=''				# option --r=<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 - Create an MP4 from an AVI media file.

SYNOPSIS
$scriptname [options] [inf outf]

DESCRIPTION
Create an MP4 from an AVI media file. If video stream is copied then
the PTS is generated for each frame (-fflags +genpts). The audio stream
is always recoded to aac.

Defaults:
  Copy video and audio stream (output size and fps are taken from inf).

  For cmd UHD, FHD, HD:

  rate = 50
  crf  = 23

  For cmd HQ, SD, DVD:

  rate = 25
  crf  = 23

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.
  -c           = Copy the audio stream instead of recoding.
  --crf=<cq>   = Set quality for h264 (Constant Rate Factor,
                 cmds --uhd, --fhd, --hd, --hq, --sd, --dvd).
                 cq=0..51(0=lossless,51=worst,def=23,17=visib.lossless)
  --r=<rate>   = Set viedeo frame rate/sec. rate=25|50|30|60 ...
                 (cmds --uhd, --fhd,--hd,--hq,--sd,--dvd).
  --uhd        = Transcode media file to UHD (3840 x 2160).
  --fhd        = Transcode media file to FHD (1920 x 1080).
  --hd         = Transcode media file to HD (1280 x 720).
  --hq         = Transcode media file to HQ (1024 x 576).
  --sd         = Transcode media file to SD (960 x 540).
  --dvd        = Transcode media file to DVD (720 x 576).
  --           = End of options.

ARGUMENTS
Give if needed:

  inf  = AVI
  outf = MP4

EXIT STATUS
On program exit:

  Code 0 = OK

  See output of 'showerrmsg'.

SEE ALSO
Related manpages:

  showerrmsg(1), ffmpeg(1), media2media(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 recode_uhd ()
{
# Transcode the media file to UHD.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=3840x2160 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=3840x2160 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=3840x2160 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function recode_fhd ()
{
# Transcode the media file to FHD.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1920x1080 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1920x1080 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1920x1080 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function recode_hd ()
{
# Transcode the media file to HD.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1280x720 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1280x720 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1280x720 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function recode_hq ()
{
# Transcode the media file to HQ.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1024x576 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1024x576 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=1024x576 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function recode_sd ()
{
# Transcode the media file to SD.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=960x540 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=960x540 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=960x540 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function recode_dvd ()
{
# Transcode the media file to DVD.
# par1 = 
# Uses echo, logger
# Return codes:
# Standard

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

local ret=0

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=720x576 --crf=$opt_crf $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=720x576 --crf=$opt_crf $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --vcodec=h264 --acodec=$opt_c --r=$opt_r --s=720x576 --crf=$opt_crf $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	ret=${_ERR_GENERAL_}
#fi

return $ret
}

#=============================================================================
function mainproc ()
{
# Transcode the media file.
# 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
#local maxarr="${#arr[@]}"		# get number of elements in arr
#unset arr[@]				# remove the entire array

case "$verbose" in
    yes)					# allow STDOUT and STDERR
	echo "Run $scriptname"
	media2media -v --fflags=+genpts --vcodec=copy --acodec=$opt_c $arg1 $arg2
	ret=$?
	;;
    '')						# suppress STDOUT or STDERR
	#echo "Run $scriptname"
	media2media --fflags=+genpts --vcodec=copy --acodec=$opt_c $arg1 $arg2
	#COMMAND 1>/dev/null
	#COMMAND 2>/dev/null
	ret=$?
	;;
    no)						# suppress STDOUT and STDERR
	media2media -q --fflags=+genpts --vcodec=copy --acodec=$opt_c $arg1 $arg2 &>/dev/null
	#COMMAND &>/dev/null
	ret=$?
	;;
esac
if [ "$ret" -ne 0 ]; then				# for ksm-progs
	Show_Err ${ret} "from media2media"
	ret=${ret}
fi
#if [ "$ret" -ne 0 ]; then				# for other progs
#	Show_Err ${_ERR_GENERAL_} "$ret from COMMAND"
#	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=${_ERR_OK_}
while [ "$err" -eq 0 ]; do
	GetOpt 'hVdqvc' 'help version debug quiet verbose crf r dvd sd hq hd fhd uhd' $@
	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";;
		    uhd)	cmd="-${_OPTNAME_}";;
		    fhd)	cmd="-${_OPTNAME_}";;
		    hd)		cmd="-${_OPTNAME_}";;
		    hq)		cmd="-${_OPTNAME_}";;
		    sd)		cmd="-${_OPTNAME_}";;
		    dvd)	cmd="-${_OPTNAME_}";;
		    d|debug)	debug=yes;;
		    q|quiet)	verbose=no;;
		    v|verbose)	verbose=yes;;
		    c)		opt_c=yes;;
#		    o|output)	opt_o=yes;;
#		    i|infile)	opt_i="${_OPTVAL_}";;
#		    o|outfile)	opt_o="${_OPTVAL_}";;
#		    p|pages)	opt_p="${_OPTVAL_}";;
		    crf)	opt_crf="${_OPTVAL_}";;
		    r)		opt_r="${_OPTVAL_}";;
		    --)		break;;
		    :)	Show_Err ${_ERR_MISSING_ARG_FOR_OPT_} "-${_OPTVAL_}"
			exit ${_ERR_MISSING_ARG_FOR_OPT_};;
		    ?)	Show_Err ${_ERR_UNKNOWN_OPT_} "-${_OPTVAL_}"
			exit ${_ERR_UNKNOWN_OPT_};;
		    *)	Show_Err ${_ERR_UNKNOWN_ERR_} "_OPTNAME_=${_OPTNAME_} _OPTVAL_=${_OPTVAL_}"
			exit ${_ERR_UNKNOWN_ERR_};;
		esac
	fi
done
err=${_ERR_OK_}
shift ${_OPTIDX_}				# rm all opts from cmdline
#
# Uncomment if giving a cmd is mandatory
#if [ -z "$cmd" ]; then					# error no cmd given
#	Show_Err ${_ERR_MISSING_SUB_CMD_} ""
#	exit ${_ERR_MISSING_SUB_CMD_}
#fi

##############################################################################
# CHECK LINK NAMES

##############################################################################
# CHECK OPTIONS

##############################################################################
# CHECK VERSION

##############################################################################
# CHECK ARGUMENTS
#
case "$cmd" in
    ''|-uhd|-fhd|-hd|-hq|-sd|-dvd)
	if [ "$#" -eq 2 ]; then				# parameter count ok
		arg1="$1"
		arg2="$2"
		shift 2
		#args="$@"
		#shift $#
	else						# error in param count
		Show_Err ${_ERR_WRONG_PAR_COUNT_} ""
		exit ${_ERR_WRONG_PAR_COUNT_}
	fi
	;;
    *)
	if [ "$#" -ne 0 ]; then				# error in param count
		Show_Err ${_ERR_WRONG_PAR_COUNT_} ""
		exit ${_ERR_WRONG_PAR_COUNT_}
	fi
	;;
esac

##############################################################################
# LOCK

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

if [ -n "$opt_c" ]; then
	opt_c="copy"
else
	opt_c="aac"
fi
if [ -z "$opt_crf" ]; then
	opt_crf="23"
fi
if [ -z "$opt_r" ]; then
	opt_r="25"
fi

# Check what to do (sub commands, options and args are removed from cmdline)
case "$cmd" in
    '')
	mainproc
	err=$?
	;;
    -h)
	helptext
	exit 0
	;;
    -V)
	echo "${scriptname}: (${package}) $package_file ${version}-${release}"
	exit 0
	;;
    -uhd)
	if [ -z "$opt_r" ]; then
		opt_r="50"
	fi
	recode_uhd
	err=$?
	;;
    -fhd)
	if [ -z "$opt_r" ]; then
		opt_r="50"
	fi
	recode_fhd
	err=$?
	;;
    -hd)
	if [ -z "$opt_r" ]; then
		opt_r="50"
	fi
	recode_hd
	err=$?
	;;
    -hq)
	if [ -z "$opt_r" ]; then
		opt_r="25"
	fi
	recode_hq
	err=$?
	;;
    -sd)
	if [ -z "$opt_r" ]; then
		opt_r="25"
	fi
	recode_sd
	err=$?
	;;
    -dvd)
	if [ -z "$opt_r" ]; then
		opt_r="25"
	fi
	recode_dvd
	err=$?
	;;
    *)							# error
	Show_Err ${_ERR_SUB_CMD_NOT_FOUND_} ""
	err=${_ERR_SUB_CMD_NOT_FOUND_}
	;;
esac

#rm -f $lockfile

exit $err
