#!/bin/sh
set -e

find "$CRUFT_ROOT/usr/lib/tmpfiles.d/" -maxdepth 1 -type f -printf "%f\n"  | while read -r definition
do
    echo "@/usr/lib/tmpfiles.d/${definition}"

    grep -v ^# < "$CRUFT_ROOT/usr/lib/tmpfiles.d/${definition}" | sed 's/%./*/g' | while read -r _action path _extra
    do
        case "$path" in
             /etc/*)
                  echo "$path"
                  ;;
             /usr/*)
                  echo "$path"
                  ;;
             /var/*)
                  echo "$path"
                  ;;
        esac
    done | sort -u | while read -r path
    do
        # we purposefully want the shell to glob these paths
        #
        # false positives like "/var/log/journal/*/system.journal"
        # are not a problem, they will never match a real file
        #
        # shellcheck disable=SC2086
        echo $path | tr ' ' '\n'
    done
done
