nsm/nsm

100 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# CONSTANTS
readonly PKGVER="0.0.1"
readonly LICENSE="GNU AGPLv3"
readonly ROOTPATH="$(dirname $(readlink -f $(which ${0})))"
readonly CONFIG_DIR="/usr/local/noveria/etc/nsm"
readonly RUN_DIR="/usr/local/noveria/run/nsm"
readonly CONFIG="$CONFIG_DIR/nsm.json"
for util in ${ROOTPATH}/utils/*; do
source ${util}
done
# VARIABLES
declare debuglevel=3
declare shell=0
declare mainfunc=0
function usage_general() {
cat <<EOF
usage: $(basename ${0}) <operation> [subcommand]
operations:
$(basename ${0}) {-h --help}
$(basename ${0}) {-V --version}
$(basename ${0}) {-v --verbose}
$(basename ${0}) {-s --shell}
EOF
}
function usage() {
usage_general
cat<<EOF
subcommands:
$(basename ${0}) check
$(basename ${0}) update
$(basename ${0}) sysupgrade
EOF
}
function version() {
echo -e "Novos System Manager v${PKGVER}
Copyright (C) $(date +%Y) Noveria Network
This program may be freely redistributed under
the terms of the ${LICENSE}"
}
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-V|--version)
version
exit 0
;;
-v|--verbose)
debuglevel=4
;;
-s|--shell)
shell=1
;;
*)
COMMAND="$1"
SUBCOMMAND="$2"
shift; shift
PARAMS="$@"
break
esac
shift
done
if ! test -d "$RUN_DIR"; then
mkdir -p "$RUN_DIR"
fi
if ! find "$ROOTPATH/functions" -iname "$COMMAND" &> /dev/null; then
log e "'$COMMAND' is not a viable function!"
exit 1
fi
if test -z "${SUBCOMMAND}"; then
SUBCOMMAND="main"
fi
if ! grep -oE "${COMMAND}_${SUBCOMMAND}" "$ROOTPATH/functions/$COMMAND" &> /dev/null; then
if ! grep -oE "${COMMAND}_main" "$ROOTPATH/functions/$COMMAND" &> /dev/null; then
log e "'$SUBCOMMAND' is not viable command in '$COMMAND'"
exit 1
else
mainfunc=1
fi
fi
source "$ROOTPATH/functions/$COMMAND"
if (( mainfunc )); then
"${COMMAND}_main" "${PARAMS[@]}"
else
"${COMMAND}_${SUBCOMMAND}" "${PARAMS[@]}"
fi