#!/bin/sh

# this script needs root. Since the daily aide check does not run 
# privileged, a dedicated service unit (runs as root) and invokes this
# script before the daily aide check is started. The output is written to
# a cache file that the daily aide check reads. This script is invoked again,
# this time producing no output since the LVM tool calls fail.
# If aide is directly invoked as root, the cache file is read but its values
# are overwritten by the output generated by the second call to this script.

undefine() {
    local var=$1
    printf "@@if defined %s\n" "${var}"
    printf "@@undef %s\n" "${var}"
    printf "@@endif\n"
}

if command -v dmsetup >/dev/null; then
    if [ "$(dmsetup info --columns --options uuid --noheadings 2>/dev/null| grep -cvi 'no devices found')" != 0 ]; then
        undefine DM_UUIDS_W_P
        undefine DM_UUIDS_WO_P
        printf "@@define DM_UUIDS_W_P (%s)\\n" \
            "$(\
                dmsetup info --columns --options uuid --noheadings | \
                paste --serial --delimiters='|'
            )"
        printf "@@define DM_UUIDS_WO_P (%s)\\n" \
            "$(\
                dmsetup info --columns --options uuid --noheadings | \
                sed 's/^LVM-//' | paste --serial --delimiters='|'
             )"
    fi
    if [ "$(dmsetup info --columns --options name --noheadings 2>/dev/null| grep -cvi 'no devices found')" != 0 ]; then
        undefine DM_NAMES
        printf "@@define DM_NAMES (%s)\\n" \
            "$(\
                dmsetup info --columns --options name --noheadings | \
                paste --serial --delimiters='|'
            )"
        printf "@@define DM_NAMES_WO_D (%s)\\n" \
            "$(\
                dmsetup info --columns --options name --noheadings | \
                sed -e 's/-/\\\\\\\\x2f/' | \
                paste --serial --delimiters='|'
            )"
    fi
fi

if command -v pvs >/dev/null; then
    if [ "$(pvs --noheadings --options uuid 2>/dev/null | wc -l)" != 0 ]; then
        undefine LVM_PV_UUIDS
        undefine LVM_PV_UUIDS_NODASH
        printf "@@define LVM_PV_UUIDS (%s)\\n" \
            "$(\
                pvs --noheadings --options uuid | \
                sed 's/^[[:space:]]*\([^[:space:]]\+\).*/\1/' | \
                sort --uniq | \
                paste --serial --delimiters='|'
              )"
        printf "@@define LVM_PV_UUIDS_NODASH (%s)\\n" \
            "$(\
                pvs --noheadings --options uuid | \
                sed 's/^[[:space:]]*\([^[:space:]]\+\).*/\1/;s/-//g' | \
                sort --uniq | \
                paste --serial --delimiters='|' \
              )"
    fi
fi
if command -v vgs >/dev/null; then
    if [ "$(vgs --noheadings --options vg_name 2>/dev/null | wc -l)" != 0 ]; then
        printf "@@define LVM_VGS (%s)\\n" \
            "$(\
                vgs --noheadings --options vg_name | \
                sed 's/^[[:space:]]*\([^[:space:]]\+\).*/\1/' | \
                sort --uniq | \
                paste --serial --delimiters='|'
            )"
    fi
fi
if command -v lvs >/dev/null; then
    if [ "$(lvs --noheadings --options vg_name 2>/dev/null | wc -l)" != 0 ]; then
        printf "@@define LVM_LVS (%s)\\n" \
            "$(\
                lvs --noheadings --options lv_name | \
                sed 's/^[[:space:]]*\([^[:space:]]\+\).*/\1/' | \
                sort --uniq | \
                paste --serial --delimiters='|' \
            )"
    fi
fi
# vim:tabstop=4:expandtab:shiftwidth=4
