begin to implement new style option parsing

Signed-off-by: Nico Schottelius <nico@ikn.schottelius.org>
This commit is contained in:
Nico Schottelius 2009-07-01 12:03:40 +02:00
parent de6a7893fc
commit ca1231a576

View file

@ -40,8 +40,8 @@ CPREEXEC="${CDEFAULTS}/pre_exec"
CPOSTEXEC="${CDEFAULTS}/post_exec" CPOSTEXEC="${CDEFAULTS}/post_exec"
TMP=$(mktemp "/tmp/${__myname}.XXXXXX") TMP=$(mktemp "/tmp/${__myname}.XXXXXX")
VERSION=0.7.1 VERSION=0.8
RELEASE="2009-02-02" RELEASE="2009-XX-XX"
HALF_VERSION="ccollect ${VERSION}" HALF_VERSION="ccollect ${VERSION}"
FULL_VERSION="ccollect ${VERSION} (${RELEASE})" FULL_VERSION="ccollect ${VERSION} (${RELEASE})"
@ -55,9 +55,13 @@ DDATE="date +%Y-%m-%d-%H:%M:%S"
TSORT="tc" TSORT="tc"
# #
# unset parallel execution # unset values
# #
PARALLEL="" PARALLEL=""
USE_ALL=""
export INTERVAL=""
export no_sources=0
# #
# catch signals # catch signals
@ -87,6 +91,9 @@ add_name()
awk "{ print \"[${name}] \" \$0 }" awk "{ print \"[${name}] \" \$0 }"
} }
#
# Execute on remote host, if backing up to a remote host
#
pcmd() pcmd()
{ {
if [ "$remote_host" ]; then if [ "$remote_host" ]; then
@ -96,21 +103,15 @@ pcmd()
fi fi
} }
#
# Version
#
display_version() display_version()
{ {
echo "${FULL_VERSION}" echo "${FULL_VERSION}"
exit 0 exit 0
} }
#
# Tell how to use us
#
usage() usage()
{ {
echo "${__myname}: <interval name> [args] <sources to backup>" echo "${__myname}: [args] <interval name> <sources to backup>"
echo "" echo ""
echo " ccollect creates (pseudo) incremental backups" echo " ccollect creates (pseudo) incremental backups"
echo "" echo ""
@ -128,49 +129,12 @@ usage()
} }
# #
# need at least interval and one source or --all # Parse options
# #
if [ $# -lt 2 ]; then
if [ "$1" = "-V" -o "$1" = "--version" ]; then
display_version
else
usage
fi
fi
#
# check for configuraton directory
#
[ -d "${CCOLLECT_CONF}" ] || _exit_err "No configuration found in " \
"\"${CCOLLECT_CONF}\" (is \$CCOLLECT_CONF properly set?)"
#
# Filter arguments
#
export INTERVAL="$1"; shift
i=1
no_sources=0
#
# Capture options and create source "array"
#
WE=""
ALL=""
NO_MORE_ARGS=""
while [ "$#" -ge 1 ]; do while [ "$#" -ge 1 ]; do
eval arg=\"\$1\"; shift case "$1" in
if [ "${NO_MORE_ARGS}" = 1 ]; then
eval source_${no_sources}=\"${arg}\"
no_sources=$((${no_sources}+1))
# make variable available for subscripts
eval export source_${no_sources}
else
case "${arg}" in
-a|--all) -a|--all)
ALL=1 USE_ALL=1
;; ;;
-v|--verbose) -v|--verbose)
set -x set -x
@ -181,33 +145,56 @@ while [ "$#" -ge 1 ]; do
-h|--help) -h|--help)
usage usage
;; ;;
-V|--version)
display_version
;;
-h|--help|-*)
usage
;;
--) --)
NO_MORE_ARGS=1 break
;; ;;
*) *)
eval source_${no_sources}=\"$arg\" break
no_sources=$(($no_sources+1))
;; ;;
esac esac
fi shift
i=$(($i+1))
done done
# also export number of sources
export no_sources #
# check for configuraton directory
#
[ -d "${CCOLLECT_CONF}" ] || _exit_err "No configuration found in " \
"\"${CCOLLECT_CONF}\" (is \$CCOLLECT_CONF properly set?)"
#
# Capture sources in an "array"
#
while [ "$#" -ge 1 ]; do
eval arg=\"\$1\"; shift
if [ -z "$INTERVAL" ]; then
INTERVAL="$arg"
else
eval source_${no_sources}=\"${arg}\"
no_sources="$((${no_sources}+1))"
# make variable available for subscripts
eval export source_${no_sources}
fi
done
# #
# Look, if we should take ALL sources # Look, if we should take ALL sources
# #
if [ "${ALL}" = 1 ]; then if [ "${USE_ALL}" = 1 ]; then
# reset everything specified before no_sources="0"
no_sources=0
# #
# get entries from sources # get entries from sources
# #
cwd=$(pwd -P) cwd="$(pwd -P)"
( cd "${CSOURCES}" && ls > "${TMP}" ); ret=$? ( cd "${CSOURCES}" && ls > "${TMP}" ); ret=$?
[ "${ret}" -eq 0 ] || _exit_err "Listing of sources failed. Aborting." [ "${ret}" -eq 0 ] || _exit_err "Listing of sources failed. Aborting."
@ -221,7 +208,7 @@ fi
# #
# Need at least ONE source to backup # Need at least ONE source to backup
# #
if [ "${no_sources}" -lt 1 ]; then if [ "${no_sources}" -lt 1 -o -z "${INTERVAL}" ]; then
usage usage
else else
_techo "${HALF_VERSION}: Beginning backup using interval ${INTERVAL}" _techo "${HALF_VERSION}: Beginning backup using interval ${INTERVAL}"
@ -595,4 +582,4 @@ if [ -x "${CPOSTEXEC}" ]; then
fi fi
rm -f "${TMP}" rm -f "${TMP}"
_techo "Finished ${WE}" _techo "Finished"