#!/bin/sh -e

got=false
err=false

if profile=`printcontenv S6_RUNTIME_PROFILE` ; then
  etc="/etc/cont-profile.d/$profile"
else
  etc=/etc
fi

if v=`printcontenv S6_VERBOSITY` && eltest "$v" =~ '^[[:digit:]]+$' ; then : ; else
  v=2
fi

apply () {
  if test $v -ge 2 ; then
    echo "fix-attrs: info: applying $1" 1>&2
  fi
  got=true
  if ! /package/admin/s6-overlay-3.2.0.2/libexec/fix-attrs < "$1" ; then
    err=true
  fi
}

for file in `s6-ls "$etc/fix-attrs.d" 2>/dev/null | s6-sort` ; do
  apply "$etc/fix-attrs.d/$file"
done

if $got && test $v -ge 1 ; then
  echo 'fix-attrs: warning: fix-attrs is deprecated, please fix volume permissions in your container manager instead' 1>&2
fi
if $err && b=0`printcontenv S6_BEHAVIOUR_IF_STAGE2_FAILS` && test "$b" -ne 0 ; then
  if test $v -ge 1 ; then
    echo 'fix-attrs: warning: some fix files failed to apply' 1>&2
  fi
  if test "$b" -ge 2 ; then
    exit 1
  fi
fi
