#!/bin/bash
set -e

# this is usefull even when systemd is not the init system
# because these declarative configs are shipped anyway

cd /usr/lib/tmpfiles.d/

ls -1 | while read definition
do
    package="${definition%.*}"
    case "$package" in
        debian)
           package="base-files"
           ;;
        lighttpd.tmpfile)
           # there is ../rules/lighttpd
           continue
           ;;
        journal-nocow|legacy|var)
           continue
           ;;
        systemd-nologin|systemd-pstore|systemd-tmp)
           package="systemd"
           ;;
    esac

    grep -v ^# < $definition | sed 's/%./*/g' | while read action path extra
    do
        case "$path" in
             /etc/*)
                  echo "$path"
                  ;;
             /var/*)
                  echo "$path"
                  ;;
        esac
    done | sort -u | while read path
    do
        [ "$package" != "$last_package" ] && echo "$package"
        # we purposefuly 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
        echo $path
        last_package="$package"
    done
done
