#!/bin/sh
set -e
create=0 root= conf=
for arg; do
  case "$arg" in
    --create)  create=1 ;;
    --root=*)  root="${arg#--root=}" ;;
    --*)       ;;
    *)         conf="$arg" ;;
  esac
done
[ "$create" = 1 ] && [ -n "${conf:-}" ] || exit 0
for dir in /etc/tmpfiles.d /run/tmpfiles.d /usr/local/lib/tmpfiles.d /usr/lib/tmpfiles.d; do
  f="${root}${dir}/${conf}"
  [ -f "$f" ] || continue
  while IFS= read -r line; do
    set -- $line
    case "${1-}" in
      '#'*|'') continue ;;
      d|D|q|Q|v)
        install -d -m "${3:-0755}" "${root}${2}"
        [ "${4:--}" = '-' ] || chown "${4}" "${root}${2}" 2>/dev/null || true
        [ "${5:--}" = '-' ] || chgrp "${5}" "${root}${2}" 2>/dev/null || true
        ;;
      f|F)
        [ -f "${root}${2}" ] || install -m "${3:-0644}" /dev/null "${root}${2}"
        [ "${4:--}" = '-' ] || chown "${4}" "${root}${2}" 2>/dev/null || true
        [ "${5:--}" = '-' ] || chgrp "${5}" "${root}${2}" 2>/dev/null || true
        ;;
    esac
  done < "$f"
  break
done
