]> git.nbdom.net Git - nb.git/commitdiff
cron improve
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 18 Nov 2014 23:03:37 +0000 (23:03 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 18 Nov 2014 23:03:37 +0000 (23:03 +0000)
bin/nb-install

index 5676efeb87d8e94a9b1854042de16416dd029337..e528246408256f0d8f23773c82329649049369a2 100755 (executable)
@@ -1,13 +1,24 @@
 #!/usr/bin/env bash
+##############################################################################
+#
+# Install - NB 18.11.14
+#
+##############################################################################
 . "${BASH_SOURCE%/*}/../etc/profile.sh" || exit
 declare -r NAME="$(basename "${0}")"
-
+declare TMP=/tmp/$NAME.$$
 declare install="rsync -au --force"
 
+#
+# Args
+#
 case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac
 case "$@" in *-v*) install="$install -v";; esac
 case "$@" in *-n*) install="$install -n";; esac
 
+#
+# Functions
+#
 function verbose() {
   printf '> %s\n' "$@"
 }
@@ -16,41 +27,58 @@ function crond2tab() {
   local src="$1"
 }
 
+function fdiff() {
+  [ -n "$(diff -q $1 $2)" ] && return 0
+  return 1
+}
+
 #
 # Cron
 #
 if [ -d /etc/cron.d ]; then
-  if [ -n "$(diff -q $NB_ROOT/etc/cron/nb /etc/cron.d/nb)" ]; then
+  if fdiff "$NB_ROOT/etc/cron/nb" "/etc/cron.d/nb"; then
     verbose "Install /etc/cron.d/nb"
     $install "$NB_ROOT/etc/cron/nb" /etc/cron.d/nb
   fi
 
 else
 
-  crontab -l > /tmp/nb-crontab.orig
+  crontab -l > $TMP.crontab.orig.orig
   (
     crontab -l | grep -vE 'nb-update|^PATH'
     # Delete user
     perl -ane '/^PATH/ and print; /^[\*\d]/ or next; print join(" ",@F[0..4,6..$#F])."\n"' $NB_ROOT/etc/cron/nb
-  ) > /tmp/nb-crontab.new
+  ) > $TMP.crontab.orig.new
 
-  if [ -n "$(diff -q /tmp/nb-crontab.orig /tmp/nb-crontab.new)" ]; then
+  if fdiff $TMP.crontab.orig.orig $TMP.crontab.orig.new; then
     verbose "Install /etc/cron.d/nb"
-    crontab /tmp/nb-crontab.new
+    crontab $TMP.crontab.orig.new
   fi
-  rm -f /tmp/nb-crontab.*
 
 fi
 
 #
 # Profile
 #
+echo "[ -r $NB_ROOT/etc/profile.sh ] && . $NB_ROOT/etc/profile.sh" > $TMP.profile
 if [ -d /etc/profile.d ]; then
-  verbose "Install /etc/profile.d/nb-profile.sh"
-  echo "[ -r $NB_ROOT/etc/profile.sh ] && . $NB_ROOT/etc/profile.sh" > /tmp/nb-profile.sh \
-    && $install -c /tmp/nb-profile.sh /etc/profile.d/nb-profile.sh \
-    && chmod 755 /etc/profile.d/nb-profile.sh \
-  ;
 
-  rm -f /tmp/nb-profile.sh
+  if fdiff $TMP.profile /etc/profile.d/nb-profile.sh; then
+    verbose "Install /etc/profile.d/nb-profile.sh"
+    $install -c $TMP.profile /etc/profile.d/nb-profile.sh
+    chmod 755 /etc/profile.d/nb-profile.sh
+  fi
+
+elif [ -w /etc/profile ]; then
+
+  if ! grep -qm1 -F "$(cat $TMP.profile)" /etc/profile; then
+    verbose "Install /etc/profile"
+    cat $TMP.profile >> /etc/profile
+  fi
+
 fi
+
+#
+# Bye
+#
+rm -f $TMP*