#!/bin/bash

if ( ! which lynis ) ; then
    : ${LYNIS_VERSION:=3.0.6}
    : ${LYNIS_DOWNLOAD_URL:=https://downloads.cisofy.com/lynis/lynis-${LYNIS_VERSION}.tar.gz}
    curl -o /tmp/lynis.tgz ${LYNIS_DOWNLOAD_URL:=https://downloads.cisofy.com/lynis/lynis-${LYNIS_VERSION}.tar.gz} 
    tar xvfz /tmp/lynis.tgz
    cd lynis
    cat <<EOF | sudo tee /usr/bin/lynis
cd $(pwd)
./lynis "\$@"
EOF
    sudo chmod +x /usr/bin/lynis
fi

lynis -q

echo ---

cat <<EOF
id: lynis
created: $(date +"%Y%m%d-%H%M")
mode: amp
EOF

if ( grep -q Warning: ~/lynis.log ) ; then

    cat <<EOF
    
summary: Lynis reported one or more warnings
pass: false
notes: |2
  Lynis warnings:

$(grep Warning: ~/lynis.log | sed 's/^/    /')

EOF


else

    cat <<EOF
    
summary: Lynis succeeded with no warnings
pass: true
notes: |2
EOF

fi

    cat <<EOF

  Lynis tests run: $(grep Test: ~/lynis.log | wc | awk '{print $1}')

  Lynis suggestions:

$(grep Suggestion: ~/lynis.log | sed 's/^/    /')

EOF

