#!/bin/bash

# Script that validates that the pillar/secret/*/*.sls files contain the
# appropriate header, and that none other pillar files contain this header or
# any secrets

HEADER="yaml\\|gpg"

SECRETS_SLS=$(find pillar/secrets -name '*.sls' 2> /dev/null)
if [[ -n $SECRETS_SLS ]];  then
    for secret_sls in ${SECRETS_SLS[@]}; do
        head -n 1 $secret_sls | grep -q -P "$HEADER" || {
            echo "$secret_sls is missing pattern \"$HEADER\" in shebang"
            STATUS=1
        }
    done
fi

for sls in $(find pillar/ -not -path 'pillar/secrets/*' -name '*.sls'); do
    if $(grep -q -P "$HEADER" $sls); then
        echo "$sls has the \"$HEADER\" header, please remove it"
        STATUS=1
    fi
    if $(grep -q "BEGIN GPG MESSAGE" $sls); then
        echo "$sls contains secrets. Please move them to pillar/secrets/${sls#*/}"
        STATUS=1
    fi
done

exit $STATUS
