#!/bin/bash
#
#    Tool to rebuild all checksums for check_checksums
#
#
#    Copyright (C) 2013 Bernd Zeimetz <b.zeimetz@conova.com>
#
#    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.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e
umask 027

for t in md5 sha1 sha224 sha256 sha384 sha512; do
    fname="/etc/nagios/check_checksums.${t}"
    tool="${t}sum"
    if [ -f ${fname} ]; then
        tmp=`mktemp`
        chown root:nagios ${tmp}
        chmod 640 ${tmp}

        trap "rm -f ${tmp}" EXIT

        sed 's,^[^ ]*  ,,' ${fname} | while read f; do
            if [ -f "${f}" ]; then
                ${tool} "${f}" >> ${tmp}
            else
                echo "${f} went missing, ignoring!"
            fi
        done

        ln "${fname}" "${fname}_`date '+%s'`"
        mv "${tmp}" "${fname}"
    fi
done


