From f703a959717af4a1749b4986d296d5a1b95df2e1 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Sat, 5 Aug 2017 20:41:21 +0100
Subject: [PATCH] Only copy stig tests script if it changes

---
 src/freedombone-utils-cron | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/freedombone-utils-cron b/src/freedombone-utils-cron
index 13e19f55c..5fa3dd142 100755
--- a/src/freedombone-utils-cron
+++ b/src/freedombone-utils-cron
@@ -78,18 +78,30 @@ function randomize_cron {
 }
 
 function schedule_stig_tests {
-    echo '#!/bin/bash' > /etc/cron.daily/stig_tests
-    echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}" >> /etc/cron.daily/stig_tests
-    echo "pkill ${PROJECT_NAME}-tests" >> /etc/cron.daily/stig_tests
-    echo 'rm -rf /tmp/*' >> /etc/cron.daily/stig_tests
-    echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests" >> /etc/cron.daily/stig_tests
-    echo 'if [ ! "$?" = "0" ]; then' >> /etc/cron.daily/stig_tests
-    echo "    echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS" >> /etc/cron.daily/stig_tests
-    echo 'fi' >> /etc/cron.daily/stig_tests
-    echo 'if [ -f /tmp/daily-stig-tests ]; then' >> /etc/cron.daily/stig_tests
-    echo '  rm /tmp/daily-stig-tests' >> /etc/cron.daily/stig_tests
-    echo 'fi' >> /etc/cron.daily/stig_tests
-    chmod +x /etc/cron.daily/stig_tests
+    stig_tests_script=/tmp/stig_tests_script
+    echo '#!/bin/bash' > $stig_tests_script
+    echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}" >> $stig_tests_script
+    echo "pkill ${PROJECT_NAME}-tests" >> $stig_tests_script
+    echo 'rm -rf /tmp/*' >> $stig_tests_script
+    echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests" >> $stig_tests_script
+    echo 'if [ ! "$?" = "0" ]; then' >> $stig_tests_script
+    echo "    echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS" >> $stig_tests_script
+    echo 'fi' >> $stig_tests_script
+    echo 'if [ -f /tmp/daily-stig-tests ]; then' >> $stig_tests_script
+    echo '  rm /tmp/daily-stig-tests' >> $stig_tests_script
+    echo 'fi' >> $stig_tests_script
+    chmod +x $stig_tests_script
+
+    if [ ! -f /etc/cron.daily/stig_tests ]; then
+        cp $stig_tests_script /etc/cron.daily/stig_tests
+    else
+        HASH1=$(sha256sum $stig_tests_script | awk -F ' ' '{print $1}')
+        HASH2=$(sha256sum /etc/cron.daily/stig_tests | awk -F ' ' '{print $1}')
+        if [[ "$HASH1" != "$HASH2" ]]; then
+            cp $stig_tests_script /etc/cron.daily/stig_tests
+        fi
+    fi
+    rm $stig_tests_script
 }
 
 # NOTE: deliberately there is no "exit 0"
-- 
GitLab