sysconf_dir='/etc'
config_rel_dir='IMagick-7'
policy_dir='/usr/share/ImageMagick/policies'

policy_link="$sysconf_dir/$config_rel_dir/policy.xml"

policy="$1"
if [ -n "$policy" ]; then
  policies="$policy"
  echo "Warning: setting policy to $policy temporarily for testing purposes only,"
  echo "         it will not survive next ImageMagick package install; for permanent"
  echo "         setting install wanted policy package only or consider MAGICK_CONFIGURE_PATH"
  echo "         if you need different ImageMagick instaces running under different policies"
else
  policies='websafe SUSE secure limited open'
fi

for policy in $policies; do
  if [ -f $policy_dir/$policy/policy.xml ]; then
    # safest policy found
    if [ ! -e $policy_link -o -L $policy_link ]; then
      # in case policy.xml is not a link, do not overwrite it
      echo "Using '$policy' policy."
      ln -sf $policy_dir/$policy/policy.xml $policy_link
    fi
    exit 0
  fi
done

echo "No (such) policy found."
exit 0
