]> git.nbdom.net Git - nb.git/commitdiff
bin/crontab_install
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Oct 2024 13:34:06 +0000 (15:34 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Oct 2024 13:34:06 +0000 (15:34 +0200)
bin/crontab_install [new file with mode: 0755]

diff --git a/bin/crontab_install b/bin/crontab_install
new file mode 100755 (executable)
index 0000000..b20198e
--- /dev/null
@@ -0,0 +1,119 @@
+#!/usr/bin/env bash
+#################################################################################
+#
+# NB 08.10.24 - (C) 2024 Nicolas Boisselier
+#
+#################################################################################
+
+set -o errexit
+declare -r NAME="$(basename "${0}")"
+#declare -r DIR_NAME="$(cd "$(dirname "${0}")"; echo $(pwd))"
+
+################################################################################
+#
+# Default value options
+#
+#################################################################################
+VERBOSE=1
+DEBUG=0
+
+################################################################################
+#
+# Functions
+#
+#################################################################################
+usage() {
+echo "
+=head1 NAME
+
+${NAME}
+
+=head1 SYNOPSIS
+
+Install ~/.crontab* files
+
+       -v, --verbose   Print verbose mode
+       -d, --debug     Print debug messages   
+       -h, --help      Print this help (alternatives: --man, --help2man)
+
+=head1 DESCRIPTION
+
+Long description
+
+=head1 SEE ALSO
+
+http://blog.nbdom.net/
+
+=head1 LICENSE
+
+Copyright (C) 2024 Nicolas Boisselier
+
+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 3 of the License, or (at your option) any later version.
+
+See <http://www.gnu.org/licenses/>.
+
+=head1 AUTHOR
+
+Nicolas Boisselier <nico@nbdom.net>
+"
+}
+
+cat_crontab() {
+       local f
+       for f in .crontab $(
+               (hostname -f; hostname -s) \
+                       | awk '!x[$1]++ && "$1"!="localhost" && !/^ *$/{print ".crontab-host."$1}' \
+       ); do
+               [ "$DEBUG" = "0" ] || echo "DEBUG: $f $([ -r "$f" ] || echo "NOT ")EXISTS" 1>&2
+               [ -r "$f" ] || continue
+               echo "#---------------------------------------"
+               echo "# >>> $f"
+               echo "#---------------------------------------"
+               cat "$f"
+               echo "#---------------------------------------"
+               echo "# <<< $f"
+               echo "#---------------------------------------"
+       done
+
+}
+
+#################################################################################
+#
+# Args
+#
+#################################################################################
+#shopt -s extglob
+#${1?"Usage: $0 ARGUMENT"}
+while [ $# -gt 0 ]; do
+
+       case "$1" in
+
+               -*help|-h) usage | pod2text --width 250; exit 0 ;;
+               --man) usage | pod2man | man -l -; exit 0 ;;
+               --help2man) usage | pod2man; exit 0 ;;
+
+               --verbose|-v) VERBOSE=$(($VERBOSE+1)) ;;
+
+               --debug|-d) DEBUG=$(($DEBUG+1)) ;;
+
+               *) echo "Unknow option: $1 at $0!"; exit -1; ;;
+
+       esac
+       shift
+
+done
+
+#################################################################################
+#
+# Main
+#
+#################################################################################
+cd ~ || exit
+which crontab > /dev/null || exit
+CRON="$(cat_crontab)"
+[ -n "$CRON" ] || exit
+[ "$(crontab -l)" != "$CRON" ] || exit
+[ "$VERBOSE" -gt "0" ] && echo "Update crontab"
+#echo "<$CRON>"
+echo "$CRON" | crontab
+